2015-04-28 19:03:05 +02:00
|
|
|
/**
|
|
|
|
* @fileoverview Module for loading rules from files and directories.
|
|
|
|
* @author Michael Ficarra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Requirements
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-08-14 00:06:43 +02:00
|
|
|
const fs = require("fs"),
|
2015-04-28 19:03:05 +02:00
|
|
|
path = require("path");
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Public Interface
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all rule modules from specified directory.
|
2016-02-15 23:45:53 +01:00
|
|
|
* @param {string} [rulesDir] Path to rules directory, may be relative. Defaults to `lib/rules`.
|
|
|
|
* @param {string} cwd Current working directory
|
2015-04-28 19:03:05 +02:00
|
|
|
* @returns {Object} Loaded rule modules by rule ids (file names).
|
|
|
|
*/
|
2016-02-15 23:45:53 +01:00
|
|
|
module.exports = function(rulesDir, cwd) {
|
2015-04-28 19:03:05 +02:00
|
|
|
if (!rulesDir) {
|
|
|
|
rulesDir = path.join(__dirname, "rules");
|
|
|
|
} else {
|
2016-02-15 23:45:53 +01:00
|
|
|
rulesDir = path.resolve(cwd, rulesDir);
|
2015-04-28 19:03:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 00:06:43 +02:00
|
|
|
const rules = Object.create(null);
|
2016-04-09 14:11:01 +02:00
|
|
|
|
2017-01-01 06:40:59 +01:00
|
|
|
fs.readdirSync(rulesDir).forEach(file => {
|
2015-04-28 19:03:05 +02:00
|
|
|
if (path.extname(file) !== ".js") {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-12 20:50:19 +01:00
|
|
|
rules[file.slice(0, -3)] = path.join(rulesDir, file);
|
2015-04-28 19:03:05 +02:00
|
|
|
});
|
|
|
|
return rules;
|
|
|
|
};
|