2 * @fileoverview Module for loading rules from files and directories.
3 * @author Michael Ficarra
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const fs = require("fs"),
13 path = require("path");
15 const rulesDirCache = {};
17 //------------------------------------------------------------------------------
19 //------------------------------------------------------------------------------
22 * Load all rule modules from specified directory.
23 * @param {string} relativeRulesDir Path to rules directory, may be relative.
24 * @param {string} cwd Current working directory
25 * @returns {Object} Loaded rule modules.
27 module.exports = function(relativeRulesDir, cwd) {
28 const rulesDir = path.resolve(cwd, relativeRulesDir);
30 // cache will help performance as IO operation are expensive
31 if (rulesDirCache[rulesDir]) {
32 return rulesDirCache[rulesDir];
35 const rules = Object.create(null);
37 fs.readdirSync(rulesDir).forEach(file => {
38 if (path.extname(file) !== ".js") {
41 rules[file.slice(0, -3)] = require(path.join(rulesDir, file));
43 rulesDirCache[rulesDir] = rules;