-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathbuild-vue-typings.js
209 lines (184 loc) · 6.04 KB
/
build-vue-typings.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
/* eslint no-console: "off" */
/* eslint global-require: "off" */
/* eslint no-param-reassign: "off" */
const getOutput = require('./get-output.js');
const fs = require('./utils/fs-extra.js');
const { COLOR_PROPS, ICON_PROPS, ROUTER_PROPS, ACTIONS_PROPS } = require('./ts-extend-props.js');
const extendMap = {
colorProps: COLOR_PROPS,
iconProps: ICON_PROPS,
routerProps: ROUTER_PROPS,
actionsProps: ACTIONS_PROPS,
};
function generateComponentEvents(fileContent) {
if (!fileContent.includes('emits: [')) return '';
let events;
fileContent.replace(/emits: \[([^\]]*)\]/, (str, eventsString) => {
events = eventsString
.split(',')
.map((ev) => ev.replace(/'/g, '').trim())
.filter((ev) => !!ev);
});
if (events && events.length) {
return [
`(${events.map((ev) => `"${ev}"`).join(' | ')})[]`,
`${events.map((ev) => `"${ev}"`).join(' | ')}`,
].join(',\n ');
}
return '';
}
function generateComponentProps(fileContent) {
// eslint-disable-next-line
let propsContent = fileContent.match(/props\:\ {([^ъ]*)},\n (emits|setup)/g);
if (propsContent && propsContent[0]) {
propsContent = propsContent[0]
.replace('props: {', '')
.replace('},\n emits', '')
.replace('},\n setup', '');
}
if (!propsContent) {
return '';
}
const props = [];
const extendWith = [];
(propsContent.match(/\.\.\.([a-zA-Z]*)/g) || []).forEach((prop) => {
extendWith.push(prop.replace('...', ''));
});
const getType = (str) => {
if (str.includes('['))
str = str
// eslint-disable-next-line
.replace(/[\[\]]*/g, '')
.split(', ')
.map((t) => t.replace(',', ''));
else if (str.includes(',')) str = str.replace(',', '');
return str;
};
// eslint-disable-next-line
propsContent.replace(/\n ([a-zA-Z0-9]*): ([\[\], a-zA-Z0-9]*)/g, (str, name, type) => {
if (!type) return;
props.push({ name, type: getType(type) });
});
// eslint-disable-next-line
propsContent.replace(/\n ([a-zA-Z0-9]*): {([^ъ^\}]*)}/g, (str1, name, typeObj) => {
const prop = { name };
// eslint-disable-next-line
typeObj.replace(/type: ([\[\], a-zA-Z0-9]*)/, (str2, typeStr) => {
prop.type = getType(typeStr);
});
if (typeObj.includes('default: ')) {
prop.default = typeObj.split('default: ')[1].replace(',\n', '').trim();
}
props.push(prop);
});
const toVueType = (type) => {
if (type === 'string') return 'String';
if (type === 'boolean') return 'Boolean';
if (type === 'number') return 'Number';
if (type === 'any') return 'Object';
return '';
};
extendWith.forEach((extendGroup) => {
extendMap[extendGroup]
.trim()
.split('\n')
.forEach((propsRow) => {
// eslint-disable-next-line
let [name, type] = propsRow.replace(/;/, '').split('?: ');
if (type.includes(' | ')) {
type = type.split(' | ').map(toVueType);
} else {
type = toVueType(type);
}
props.push({ name, type });
});
});
const getVueType = ({ type }) => {
const plainType = (t) => {
if (t === 'String') return 'StringConstructor';
if (t === 'Boolean') return 'BooleanConstructor';
if (t === 'Number') return 'NumberConstructor';
if (t === 'Function') return 'FunctionConstructor';
if (t === 'Object') return 'ObjectConstructor';
if (t === 'Array') return 'ArrayConstructor';
return 'any';
};
if (typeof type === 'string') {
return plainType(type);
}
return type.map(plainType).join(' | ');
};
const getVueDefault = (prop) => {
if ('default' in prop) {
const value = prop.default;
if (value.includes("'")) return 'string';
if (value === 'true' || value === 'false') return 'boolean';
if (!Number.isNaN(parseFloat(value))) return 'number';
return `${prop.default}`;
}
return '';
};
return props
.map((prop) => {
const defaultValue = getVueDefault(prop);
return `
${prop.name}: {
type: ${getVueType(prop)};${defaultValue ? `\n default: ${defaultValue};` : ''}
}`;
})
.join(',\n');
}
function generateComponentTypings(componentName, fileContent) {
if (componentName.includes('Swiper') || componentName.includes('Skeleton')) {
return fileContent.replace('<script>', '').replace('</script>', '');
}
return `
import { ComponentOptionsMixin, DefineComponent, PropType } from 'vue';
// IMPORTS
declare const ${componentName}: DefineComponent<
{
// PROPS
},
() => JSX.Element,
unknown,
{},
{},
ComponentOptionsMixin,
ComponentOptionsMixin,
// EVENTS
>;
export default ${componentName};
`
.replace('// IMPORTS', '')
.replace('// PROPS', generateComponentProps(fileContent))
.replace('// EVENTS', generateComponentEvents(fileContent));
}
function buildTypings(cb) {
const output = `${getOutput()}/vue`;
const files = fs.readdirSync('src/vue/components').filter((file) => file.indexOf('.d.ts') < 0);
const componentImports = [];
const componentExports = [];
files.forEach((fileName) => {
const componentName = fileName
.replace('.vue', '')
.split('-')
.map((word) => word[0].toUpperCase() + word.substr(1))
.join('');
const fileBase = fileName.replace('.vue', '');
componentImports.push(`import f7${componentName} from './components/${fileBase}.js';`);
componentExports.push(`f7${componentName}`);
const typingsContent = generateComponentTypings(
componentName,
fs.readFileSync(`src/vue/components/${fileName}`, 'utf-8'),
);
fs.writeFileSync(`${output}/components/${fileBase}.d.ts`, typingsContent);
});
const mainTypings = fs
.readFileSync('src/vue/framework7-vue.d.ts', 'utf-8')
.replace('// IMPORT_COMPONENTS', componentImports.join('\n'))
.replace('// EXPORT_COMPONENTS', `export { ${componentExports.join(', ')} }`);
fs.writeFileSync(`${output}/framework7-vue.d.ts`, mainTypings);
if (cb) cb();
}
module.exports = buildTypings;