2021-09-30 20:25:38 +00:00
|
|
|
import * as colors from 'colors/safe';
|
2021-08-24 22:56:47 +00:00
|
|
|
import * as diff from 'diff';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2021-09-30 20:25:38 +00:00
|
|
|
import * as toc from '../tool/toc';
|
2021-08-24 22:56:47 +00:00
|
|
|
|
2023-02-07 01:59:36 +00:00
|
|
|
if (process.env.CI) colors.enable();
|
|
|
|
|
2021-08-24 22:56:47 +00:00
|
|
|
toc.files.forEach(file => {
|
|
|
|
const markdown = fs.readFileSync(file).toString();
|
|
|
|
const currentToc = toc.getCurrent(markdown);
|
2023-02-07 01:59:36 +00:00
|
|
|
if (currentToc === null) return;
|
2021-08-24 22:56:47 +00:00
|
|
|
|
|
|
|
const generatedToc = toc.generate(markdown);
|
|
|
|
if (currentToc === generatedToc) return;
|
|
|
|
|
2023-02-07 01:59:36 +00:00
|
|
|
console.error(colors.red(`${file}'s table of contents is incorrect:`));
|
|
|
|
const patch = diff.structuredPatch(
|
|
|
|
file,
|
|
|
|
file,
|
|
|
|
currentToc,
|
|
|
|
generatedToc,
|
|
|
|
'actual',
|
|
|
|
'expected'
|
|
|
|
);
|
|
|
|
console.error(colors.red('--- actual'));
|
|
|
|
console.error(colors.green('+++ expected'));
|
|
|
|
for (const hunk of patch.hunks) {
|
|
|
|
console.error(
|
|
|
|
`@@ -${hunk.oldStart},-${hunk.oldLines} ` +
|
|
|
|
`+${hunk.newStart},${hunk.newLines} @@`
|
|
|
|
);
|
|
|
|
for (const line of hunk.lines) {
|
|
|
|
if (line.startsWith('+')) {
|
|
|
|
console.error(colors.green(line));
|
|
|
|
} else if (line.startsWith('-')) {
|
|
|
|
console.error(colors.red(line));
|
|
|
|
} else {
|
|
|
|
console.error(colors.grey(line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.error();
|
2021-08-24 22:56:47 +00:00
|
|
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
});
|
2023-02-07 01:59:36 +00:00
|
|
|
|
|
|
|
if (process.exitCode === 1) {
|
|
|
|
console.error(
|
|
|
|
'To fix tables of contents, run ' +
|
|
|
|
colors.blue('`npx ts-node tool/update-toc.ts`') +
|
|
|
|
'.'
|
|
|
|
);
|
|
|
|
}
|