-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
49 lines (42 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @fileoverview Allows a local ESLint rules directory to be used without a command-line flag
* @author Teddy Katz
*/
'use strict';
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const fs = require('fs');
const path = require('path');
//------------------------------------------------------------------------------
// Plugin Definition
//------------------------------------------------------------------------------
const cache = {};
const ruleExtensions = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts']);
module.exports = {
get rules() {
const RULES_DIR = module.exports.RULES_DIR;
if (typeof module.exports.RULES_DIR !== 'string' && !Array.isArray(RULES_DIR)) {
throw new Error('To use eslint-plugin-rulesdir, you must load it beforehand and set the `RULES_DIR` property on the module to a string or an array of strings.');
}
const cacheKey = JSON.stringify(RULES_DIR);
if (!cache[cacheKey]) {
const rules = Array.isArray(RULES_DIR) ? RULES_DIR : [RULES_DIR];
const rulesObject = {};
rules.forEach((rulesDir) => {
fs.readdirSync(rulesDir)
.filter(filename => ruleExtensions.has(path.extname(filename)))
.map(filename => path.resolve(rulesDir, filename))
.forEach((absolutePath) => {
const ruleName = path.basename(absolutePath, path.extname(absolutePath));
if (rulesObject[ruleName]) {
throw new Error(`eslint-plugin-rulesdir found two rules with the same name: ${ruleName}`);
}
rulesObject[ruleName] = require(absolutePath);
});
});
cache[cacheKey] = rulesObject;
}
return cache[cacheKey];
},
};