Skip to content

Commit

Permalink
chore: refactor generate-rules-table into regenerate-docs tool
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jul 26, 2020
1 parent c1edf8a commit e518fe3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
36 changes: 27 additions & 9 deletions tools/generate-rules-table.ts → tools/regenerate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
Expand All @@ -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');

0 comments on commit e518fe3

Please sign in to comment.