-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild-stories.mjs
195 lines (168 loc) · 4.65 KB
/
build-stories.mjs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import util from 'node:util';
import prettier from 'prettier';
import report from './report.mjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const readdir = util.promisify(fs.readdir);
const VENDOR_PREFIX = 'igc-';
const SRC_DIR = path.resolve(__dirname, '../docs/json');
const DEST_DIR = path.resolve(__dirname, '../stories');
const REPLACE_REGEX = /\/\/ region default.*\/\/ endregion/gs;
const UNION_TYPE_REGEX = /^("\w+"|[\d-]+)\s\|/;
const SUPPORTED_TYPES = ['string', 'number', 'boolean', 'Date'];
function reportMissingFiles(files) {
const msg = String.raw`
The following story files were not found:
${files.join('\n')}
Check if they are needed at all...`;
report.warn(msg);
}
const capitalize = (str) => {
const arr = str.split('-');
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
}
return arr.join(' ');
};
/**
* Fixes the TS types to appropriate controls in the storybook js presentation.
*
* @param {string} propType
* @returns
*/
function fixControlProp(propType, options) {
if (propType.startsWith('string')) {
return 'text';
}
if (propType.startsWith('Date')) {
return 'date';
}
if (propType.startsWith('number')) {
return 'number';
}
if (options) {
return {
type: options.length > 4 ? 'select' : 'inline-radio',
};
}
return propType;
}
/**
*
* @param {string} path
* @returns
*/
async function processFileMeta(path) {
let data = await readFile(path, 'utf8');
data = await JSON.parse(data);
return extractTags(data.tags[0]);
}
/**
*
* @param {object} meta
* @returns
*/
function extractTags(meta) {
return {
component: meta.name,
args: Array.from(meta.properties || [])
.filter(
(prop) =>
prop.type &&
(SUPPORTED_TYPES.some(
(type) => prop.type === type || prop.type.startsWith(`${type} `)
) ||
UNION_TYPE_REGEX.test(prop.type))
)
.map((prop) => {
const options =
UNION_TYPE_REGEX.test(prop.type) &&
!SUPPORTED_TYPES.some(
(type) => prop.type === type || prop.type.startsWith(`${type} `)
)
? prop.type.split('|').map((part) => part.trim().replace(/"/g, ''))
: undefined;
return [
prop.name,
{
type: prop.type,
description: prop.description,
options,
control: fixControlProp(prop.type, options),
defaultValue: prop.default
? prop.type === 'boolean'
? prop.default === 'true'
: prop.type === 'Date'
? undefined
: prop.default.replace(/"/g, '')
: undefined,
},
];
}),
};
}
const buildArgTypes = (meta, indent = ' ') => {
// Skip ArgTypes for "dumb" components.
if (!meta.args.length) {
return '';
}
return [
'interface ArgTypes {',
...meta.args.map(([name, obj]) => `${indent}${name}: ${obj.type};`),
'}',
].join('\n');
};
/**
*
* @param {Buffer} story
* @param {object} meta
* @returns
*/
function buildStoryMeta(story, meta) {
const storyMeta = {
title: capitalize(meta.component.replace(VENDOR_PREFIX, '')),
component: meta.component,
argTypes: {},
};
meta.args.forEach((arg) => (storyMeta.argTypes[arg[0]] = arg[1]));
let payload = `// region default\nconst metadata = ${JSON.stringify(
storyMeta,
undefined,
2
)}\nexport default metadata;\n${buildArgTypes(meta)}\n// endregion`;
payload = prettier
.format(payload, { singleQuote: true, parser: 'babel' })
.trim();
return story.toString().replace(REPLACE_REGEX, payload);
}
async function buildStories() {
const files = await readdir(SRC_DIR);
const missing = [];
for (const file of files) {
const meta = await processFileMeta(path.join(SRC_DIR, file));
const storyName = `${meta.component.replace(VENDOR_PREFIX, '')}.stories.ts`;
const outFile = path.join(DEST_DIR, storyName);
try {
const story = await readFile(outFile, 'utf8');
await writeFile(outFile, buildStoryMeta(story, meta), 'utf8');
} catch (e) {
if (e.code === 'ENOENT') {
missing.push(`\t${storyName}`);
} else {
report.error(e);
process.exit(-1);
}
}
}
if (missing.length > 0) {
reportMissingFiles(missing);
}
}
(async () => {
await buildStories();
report.success('Stories metadata generation finished\n');
})();