Skip to content

Commit 6fb4235

Browse files
authored
Add check comparing children lists vs. files present (#334)
1 parent 90e8c07 commit 6fb4235

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

astro.config.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { defineConfig } from "astro/config";
22
import node from "@astrojs/node";
3-
// TODO: dynamically import cheerio only when needed,
4-
// when https://github.com/withastro/astro/issues/12689 is resolved
3+
import { parseFrontmatter } from "@astrojs/markdown-remark";
54
import { load } from "cheerio";
65
import fg from "fast-glob";
76
import { remarkDefinitionList, defListHastHandlers } from "remark-definition-list";
87
import remarkDirective from "remark-directive";
98

109
import { readFile, writeFile } from "fs/promises";
11-
import { join } from "path";
10+
import { basename, dirname, join } from "path";
1211
import { fileURLToPath } from "url";
1312

1413
import { guidelinesRehypePlugins, guidelinesRemarkPlugins } from "./src/lib/markdown/guidelines";
@@ -28,14 +27,63 @@ export default defineConfig({
2827
rehypePlugins: [...guidelinesRehypePlugins],
2928
remarkRehype: {
3029
// https://github.com/wataru-chocola/remark-definition-list/issues/50#issuecomment-1445130314
31-
handlers: { ...defListHastHandlers }
32-
}
30+
handlers: { ...defListHastHandlers },
31+
},
3332
},
3433
experimental: {
3534
contentIntellisense: true,
3635
preserveScriptOrder: true,
3736
},
3837
integrations: [
38+
{
39+
/** Checks for mismatched children array vs. subdirectory contents */
40+
name: "children-check",
41+
hooks: {
42+
"astro:build:start": async () => {
43+
const groupsPath = join("guidelines", "groups");
44+
const groupFilenames = await fg.glob(join(groupsPath, "*.json"), {
45+
ignore: [join(groupsPath, "index.json")],
46+
});
47+
48+
// Check at group level (index.json -> *.json)
49+
const listedCount = JSON.parse(
50+
await readFile(join(groupsPath, "index.json"), "utf8")
51+
).length;
52+
const actualCount = groupFilenames.length;
53+
if (listedCount !== actualCount) {
54+
throw new Error(
55+
`Group index.json lists ${listedCount} children but there are ${actualCount} files`
56+
);
57+
}
58+
59+
// Check at group->guideline level (*.json -> */*.md)
60+
for (const filename of groupFilenames) {
61+
const id = basename(filename, ".json");
62+
const data = JSON.parse(await readFile(filename, "utf8"));
63+
64+
const actualCount = (await fg.glob(join(groupsPath, id, "*.md"))).length;
65+
if (data.children.length !== actualCount) {
66+
throw new Error(
67+
`Group ${id} lists ${data.children.length} children but has ${actualCount} files`
68+
);
69+
}
70+
}
71+
72+
// Check at guideline level (*/*.md -> */*/*.md)
73+
for (const filename of await fg.glob(join(groupsPath, "*", "*.md"))) {
74+
const id = join(basename(dirname(filename)), basename(filename, ".md"));
75+
const data = parseFrontmatter(await readFile(filename, "utf8")).frontmatter;
76+
77+
const actualCount = (await fg.glob(join(groupsPath, id, "*.md"))).length;
78+
if (data.children.length !== actualCount) {
79+
throw new Error(
80+
`Group ${id} lists ${data.children.length} children but has ${actualCount} files`
81+
);
82+
}
83+
}
84+
},
85+
},
86+
},
3987
{
4088
/** Filters output to reduce diff noise, esp. due to script/style/dependency updates */
4189
name: "diffable-html",

0 commit comments

Comments
 (0)