-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract-component-props.js
56 lines (46 loc) · 1.76 KB
/
extract-component-props.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
import fs from 'node:fs';
import { withDefaultConfig } from 'react-docgen-typescript';
const options = {
savePropValueAsString: true,
};
const components = withDefaultConfig({
...options,
}).parse(['./node_modules/@obosbbl/grunnmuren-react/src/index.ts']);
// see re-exports-props.ts
const propFixes = withDefaultConfig({
...options,
}).parse(['./re-exports-props.ts']);
for (const componentToFix of Object.values(propFixes)) {
const toUpdate = components.find(
(c) => c.displayName === componentToFix.displayName,
);
toUpdate.props = componentToFix.props;
}
const outputPath = './component-props.ts';
console.log(`Writing props to "${outputPath}"...\n`);
let withoutError = true;
try {
fs.writeFileSync(
outputPath,
Object.values(components)
.map((prop) => {
const prettifiedName = prop.displayName.replace('_', '');
const output = `export const ${prettifiedName} = ${JSON.stringify({ ...prop, displayName: prettifiedName }, null, 2)}`;
// Quick fix to also expose ListBoxItem, ListBoxSection and ListBoxHeader as ComboboxItem, ComboboxSection, ComboboxHeader, SelectItem, SelectSection and SelectHeader
if (prettifiedName.startsWith('ListBox')) {
return `${output}
export const ${prettifiedName.replace('ListBox', 'Combobox')} = ${JSON.stringify({ ...prop, displayName: prettifiedName }, null, 2)}
export const ${prettifiedName.replace('ListBox', 'Select')} = ${JSON.stringify({ ...prop, displayName: prettifiedName }, null, 2)}
`;
}
return output;
})
.join('\n'),
);
} catch (error) {
withoutError = false;
console.error('\x1b[31m%s\x1b[0m', `\nSomething went wrong: ${error}`);
}
if (withoutError) {
console.log('\x1b[32m%s\x1b[0m', '\n✔ Done!');
}