Files
med-notes/.pnpm-store/v10/files/ab/f9e020cda7b55ada94e0909dfea7ee31b7c25a7e39c1b4f8890a97045093c3eb2a0ed5a4c92e9bb936fc0872bc4cf41467608da2a901bad6679f052cf7fea4
2025-05-09 05:30:08 +02:00

47 lines
1.3 KiB
Plaintext

/**
* @fileoverview Module for loading rules from files and directories.
* @author Michael Ficarra
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const fs = require("node:fs"),
path = require("node:path");
const rulesDirCache = {};
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Load all rule modules from specified directory.
* @param {string} relativeRulesDir Path to rules directory, may be relative.
* @param {string} cwd Current working directory
* @returns {Object} Loaded rule modules.
*/
module.exports = function (relativeRulesDir, cwd) {
const rulesDir = path.resolve(cwd, relativeRulesDir);
// 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)] = require(path.join(rulesDir, file));
});
rulesDirCache[rulesDir] = rules;
return rules;
};