-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign-examples.js
65 lines (53 loc) · 1.64 KB
/
design-examples.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
64
65
/**
* Global data file for design-examples.
*
* Finds all design system example templates under views/design-system/
* and exports an array of examples to be rendered e.g.
*
* [
* {
* group: 'components',
* item: 'radios',
* layout: { name: 'default', template: 'design-example-wrapper.njk' },
* type: 'conditional'
* },
* ...
* ]
*/
const path = require('node:path');
const glob = require('glob');
const isNotNull = (value) => value !== null;
function exampleToRender(layout) {
return function createExampleSettings({ group, item, type }) {
return {
group,
item,
layout,
type,
};
};
}
const designSystemTemplatesPath = path.join(__dirname, '../design-system/');
const exampleTemplatesGlobPattern = path.join(designSystemTemplatesPath, '*/**/index.njk');
const examples = glob.sync(exampleTemplatesGlobPattern)
.map((examplePath) => {
const cleanPath = examplePath
.replace(designSystemTemplatesPath, '')
.replace('/index.njk', '');
// Structure: {group}/{item}/{type}
const pathParts = cleanPath.split('/');
const isExamplePath = pathParts.length === 3;
if (isExamplePath) {
const [group, item, type] = pathParts;
return { group, item, type };
}
return null;
})
.filter(isNotNull);
const layouts = [
{ name: 'default', template: 'design-example-wrapper.njk' },
{ name: 'fullpage', template: 'design-example-wrapper-full.njk' },
{ name: 'blankpage', template: 'design-example-wrapper-blank.njk' },
];
const examplesToRender = layouts.flatMap((layout) => examples.map(exampleToRender(layout)));
module.exports = examplesToRender;