Sha256: d592d0f226362b05eaf4f36a826ad63ea31863e79b881dc1b118e08dcc3959f5

Contents?: true

Size: 1.43 KB

Versions: 43

Compression:

Stored size: 1.43 KB

Contents

/**
 * @fileoverview Module for loading rules from files and directories.
 * @author Michael Ficarra
 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const fs = require("fs"),
    path = require("path");

const rulesDirCache = {};

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

/**
 * Load all rule modules from specified directory.
 * @param {string} [relativeRulesDir] Path to rules directory, may be relative. Defaults to `lib/rules`.
 * @param {string} cwd Current working directory
 * @returns {Object} Loaded rule modules by rule ids (file names).
 */
module.exports = function(relativeRulesDir, cwd) {

    const rulesDir = relativeRulesDir
        ? path.resolve(cwd, relativeRulesDir)
        : path.join(__dirname, "rules");

    // cache will help performance as IO operation are expensive
    if (rulesDirCache[rulesDir]) {
        return rulesDirCache[rulesDir];
    }

    const rules = Object.create(null);

    fs.readdirSync(rulesDir).forEach(file => {
        if (path.extname(file) !== ".js") {
            return;
        }
        rules[file.slice(0, -3)] = path.join(rulesDir, file);
    });
    rulesDirCache[rulesDir] = rules;

    return rules;
};

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
govuk_publishing_components-18.0.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.21.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.20.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.19.1 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.19.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.18.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.17.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.16.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.15.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.14.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.13.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.12.2 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.12.1 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.12.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.11.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.10.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.9.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.8.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.7.0 node_modules/standard/node_modules/eslint/lib/load-rules.js
govuk_publishing_components-17.6.1 node_modules/standard/node_modules/eslint/lib/load-rules.js