1
1
const { readFile } = require ( 'fs/promises' )
2
- const { join, parse, ParsedPath , relative } = require ( 'path' )
2
+ const { join, parse, relative } = require ( 'path' )
3
3
const { promisify } = require ( 'util' )
4
4
5
5
const glob = promisify ( require ( 'glob' ) )
@@ -14,6 +14,11 @@ const configPaths = require('../config/paths')
14
14
* Used to cache slow operations
15
15
*
16
16
* See `config/jest/globals.mjs`
17
+ *
18
+ * @type {{
19
+ * directories?: Map<string, string[]>,
20
+ * componentsData?: ComponentData[]
21
+ * }}
17
22
*/
18
23
const cache = global . cache || { }
19
24
@@ -27,7 +32,6 @@ const cache = global.cache || {}
27
32
*/
28
33
const getListing = async ( directoryPath , pattern = '**/*' , options = { } ) => {
29
34
const listing = await glob ( pattern , {
30
- allowEmpty : true ,
31
35
cwd : directoryPath ,
32
36
matchBase : true ,
33
37
nodir : true ,
@@ -51,7 +55,7 @@ const getDirectories = (directoryPath) => {
51
55
52
56
// Retrieve from cache
53
57
if ( directories ) {
54
- return directories
58
+ return Promise . resolve ( directories )
55
59
}
56
60
57
61
// Read from disk
@@ -66,12 +70,11 @@ const getDirectories = (directoryPath) => {
66
70
* @returns {function(string): boolean } Returns true for files matching every pattern
67
71
*/
68
72
const filterPath = ( patterns ) => ( entryPath ) => {
69
- const isMatch = ( pattern ) => minimatch ( entryPath , pattern , {
70
- matchBase : true
71
- } )
72
-
73
- // Return true for files matching every pattern
74
- return patterns . every ( isMatch )
73
+ return patterns . every (
74
+ ( pattern ) => minimatch ( entryPath , pattern , {
75
+ matchBase : true
76
+ } )
77
+ )
75
78
}
76
79
77
80
/**
@@ -107,16 +110,12 @@ const getComponentData = async (componentName) => {
107
110
}
108
111
109
112
// Read from disk
110
- try {
111
- const yamlPath = join ( configPaths . components , componentName , `${ componentName } .yaml` )
112
- const yamlData = yaml . load ( await readFile ( yamlPath , 'utf8' ) , { json : true } )
113
+ const yamlPath = join ( configPaths . components , componentName , `${ componentName } .yaml` )
114
+ const yamlData = yaml . load ( await readFile ( yamlPath , 'utf8' ) , { json : true } )
113
115
114
- return {
115
- name : componentName ,
116
- ...yamlData
117
- }
118
- } catch ( error ) {
119
- throw new Error ( error )
116
+ return {
117
+ name : componentName ,
118
+ ...yamlData
120
119
}
121
120
}
122
121
@@ -177,7 +176,7 @@ module.exports = {
177
176
* Directory entry path mapper callback
178
177
*
179
178
* @callback mapPathToCallback
180
- * @param {ParsedPath } file - Parsed file
179
+ * @param {import('path'). ParsedPath } file - Parsed file
181
180
* @returns {string[] } Returns path (or array of paths)
182
181
*/
183
182
@@ -186,12 +185,33 @@ module.exports = {
186
185
*
187
186
* @typedef {object } ComponentData
188
187
* @property {string } name - Component name
189
- * @property {unknown [] } [params] - Nunjucks macro options
190
- * @property {unknown [] } [examples] - Example Nunjucks macro options
188
+ * @property {ComponentOption [] } [params] - Nunjucks macro options
189
+ * @property {ComponentExample [] } [examples] - Example Nunjucks macro options
191
190
* @property {string } [previewLayout] - Nunjucks layout for component preview
192
191
* @property {string } [accessibilityCriteria] - Accessibility criteria
193
192
*/
194
193
194
+ /**
195
+ * Component option from YAML
196
+ *
197
+ * @typedef {object } ComponentOption
198
+ * @property {string } name - Option name
199
+ * @property {string } type - Option type
200
+ * @property {boolean } required - Option required
201
+ * @property {string } description - Option description
202
+ * @property {boolean } [isComponent] - Option is another component
203
+ * @property {ComponentOption[] } [params] - Nested Nunjucks macro options
204
+ */
205
+
206
+ /**
207
+ * Component example from YAML
208
+ *
209
+ * @typedef {object } ComponentExample
210
+ * @property {string } name - Example name
211
+ * @property {object } data - Example data
212
+ * @property {boolean } [hidden] - Example hidden from review app
213
+ */
214
+
195
215
/**
196
216
* Full page example from front matter
197
217
*
0 commit comments