-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
generateReadme.js
141 lines (108 loc) · 3.91 KB
/
generateReadme.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
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
133
134
135
136
137
138
139
140
141
/**
* This script is used to inline assertions into the README.md documents.
*/
import path from 'path';
import fs from 'fs';
import _ from 'lodash';
import glob from 'glob';
import Gitdown from 'gitdown';
const trimCode = (code) => {
let lines = code.replace(/^\n/u, '').trimEnd().split('\n');
const firsLineIndentation = lines[0].match(/^\s+/u);
const lastLineIndentation = lines[lines.length - 1].match(/^\s+/u);
const firstIndentSize = firsLineIndentation ? firsLineIndentation[0].length : 0;
const lastIndentSize = lastLineIndentation ? lastLineIndentation[0].length : 0;
lines = lines.map((line, index) => {
const lineIndentSize = firstIndentSize !== 0 || index === 0 ?
Math.min(firstIndentSize, lastIndentSize) :
lastIndentSize;
return line.slice(lineIndentSize);
});
return lines.join('\n');
};
const formatCodeSnippet = (setup) => {
const paragraphs = [];
paragraphs.push(trimCode(setup.code));
if (setup.settings) {
paragraphs.push(`// Settings: ${JSON.stringify(setup.settings)}`);
}
if (setup.options) {
paragraphs.push(`// Options: ${JSON.stringify(setup.options)}`);
}
if (setup.errors) {
paragraphs.push(`// Message: ${setup.errors[0].message}`);
}
return paragraphs.join('\n');
};
const getAssertions = () => {
const assertionFiles = glob.sync(path.resolve(__dirname, '../../test/rules/assertions/*.js'));
const assertionNames = assertionFiles.map((filePath) => {
return path.basename(filePath, '.js');
});
const assertionCodes = assertionFiles.map((filePath) => {
// eslint-disable-next-line import/no-dynamic-require
const codes = require(filePath);
return {
invalid: _.map(codes.invalid, formatCodeSnippet),
valid: _.map(codes.valid, formatCodeSnippet),
};
});
return _.zipObject(assertionNames, assertionCodes);
};
const getSomeBranch = () => {
const gitConfig = fs.readFileSync(path.join(__dirname, '../../.git/config')).toString();
const [, branch] = /\[branch "([^"]+)"\]/u.exec(gitConfig) || [];
return branch;
};
const generateReadme = async () => {
const assertions = getAssertions();
const gitdown = Gitdown.readFile(path.join(__dirname, '../../.README/README.md'));
gitdown.setConfig({
gitinfo: {
defaultBranchName: getSomeBranch() || 'master',
gitPath: path.join(__dirname, '../../.git'),
},
});
let documentBody = await gitdown.get();
documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/gui, (assertionsBlock) => {
const ruleName = assertionsBlock.match(/assertions ([a-z]+)/ui)[1];
const ruleAssertions = assertions[ruleName];
if (!ruleAssertions) {
throw new Error(`No assertions available for rule "${ruleName}".`);
}
return 'The following patterns are considered problems:\n\n````js\n' + ruleAssertions.invalid.join('\n\n') +
'\n````\n\nThe following patterns are not considered problems:\n\n````js\n' + ruleAssertions.valid.join('\n\n') + '\n````\n';
});
return documentBody;
};
const generateReadmeAndWriteToDisk = async () => {
const readme = await generateReadme();
const dist = path.join(__dirname, '..', '..', 'README.md');
fs.writeFileSync(dist, readme);
};
const assertReadmeIsUpToDate = async () => {
const readme = await generateReadme();
const readmePath = path.join(__dirname, '..', '..', 'README.md');
const isUpToDate = fs.readFileSync(readmePath).toString() === readme;
if (!isUpToDate) {
throw new Error('Readme is not up to date, please run `npm run create-readme` to update it.');
}
};
const main = async () => {
try {
const hasCheckFlag = process.argv.some((arg) => {
return arg === '--check';
});
if (hasCheckFlag) {
await assertReadmeIsUpToDate();
} else {
await generateReadmeAndWriteToDisk();
}
} catch (error) {
/* eslint-disable-next-line no-console */
console.error(error);
process.exit(1);
}
};
main();
export default generateReadme;