@@ -3,6 +3,7 @@ import node from "@astrojs/node";
3
3
import { parseFrontmatter } from "@astrojs/markdown-remark" ;
4
4
import { load } from "cheerio" ;
5
5
import fg from "fast-glob" ;
6
+ import difference from "lodash-es/difference" ;
6
7
import { remarkDefinitionList , defListHastHandlers } from "remark-definition-list" ;
7
8
import remarkDirective from "remark-directive" ;
8
9
@@ -40,31 +41,44 @@ export default defineConfig({
40
41
name : "children-check" ,
41
42
hooks : {
42
43
"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
+
43
50
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" ) ) ;
47
57
48
58
// Check at group level (index.json -> *.json)
49
- const listedCount = JSON . parse (
59
+ const topLevelChildren = JSON . parse (
50
60
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 ) {
54
63
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 ) } )`
56
67
) ;
57
68
}
58
69
59
70
// 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" ) ) ;
63
73
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 ) {
66
78
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 ) } )`
68
82
) ;
69
83
}
70
84
}
@@ -74,10 +88,14 @@ export default defineConfig({
74
88
const id = join ( basename ( dirname ( filename ) ) , basename ( filename , ".md" ) ) ;
75
89
const data = parseFrontmatter ( await readFile ( filename , "utf8" ) ) . frontmatter ;
76
90
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 ) {
79
95
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 ) } )`
81
99
) ;
82
100
}
83
101
}
0 commit comments