-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgenerate-readme-table.js
61 lines (50 loc) · 1.72 KB
/
generate-readme-table.js
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
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const rules = require('..').rules;
const README_LOCATION = path.resolve(__dirname, '..', 'README.md');
const BEGIN_TABLE_MARKER = '<!-- __BEGIN AUTOGENERATED TABLE__ -->\n';
const END_TABLE_MARKER = '\n<!-- __END AUTOGENERATED TABLE__ -->';
const expectedTableLines = Object.keys(rules)
.sort()
.reduce(
(lines, ruleId) => {
const rule = rules[ruleId];
lines.push(
[
`[${ruleId}](https://github.com/eslint-community/eslint-plugin-eslint-plugin/blob/master/docs/rules/${ruleId}.md)`,
rule.meta.docs.recommended ? '✔️' : '',
rule.meta.fixable ? '🛠' : '',
rule.meta.hasSuggestions ? '💡' : '',
rule.meta.docs.description,
].join(' | ')
);
return lines;
},
[
'Name | ✔️ | 🛠 | 💡 | Description',
'----- | ----- | ----- | ----- | -----',
]
)
.join('\n');
const readmeContents = fs.readFileSync(README_LOCATION, 'utf8');
if (!readmeContents.includes(BEGIN_TABLE_MARKER)) {
throw new Error(
`Could not find '${BEGIN_TABLE_MARKER}' marker in README.md.`
);
}
if (!readmeContents.includes(END_TABLE_MARKER)) {
throw new Error(`Could not find '${END_TABLE_MARKER}' marker in README.md.`);
}
const linesStartIndex =
readmeContents.indexOf(BEGIN_TABLE_MARKER) + BEGIN_TABLE_MARKER.length;
const linesEndIndex = readmeContents.indexOf(END_TABLE_MARKER);
const updatedReadmeContents =
readmeContents.slice(0, linesStartIndex) +
expectedTableLines +
readmeContents.slice(linesEndIndex);
if (module.parent) {
module.exports = updatedReadmeContents;
} else {
fs.writeFileSync(README_LOCATION, updatedReadmeContents);
}