-
Notifications
You must be signed in to change notification settings - Fork 73
/
rules.js
59 lines (51 loc) · 1.51 KB
/
rules.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
// Operations on rules
var fs = require('fs');
var path = require('path');
function getAllRules(additionalRulesDirs) {
var rules = {};
var rulesDirs = [
path.join(__dirname, 'rules')
].concat(additionalRulesDirs || []);
rulesDirs.forEach(function(rulesDir) {
rulesDir = path.resolve(rulesDir);
fs.readdirSync(rulesDir).forEach(function(file) {
var rule = require(path.join(rulesDir, file));
rules[rule.name] = rule;
});
});
return rules;
}
function getRule(rule, additionalRulesDirs) {
return getAllRules(additionalRulesDirs)[rule];
}
function doesRuleExist(rule, additionalRulesDirs) {
return getRule(rule, additionalRulesDirs) !== undefined;
}
function isRuleEnabled(ruleConfig) {
if (Array.isArray(ruleConfig)) {
return ruleConfig[0] === 'on';
}
return ruleConfig === 'on';
}
function runAllEnabledRules(feature, file, configuration, additionalRulesDirs) {
var errors = [];
var rules = getAllRules(additionalRulesDirs);
Object.keys(rules).forEach(function(ruleName) {
var rule = rules[ruleName];
if (isRuleEnabled(configuration[rule.name])) {
var ruleConfig = Array.isArray(configuration[rule.name]) ? configuration[rule.name][1] : {};
var error = rule.run(feature, file, ruleConfig);
if (error) {
errors = errors.concat(error);
}
}
});
return errors;
}
module.exports = {
doesRuleExist: doesRuleExist,
isRuleEnabled: isRuleEnabled,
runAllEnabledRules: runAllEnabledRules,
getRule: getRule,
getAllRules: getAllRules
};