-
Notifications
You must be signed in to change notification settings - Fork 3
/
gentypes.js
59 lines (49 loc) · 1.5 KB
/
gentypes.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
const path = require('path');
const fs = require('fs');
const components = require('./components-data.json');
let output = [
...fs.readFileSync('./extra-types.d.ts').toString().split('\n'),
`export class SvelteComponent {`,
`\t$$prop_def: {};`,
`\t$$slot_def: {};`,
``,
`\t$on(event: string, handler: (e: CustomEvent) => any): () => void;`,
`}`,
];
components.forEach((component) => {
let $$prop_def = [];
let $on = "";
component.exports.forEach((prop) => {
$$prop_def.push(
"\t\t/**",
);
if (prop.description !== undefined) {
$$prop_def.push(
`\t\t * ${prop.description.replace(/\n/g, '\n\t\t * ')}`,
);
}
if (prop.default !== undefined) {
$$prop_def.push(`\t\t * @default ${prop.default}`);
}
$$prop_def.push("\t\t */");
$$prop_def.push(`\t\t${prop.name}${prop.default !== undefined || prop.readonly ? '?' : ''}: ${prop.type.replace(/\.</g, '<') || 'any'};`);
});
if (Object.keys(component.dispatch).length > 0) {
$on = `$on(event: ${Object.keys(component.dispatch).map((name) => `'${name}'`).join('|')}, handler: (e: CustomEvent<${Object.keys(component.dispatch).map((name) => component.dispatch[name].type).join('|')}>) => any): () => void;`
}
output.push(`export class ${component.name} extends SvelteComponent {`);
if ($$prop_def.length > 0) {
output.push(
`\t$$prop_def: {`,
$$prop_def.join('\n'),
"\t}"
);
}
if ($on) {
output.push(
`\t${$on}`
);
}
output.push(`}`);
});
fs.writeFileSync(path.join('dist', 'index.d.ts'), output.join('\n'));