forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-docs.js
63 lines (54 loc) · 1.52 KB
/
check-docs.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
// @ts-check
// cspell:ignore astro
// This script check the docs are compatible for astro to simplify migration
// It check each docs has
// - a `title:` frontmatter
import { readFile, readdir } from "fs/promises";
import { resolve } from "path";
import { repoRoot } from "./helpers.js";
const docsFolder = resolve(repoRoot, "docs");
async function findMarkdownFiles(folder) {
const items = await readdir(folder, { withFileTypes: true });
return (
await Promise.all(
items.map(async (item) => {
if (item.isDirectory()) {
const files = await findMarkdownFiles(resolve(folder, item.name));
return files.map((x) => resolve(folder, x));
}
if (item.name.endsWith(".md")) {
return [resolve(folder, item.name)];
} else {
return [];
}
})
)
).flat();
}
await main();
async function main() {
const docs = await findMarkdownFiles(docsFolder);
const failure = [];
const regex = /^---.*title:.*---$/ms;
for (const doc of docs) {
const buffer = await readFile(doc, { encoding: "utf-8" });
const content = buffer.toString();
if (!regex.test(content)) {
failure.push(doc);
}
}
if (failure.length > 0) {
console.log("Files with missing title: frontmatter", failure);
console.log(
[
"Make sure to add front matter in the file with title, e.g.:",
"---",
"title: xyz",
"---",
].join("\n")
);
process.exit(1);
} else {
console.log("Docs look good!");
}
}