From e518fe3d98575ff7cc199193b85ad6e53289271f Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 12 Jul 2020 14:52:19 +1200 Subject: [PATCH] chore: refactor `generate-rules-table` into `regenerate-docs` tool --- package.json | 2 +- ...rate-rules-table.ts => regenerate-docs.ts} | 36 ++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) rename tools/{generate-rules-table.ts => regenerate-docs.ts} (78%) diff --git a/package.json b/package.json index 00bcad985..4efd759a9 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "prepack": "yarn build", "prettylint": "prettylint docs/**/*.md README.md package.json", "test": "jest", - "tools:generate-rules-table": "ts-node -T tools/generate-rules-table", + "tools:regenerate-docs": "ts-node -T tools/regenerate-docs", "typecheck": "tsc -p ." }, "husky": { diff --git a/tools/generate-rules-table.ts b/tools/regenerate-docs.ts similarity index 78% rename from tools/generate-rules-table.ts rename to tools/regenerate-docs.ts index eb9b33186..be78f8aa8 100644 --- a/tools/generate-rules-table.ts +++ b/tools/regenerate-docs.ts @@ -3,9 +3,19 @@ import * as fs from 'fs'; import * as path from 'path'; import { TSESLint } from '@typescript-eslint/experimental-utils'; -import prettier from 'prettier'; +import prettier, { Options } from 'prettier'; +import { prettier as prettierRC } from '../package.json'; import config from '../src/index'; +const pathTo = { + readme: path.resolve(__dirname, '../README.md'), + rules: path.resolve(__dirname, '../src/rules'), + docs: path.resolve(__dirname, '../docs'), +}; + +const format = (str: string): string => + prettier.format(str, { ...(prettierRC as Options), parser: 'markdown' }); + type FixType = 'fixable' | 'suggest'; interface RuleDetails { @@ -84,10 +94,7 @@ const importDefault = (moduleName: string) => interopRequireDefault(require(moduleName)).default; const requireJestRule = (name: string): RuleModule => - importDefault( - `../src/rules/${name}`, - // path.join('..', 'src', 'rules', name), - ) as RuleModule; + importDefault(path.join(pathTo.rules, name)) as RuleModule; const details: RuleDetails[] = Object.keys(config.configs.all.rules) .map(name => name.split('/')[1]) @@ -108,10 +115,21 @@ const details: RuleDetails[] = Object.keys(config.configs.all.rules) }), ); -let readme = fs.readFileSync(path.resolve(__dirname, '../README.md'), 'utf8'); +details.forEach(({ name, description }) => { + const pathToDoc = path.join(pathTo.docs, 'rules', `${name}.md`); -readme = updateRulesList(details, readme); + const contents = fs + .readFileSync(pathToDoc) + .toString() + .split('\n'); -readme = prettier.format(readme, { parser: 'markdown' }); + contents[0] = `# ${description} (\`${name}\`)`; + + fs.writeFileSync(pathToDoc, format(contents.join('\n'))); +}); + +let readme = fs.readFileSync(pathTo.readme, 'utf8'); + +readme = updateRulesList(details, readme); -fs.writeFileSync(path.resolve(__dirname, '../README.md'), readme, 'utf8'); +fs.writeFileSync(pathTo.readme, format(readme), 'utf8');