|
| 1 | +/** |
| 2 | + * @fileoverview Generates the recommended configuration and import file for rules. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * node tools/build-rules.js |
| 6 | + * |
| 7 | + * @author Nicholas C. Zakas |
| 8 | + */ |
| 9 | + |
| 10 | +//----------------------------------------------------------------------------- |
| 11 | +// Imports |
| 12 | +//----------------------------------------------------------------------------- |
| 13 | + |
| 14 | +import fs from "node:fs"; |
| 15 | +import path from "node:path"; |
| 16 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 17 | + |
| 18 | +//----------------------------------------------------------------------------- |
| 19 | +// Main |
| 20 | +//----------------------------------------------------------------------------- |
| 21 | + |
| 22 | +const thisDir = path.dirname(fileURLToPath(import.meta.url)); |
| 23 | +const rulesPath = path.resolve(thisDir, "../src/rules"); |
| 24 | +const rules = fs.readdirSync(rulesPath); |
| 25 | +const recommended = []; |
| 26 | + |
| 27 | +for (const ruleId of rules) { |
| 28 | + const rulePath = path.resolve(rulesPath, ruleId); |
| 29 | + const rule = await import(pathToFileURL(rulePath)); |
| 30 | + |
| 31 | + if (rule.default.meta.docs.recommended) { |
| 32 | + recommended.push(ruleId); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const output = `const rules = /** @type {const} */ ({ |
| 37 | + ${recommended.map(id => `"json/${id.slice(0, -3)}": "error"`).join(",\n ")} |
| 38 | +}); |
| 39 | +
|
| 40 | +export default rules; |
| 41 | +`; |
| 42 | + |
| 43 | +fs.mkdirSync(path.resolve(thisDir, "../src/build"), { recursive: true }); |
| 44 | +fs.writeFileSync( |
| 45 | + path.resolve(thisDir, "../src/build/recommended-config.js"), |
| 46 | + output, |
| 47 | +); |
| 48 | + |
| 49 | +console.log("Recommended rules generated successfully."); |
| 50 | + |
| 51 | +const rulesOutput = ` |
| 52 | +${rules.map((id, index) => `import rule${index} from "../rules/${id}";`).join("\n")} |
| 53 | +
|
| 54 | +export default { |
| 55 | + ${rules.map((id, index) => `"${id.slice(0, -3)}": rule${index},`).join("\n ")} |
| 56 | +}; |
| 57 | +`.trim(); |
| 58 | + |
| 59 | +fs.writeFileSync(path.resolve(thisDir, "../src/build/rules.js"), rulesOutput); |
| 60 | + |
| 61 | +console.log("Rules import file generated successfully."); |
0 commit comments