UNPKG

1.25 kBJavaScriptView Raw
1/**
2 * @fileoverview Module for loading rules from files and directories.
3 * @author Michael Ficarra
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const fs = require("fs"),
13 path = require("path");
14
15//------------------------------------------------------------------------------
16// Public Interface
17//------------------------------------------------------------------------------
18
19/**
20 * Load all rule modules from specified directory.
21 * @param {string} [rulesDir] Path to rules directory, may be relative. Defaults to `lib/rules`.
22 * @param {string} cwd Current working directory
23 * @returns {Object} Loaded rule modules by rule ids (file names).
24 */
25module.exports = function(rulesDir, cwd) {
26 if (!rulesDir) {
27 rulesDir = path.join(__dirname, "rules");
28 } else {
29 rulesDir = path.resolve(cwd, rulesDir);
30 }
31
32 const rules = Object.create(null);
33
34 fs.readdirSync(rulesDir).forEach(function(file) {
35 if (path.extname(file) !== ".js") {
36 return;
37 }
38 rules[file.slice(0, -3)] = path.join(rulesDir, file);
39 });
40 return rules;
41};