-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsanity.js
91 lines (78 loc) · 2.29 KB
/
sanity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
var assign = require('lodash/assign'),
forEach = require('lodash/forEach'),
isEmpty = require('lodash/isEmpty'),
os = require('os'),
colors = require('colors/safe');
var sanity = {
reporter: function(report) {
console.error.call(null, report);
},
matchers: {
defined: function(value) {
return !isEmpty(value);
},
truthy: function(value) {
return !!value;
},
falsy: function(value) {
return !!value === false;
}
},
check: function(required, options) {
var failures = [],
message = '';
options = assign({
gagged: false,
goodBook: null,
passiveAggressive: false,
recover: null,
source: process.env,
zazz: true
}, options);
if (options.goodBook) {
sanity.preach(options.goodBook, options.source);
}
required.forEach(function(key) {
var matcher = sanity.matchers.defined;
if(typeof key !== 'string') {
matcher = typeof key.matcher === 'string' ? sanity.matchers[key.matcher] : key.matcher;
key = key.key;
}
if(matcher(options.source[key]) !== true) {
failures.push({key: key, value: options.source[key]});
}
});
if(failures.length > 0) {
var heading = 'ERROR: Required settings are not correct!',
errs = [(options.zazz ? colors.red(heading) : heading)];
failures.forEach(function(failure) {
errs.push(' ' + (options.zazz ? colors.bold(failure.key) : failure.key) + ': ' + failure.value);
});
message = errs.join(os.EOL);
if(!options.gagged) {
this.reporter(message);
}
}
if(typeof options.recover === 'function') {
var failedKeys = failures.map(function(entry) {
return entry.key;
});
return options.recover(message !== '' ? message : null, failedKeys);
}
if(!options.passiveAggressive && failures.length > 0) {
process.exit(1);
}
},
preach: function(insights, audience) {
forEach(insights, function(commandment, word) {
audience[word] = commandment;
});
return audience;
}
};
exports.sanity = sanity;
exports.check = sanity.check;
exports.reporter = sanity.reporter;
exports.matchers = sanity.matchers;
exports.preach = sanity.preach;