blob: 7ad98c97d736f9c6c1867dd1b8825295972d5962 [file] [log] [blame]
Tim van der Lippe835724b2020-03-10 16:48:02 +00001/**
2 * @fileoverview Allows a local ESLint rules directory to be used without a command-line flag
3 * @author Teddy Katz
4 */
5
6'use strict';
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const fs = require('fs');
13const path = require('path');
14
15//------------------------------------------------------------------------------
16// Plugin Definition
17//------------------------------------------------------------------------------
18
19const cache = {};
20module.exports = {
21 get rules() {
22 const RULES_DIR = module.exports.RULES_DIR;
23 if (typeof module.exports.RULES_DIR !== 'string') {
24 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.');
25 }
26 if (!cache[RULES_DIR]) {
27 cache[RULES_DIR] = fs.readdirSync(RULES_DIR)
28 .filter(filename => filename.endsWith('.js'))
29 .map(filename => path.resolve(RULES_DIR, filename))
30 .reduce((rules, absolutePath) => Object.assign(rules, { [path.basename(absolutePath, '.js')]: require(absolutePath) }), {});
31 }
32 return cache[RULES_DIR];
33 },
34};