-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlint.js
89 lines (67 loc) · 2.51 KB
/
lint.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
// @ts-check
// Loops through all the sample code and ensures that twoslash doesn't raise
// pnpm run --filter=glossary lint
const chalk = require("chalk");
const tick = chalk.bold.greenBright("✓");
const cross = chalk.bold.redBright("⤫");
const { readdirSync, readFileSync } = require("fs");
const { join } = require("path");
const remark = require("remark");
const remarkTwoSlash = require("remark-shiki-twoslash");
const { read } = require("gray-matter");
const languages = readdirSync(join(__dirname, "..", "copy")).filter((f) => !f.startsWith("."));
console.log("Linting the sample code which uses twoslasher in ts-config");
// Pass in a 2nd arg to filter which markdown to run
const filterString = process.argv[2] ? process.argv[2] : "";
const errorReports = [];
const go = async () => {
for (const lang of languages) {
console.log("\n\nLanguage: " + chalk.bold(lang) + "\n");
const locale = join(__dirname, "..", "copy", lang);
let options;
try {
options = readdirSync(join(locale)).filter((f) => !f.startsWith("."));
} catch {
errorReports.push({
path: join(locale, "options"),
error: `Options directory ${join(locale, "options")} doesn't exist`,
});
return;
}
for (const option of options) {
if (filterString.length && !option.includes(filterString)) return;
const optionPath = join(locale, option);
const markdown = readFileSync(optionPath, "utf8");
const markdownAST = remark().parse(markdown);
let hasError = false;
try {
await remarkTwoSlash.default({})(markdownAST);
} catch (error) {
hasError = true;
errorReports.push({ path: optionPath, error });
}
const optionFile = read(optionPath);
if (!optionFile.data.display) {
hasError = true;
// prettier-ignore
errorReports.push({ path: optionPath, error: new Error("Did not have a 'display' property in the YML header") });
}
const sigil = hasError ? cross : tick;
const name = hasError ? chalk.red(option) : option;
process.stdout.write(name + " " + sigil + ", ");
}
}
if (errorReports.length) {
process.exitCode = 1;
errorReports.forEach((err) => {
console.log(`\n> ${chalk.bold.red(err.path)}\n`);
err.error.stack = undefined;
console.log(err.error);
});
console.log("\n\n");
console.log(
"Note: you can add an extra argument to the lint script ( pnpm run --filter=glossary lint [opt] ) to just run one lint."
);
}
};
go();