/
regenerate-docs.ts
132 lines (105 loc) · 3.89 KB
/
regenerate-docs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env ts-node-transpile-only
import * as fs from 'fs';
import * as path from 'path';
import { TSESLint } from '@typescript-eslint/experimental-utils';
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 {
name: string;
description: string;
fixable: FixType | false;
}
type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
};
const staticElements = {
listHeaderRow: ['Rule', 'Description', 'Configurations', 'Fixable'],
listSpacerRow: Array(5).fill('-'),
};
const getConfigurationColumnValueForRule = (rule: RuleDetails): string => {
if (`jest/${rule.name}` in config.configs.recommended.rules) {
return '![recommended][]';
}
if (`jest/${rule.name}` in config.configs.style.rules) {
return '![style][]';
}
return '';
};
const buildRuleRow = (rule: RuleDetails): string[] => [
`[${rule.name}](docs/rules/${rule.name}.md)`,
rule.description,
getConfigurationColumnValueForRule(rule),
rule.fixable ? `![${rule.fixable}][]` : '',
];
const generateRulesListMarkdown = (details: RuleDetails[]): string =>
[
staticElements.listHeaderRow,
staticElements.listSpacerRow,
...details
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
.map(buildRuleRow),
]
.map(column => [...column, ' '].join('|'))
.join('\n');
const updateRulesList = (details: RuleDetails[], markdown: string): string => {
const listBeginMarker = `<!-- begin rules list -->`;
const listEndMarker = `<!-- end rules list -->`;
const listStartIndex = markdown.indexOf(listBeginMarker);
const listEndIndex = markdown.indexOf(listEndMarker);
if (listStartIndex === -1 || listEndIndex === -1) {
throw new Error(`cannot find start or end of rules list`);
}
return [
markdown.substring(0, listStartIndex - 1),
listBeginMarker,
'',
generateRulesListMarkdown(details),
'',
markdown.substring(listEndIndex),
].join('\n');
};
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
/* istanbul ignore next */
const interopRequireDefault = (obj: any): { default: any } =>
obj && obj.__esModule ? obj : { default: obj };
const importDefault = (moduleName: string) =>
// eslint-disable-next-line @typescript-eslint/no-require-imports
interopRequireDefault(require(moduleName)).default;
const requireJestRule = (name: string): RuleModule =>
importDefault(path.join(pathTo.rules, name)) as RuleModule;
const details: RuleDetails[] = Object.keys(config.configs.all.rules)
.map(name => name.split('/')[1])
.map(name => [name, requireJestRule(name)] as const)
.filter(
(nameAndRule): nameAndRule is [string, Required<RuleModule>] =>
!!nameAndRule[1].meta && !nameAndRule[1].meta.deprecated,
)
.map(
([name, rule]): RuleDetails => ({
name,
description: rule.meta.docs.description,
fixable: rule.meta.fixable
? 'fixable'
: rule.meta.docs.suggestion
? 'suggest'
: false,
}),
);
details.forEach(({ name, description }) => {
const pathToDoc = path.join(pathTo.docs, 'rules', `${name}.md`);
const contents = fs.readFileSync(pathToDoc).toString().split('\n');
contents[0] = `# ${description} (\`${name}\`)`;
fs.writeFileSync(pathToDoc, format(contents.join('\n')));
});
let readme = fs.readFileSync(pathTo.readme, 'utf8');
readme = updateRulesList(details, readme);
fs.writeFileSync(pathTo.readme, format(readme), 'utf8');