| 10 | } |
| 11 | |
| 12 | export function getFileCodeTree(globalObj: GlobalJSON, options: CodeOption = { |
| 13 | langs: 'react' |
| 14 | }) { |
| 15 | const MAP = cloneDeep(LANG_MAPS[options.langs]) |
| 16 | const { COMPONENTS_TREE, FILE_TREE, IMPORT_TREE } = MAP |
| 17 | let compStrs = '' |
| 18 | if (globalObj.content.find((item: CompUnion) => item == null)) { |
| 19 | throw new Error("内容存在空数据,无法出码") |
| 20 | } |
| 21 | const len = globalObj.content.length |
| 22 | for (let i = 0; i < len; i++) { |
| 23 | const comp = globalObj.content[i] |
| 24 | let str = COMPONENTS_TREE[comp.componentName] |
| 25 | |
| 26 | //分组件处理 |
| 27 | if (comp.componentName === 'Button') { |
| 28 | let suffix = i === len - 1 ? '' : '\n ' |
| 29 | let res = str.replace(/{{children}}/ig, comp.data.children).replace(/{{id}}/ig, '"' + comp.id + '"') + suffix |
| 30 | compStrs += res |
| 31 | } |
| 32 | |
| 33 | if (comp.componentName === 'Input') { |
| 34 | let suffix = i === len - 1 ? '' : '\n ' |
| 35 | let res = str.replace(/{{children}}/ig, comp.data.placeholder).replace(/{{id}}/ig, '"' + comp.id + '"') + suffix |
| 36 | compStrs += res |
| 37 | } |
| 38 | } |
| 39 | const res = FILE_TREE.root[0]['App.js'].replace(/{{importURL}}/ig, IMPORT_TREE[0]).replace(/{{compContent}}/ig, compStrs) |
| 40 | FILE_TREE.root[0]['App.js'] = res |
| 41 | |
| 42 | return FILE_TREE |
| 43 | } |