This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
framework7-react-component-gen.js
328 lines (261 loc) · 11.2 KB
/
framework7-react-component-gen.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import {stringify} from 'json-fn';
import {readFileSync, writeFileSync} from 'fs';
import * as path from 'path';
import * as fs from 'fs';
import * as to from 'to-case';
import * as babel from 'babel-core';
const ensureDirectoryExistence = (filePath) => {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
};
const generateImportString = (componentToImport, path) => {
return `import {${componentToImport}} from '${path}';`
};
const getPropType = (propValue) => {
let propType = propValue.type || propValue;
if (Array.isArray(propType)) {
return propType.reduce((typesArray, nextType) => {
return [
...typesArray,
getPropType(nextType)
]
}, []).join(' | ');
} else {
switch(propType) {
case Boolean:
return 'boolean';
case Number:
return 'number';
case String:
return 'string';
case Object:
return 'Object';
case Function:
return 'Function';
case Array:
return 'any[]';
default:
return 'string';
}
}
};
const generateTypeScriptInterfaceFromProps = (props, componentName, eventList, slotList, mixin) => {
let interfaceProps = [];
if (props) {
interfaceProps = Object.keys(props).reduce((interfaceProps, nextPropName) => {
const propType = getPropType(props[nextPropName]);
return [
...interfaceProps,
` ${to.camel(nextPropName)}?: ${propType};`
];
}, []);
}
eventList.forEach(eventName => {
interfaceProps.push(` ${to.camel('on-' + eventName.split(':').join('-'))}?: (eventArgs?: any) => void;`);
});
slotList.forEach(slotName => {
interfaceProps.push(` ${to.camel(slotName + '-slot')}?: React.ReactElement<any>;`)
});
if (!props || !props.id) interfaceProps.push(' id?: string;');
if (!props || !props.className) interfaceProps.push(' className?: string;');
if (!props || !props.style) interfaceProps.push(' style?: {[cssAttribute: string]: string};');
if (interfaceProps.length) {
if (mixin) {
return `\nexport interface I${componentName}Props extends I${mixin}Props {\n${interfaceProps.join('\n')}\n}`;
} else {
return `\nexport interface I${componentName}Props {\n${interfaceProps.join('\n')}\n}`;
}
} else {
return null;
}
};
const getEventList = (vueComponentString) => {
const regex = new RegExp(/\$emit\(["']([A-Za-z0-9-:]+)["'],?/, 'g');
let match;
const events = [];
while (match = regex.exec(vueComponentString)) {
if (events.indexOf(match[1]) === -1) events.push(match[1]);
}
return events;
};
const getComponentToTagMappings = () => {
const framework7Vue = readFileSync('./node_modules/framework7-vue/src/framework7-vue.js', 'utf8');
const framework7VueString = stringify(framework7Vue);
const regex = new RegExp(/["'](f7-[A-Za-z0-9-]+)["']\s*\:\s*([A-Za-z0-9]+),/, 'g');
let match;
const tagToComponentMap = {};
const componentToTagMap = {};
while (match = regex.exec(framework7VueString)) {
const tagName = match[1];
const componentName = match[2];
tagToComponentMap[tagName] = componentName;
componentToTagMap[componentName] = tagName;
}
return {
componentToTagMap,
tagToComponentMap
};
};
const getInstantiatedComponentList = (vueComponentString, tagToComponentMap) => {
return Object.keys(tagToComponentMap).reduce((componentList, nextTag) => {
const regex = new RegExp(`["']${nextTag}["']`, 'g');
if (regex.test(vueComponentString) && componentList.indexOf(tagToComponentMap[nextTag]) === -1) {
return componentList.concat([tagToComponentMap[nextTag]]);
} else {
return componentList;
}
}, []);
};
const getSlotList = (vueComponentString) => {
const regexDict = new RegExp(/\$slots\[\s*["']([A-Za-z0-9-:]+)["']\s*]/, 'g');
const regexDot = new RegExp(/\$slots\.([A-Za-z0-9-:]+)/, 'g');
const regexT = new RegExp(/_vm\._t\(["']([A-Za-z0-9-:]+)["']\)/, 'g');
let match;
const slots = [];
[regexDict, regexDot, regexT].forEach(regex => {
while (match = regex.exec(vueComponentString)) {
if (slots.indexOf(match[1]) === -1 && match[1] !== 'default') slots.push(match[1]);
}
});
return slots;
};
const getComponentMixinMap = () => {
let framework7Vue = readFileSync('./framework7-vue/framework7-vue.js', 'utf8');
const regex = new RegExp(/mixins:\s*\[([A-Za-z0-9]+)/, 'g');
let match;
framework7Vue = framework7Vue.replace(regex, (mixinStatement, mixinName) => {
return mixinStatement.replace(mixinName, `'${mixinName}'`);
});
let transpiledFramework7Vue = babel.transform(framework7Vue, {
presets: ['es2015']
}).code;
const framework7VueExports = new Function('exports, console', 'try {\n' + transpiledFramework7Vue + '\n} catch (err) { console.log("An error occurred: "); console.error(err);}; return exports;')({}, console);
return Object.keys(framework7VueExports).reduce((componentMixinMap, nextExportName) => {
const exportedComponent = framework7VueExports[nextExportName];
if (exportedComponent.mixins) {
return {
...componentMixinMap,
[nextExportName]: exportedComponent.mixins[0]
};
} else {
return componentMixinMap;
}
}, {});
};
const generateReactifyF7VueCall = (
vueComponent,
vueComponentName,
vueComponentString,
reactComponentName,
componentToTagMappings,
componentMixinMap,
overrides
) => {
const reactifyF7VueArgs = [];
let eventList;
let instantiatedComponentList;
let slotList;
const imports = [
`import * as React from 'react'`,
generateImportString('reactifyF7Vue', '../src/utils/ReactifyF7Vue'),
generateImportString(vueComponentName, '../framework7-vue/framework7-vue')
];
reactifyF7VueArgs.push(`component: ${vueComponentName}`);
reactifyF7VueArgs.push(`name: '${reactComponentName}'`);
reactifyF7VueArgs.push(`tag: '${componentToTagMappings.componentToTagMap[reactComponentName]}'`);
const componentOverrides = overrides && overrides[reactComponentName];
if (componentOverrides && componentOverrides.events) {
eventList = componentOverrides.events;
} else {
eventList = getEventList(vueComponentString);
}
if (eventList.length) reactifyF7VueArgs.push(`events: [\n\t\t'${eventList.join('\',\n\t\t\'')}'\n\t]`);
if (componentOverrides && componentOverrides.instantiatedComponents) {
instantiatedComponentList = componentOverrides.instantiatedComponents.instantiatedComponents;
} else {
instantiatedComponentList = getInstantiatedComponentList(vueComponentString, componentToTagMappings.tagToComponentMap);
}
if (instantiatedComponentList.length) reactifyF7VueArgs.push(`instantiatedComponents: [\n\t\t${instantiatedComponentList.join(',\n\t\t')}\n\t]`);
if (componentOverrides && componentOverrides.slots) {
slotList = componentOverrides.slots;
} else {
slotList = getSlotList(vueComponentString);
}
if (slotList.length) reactifyF7VueArgs.push(`slots: [\n\t\t'${slotList.join('\',\n\t\t\'')}'\n\t]`);
imports.push(...instantiatedComponentList.map(componentName => {
return generateImportString(componentName, `./${componentName}`)
}));
const mixin = componentMixinMap[vueComponentName];
if (mixin) {
imports.push(generateImportString(`Vue${mixin}`, '../framework7-vue/framework7-vue'));
imports.push(generateImportString(`I${mixin}Props`, `./${mixin}`));
reactifyF7VueArgs.push(`mixin: Vue${mixin}`);
}
const typeScriptInterface = generateTypeScriptInterfaceFromProps(
vueComponent.props,
reactComponentName,
eventList,
slotList,
mixin
);
const propInterfaceName = (typeScriptInterface) ? `I${reactComponentName}Props` : 'void';
const reactifyVueCall = `\nexport const ${reactComponentName} = reactifyF7Vue<${propInterfaceName}>({\n\t${reactifyF7VueArgs.join(',\n\t')}\n});`;
const componentCode = [
imports.join('\n'),
typeScriptInterface
];
if (vueComponentName.indexOf('Mixin') === -1) componentCode.push(reactifyVueCall);
return componentCode.join('\n');
};
const generateIndexTsFile = (vueComponents, excludes) => {
const importedFiles = [];
const exportedModules = [];
importedFiles.push(generateImportString('Framework7App', '../src/components/Framework7App'));
exportedModules.push('Framework7App');
importedFiles.push(generateImportString('Framework7', '../framework7/Framework7'));
exportedModules.push('Framework7');
Object.keys(vueComponents).forEach(vueComponentName => {
if (vueComponentName.indexOf('Mixin') === -1) {
const reactComponentName = vueComponentName.replace('Vue', '');
if (excludes && excludes.indexOf(reactComponentName) !== -1) {
importedFiles.push(generateImportString(reactComponentName, `../src/components/${reactComponentName}`));
} else {
importedFiles.push(generateImportString(reactComponentName, `./${reactComponentName}`));
}
exportedModules.push(reactComponentName);
}
});
const indexTsFile = `${importedFiles.join('\n')}\n\nexport {\n\t${exportedModules.join(',\n\t')}\n}`;
const outPath = './framework7-react/index.ts';
writeFileSync(outPath, indexTsFile);
}
export const generateReactComponents = (args) => {
const componentFile = [];
const componentMixinMap = getComponentMixinMap();
const vueComponents = require('./framework7-vue/framework7-vue');
Object.keys(vueComponents).forEach(vueComponentName => {
const reactComponentName = vueComponentName.replace('Vue', '');
const vueComponent = vueComponents[vueComponentName]
const vueComponentString = stringify(vueComponent).split(`\\"`).join('"');
const componentToTagMappings = getComponentToTagMappings();
if (!args || !args.exclude || args.exclude.indexOf(reactComponentName) === -1) {
const reactifyF7VueCall = generateReactifyF7VueCall(
vueComponent,
vueComponentName,
vueComponentString,
reactComponentName,
componentToTagMappings,
componentMixinMap,
args && args.overrides
);
const outPath = `./framework7-react/${reactComponentName}.ts`;
ensureDirectoryExistence(outPath);
writeFileSync(outPath, reactifyF7VueCall);
}
});
generateIndexTsFile(vueComponents, args && args.exclude);
};