Skip to content

Commit 3dc9018

Browse files
authored
Improve error messaging around children/filesystem discrepancies (#349)
1 parent 062b869 commit 3dc9018

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

astro.config.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import node from "@astrojs/node";
33
import { parseFrontmatter } from "@astrojs/markdown-remark";
44
import { load } from "cheerio";
55
import fg from "fast-glob";
6+
import difference from "lodash-es/difference";
67
import { remarkDefinitionList, defListHastHandlers } from "remark-definition-list";
78
import remarkDirective from "remark-directive";
89

@@ -40,31 +41,44 @@ export default defineConfig({
4041
name: "children-check",
4142
hooks: {
4243
"astro:build:start": async () => {
44+
const getUniqueEntries = (array1: any[], array2: any[]) =>
45+
(array1.length > array2.length
46+
? difference(array1, array2)
47+
: difference(array2, array1)
48+
).join(", ");
49+
4350
const groupsPath = join("guidelines", "groups");
44-
const groupFilenames = await fg.glob(join(groupsPath, "*.json"), {
45-
ignore: [join(groupsPath, "index.json")],
46-
});
51+
const groupIds = (
52+
await fg.glob("*.json", {
53+
cwd: groupsPath,
54+
ignore: ["index.json"],
55+
})
56+
).map((filename) => basename(filename, ".json"));
4757

4858
// Check at group level (index.json -> *.json)
49-
const listedCount = JSON.parse(
59+
const topLevelChildren = JSON.parse(
5060
await readFile(join(groupsPath, "index.json"), "utf8")
51-
).length;
52-
const actualCount = groupFilenames.length;
53-
if (listedCount !== actualCount) {
61+
);
62+
if (topLevelChildren.length !== groupIds.length) {
5463
throw new Error(
55-
`Group index.json lists ${listedCount} children but there are ${actualCount} files`
64+
`groups/index.json lists ${topLevelChildren.length} children but there are ${
65+
groupIds.length
66+
} files (check: ${getUniqueEntries(topLevelChildren, groupIds)})`
5667
);
5768
}
5869

5970
// 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"));
71+
for (const id of groupIds) {
72+
const data = JSON.parse(await readFile(join(groupsPath, `${id}.json`), "utf8"));
6373

64-
const actualCount = (await fg.glob(join(groupsPath, id, "*.md"))).length;
65-
if (data.children.length !== actualCount) {
74+
const actualFiles = (await fg.glob("*.md", { cwd: join(groupsPath, id) })).map(
75+
(filename) => basename(filename, ".md")
76+
);
77+
if (data.children.length !== actualFiles.length) {
6678
throw new Error(
67-
`Group ${id} lists ${data.children.length} children but has ${actualCount} files`
79+
`groups/${id}.json lists ${data.children.length} children but groups/${id}/ contains ${
80+
actualFiles.length
81+
} files (check: ${getUniqueEntries(actualFiles, data.children)})`
6882
);
6983
}
7084
}
@@ -74,10 +88,14 @@ export default defineConfig({
7488
const id = join(basename(dirname(filename)), basename(filename, ".md"));
7589
const data = parseFrontmatter(await readFile(filename, "utf8")).frontmatter;
7690

77-
const actualCount = (await fg.glob(join(groupsPath, id, "*.md"))).length;
78-
if (data.children.length !== actualCount) {
91+
const actualFiles = (await fg.glob("*.md", { cwd: join(groupsPath, id) })).map(
92+
(filename) => basename(filename, ".md")
93+
);
94+
if (data.children.length !== actualFiles.length) {
7995
throw new Error(
80-
`Group ${id} lists ${data.children.length} children but has ${actualCount} files`
96+
`groups/${id}.md lists ${data.children.length} children but groups/${id}/ contains ${
97+
actualFiles.length
98+
} files (check: ${getUniqueEntries(actualFiles, data.children)})`
8199
);
82100
}
83101
}

0 commit comments

Comments
 (0)