-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport-components-data.js
79 lines (66 loc) · 2.33 KB
/
export-components-data.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const path = require('path');
const fs = require('fs');
const jsdocApi = require('jsdoc-api');
const components = [];
function readdirFilesRecursiveSync(dir, output) {
output = output || [];
fs.readdirSync(dir).forEach(file => {
let fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
readdirFilesRecursiveSync(fullPath, output);
} else {
output.push(fullPath);
}
});
return output;
}
/** @type {string[]} */
const componentFiles = readdirFilesRecursiveSync(path.join('src', 'components'));
for (const entry of componentFiles) {
const componentData = {
path: entry,
name: entry.match(/([^/]+).svelte/)[1],
description: '',
slots: {},
dispatch: {},
forward: {},
};
let content = (fs.readFileSync(entry).toString().match(/<script>(.*)<\/script>/s) || [])[1];
const componentDescription = content.match(/(\s*\/\*\*[^@]+@component.*?\*\/)/sg)?.[0];
if (componentDescription) {
const commentBlock = jsdocApi.explainSync({
source: componentDescription + '\nlet __component_description;',
})[0];
componentData.description = commentBlock.tags.find(t => t.originalTitle === 'component').value;
const slotTag = commentBlock.tags.find(t => t.originalTitle === 'slot');
if (slotTag) {
componentData.slots = JSON.parse(slotTag.text);
}
const dispatchTag = commentBlock.tags.find(t => t.originalTitle === 'dispatch');
if (dispatchTag) {
componentData.dispatch = JSON.parse(dispatchTag.text);
}
const forwardTag = commentBlock.tags.find(t => t.originalTitle === 'forward');
if (forwardTag) {
componentData.forward = JSON.parse(forwardTag.text);
}
}
const commentBlocks = jsdocApi.explainSync({
source: content,
}).filter(c => Boolean(c.comment));
componentData.exports = [];
for (const block of commentBlocks) {
if (block.meta && block.meta.code && block.meta.code && block.meta.code.name && block.meta.code.name.startsWith('exports.')) {
const defaultValue = content.slice(block.meta.range[0], block.meta.range[1]).match(/=\s?(.*?);?$/)?.[1];
componentData.exports.push({
readonly: !!block.readonly,
name: block.name,
type: block.type.names.join('|'),
default: defaultValue,
description: block.description
});
}
}
components.push(componentData);
}
fs.writeFileSync(path.join(__dirname, 'components-data.json'), JSON.stringify(components));