Tim van der Lippe | 835724b | 2020-03-10 16:48:02 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 12 | const fs = require('fs'); |
| 13 | const path = require('path'); |
| 14 | |
| 15 | //------------------------------------------------------------------------------ |
| 16 | // Plugin Definition |
| 17 | //------------------------------------------------------------------------------ |
| 18 | |
| 19 | const cache = {}; |
| 20 | module.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 | }; |