const fs = require('fs'); const path = require('path'); const filePath = path.join(process.cwd(), 'config.ts'); const argv = process.argv; const fixResponsive = argv.indexOf('fix-responsive') > 0; const templates = require('./templates'); if (fs.existsSync(filePath)) { console.log('Reading ' + filePath); const file = fs.readFileSync(filePath, 'utf-8'); const fileArr = file.split('\n'); const confArr = []; let parseStart = false; fileArr.forEach((row) => { if (!parseStart && row.indexOf('export const formProps: FormProps =') >= 0) { parseStart = true; confArr.push('{'); return; } if (parseStart) { if (row.indexOf('};') === 0) { confArr.push('}'); parseStart = false; return; } confArr.push(row); } }); console.log('total rows: ' + confArr.length); formProps = eval('(' + confArr.join('\n') + ')'); } else { process.exit(0); } let tmpl = ''; let tabCount = 0; let tableCount = 0; const refList = []; const importList = []; const components = []; // 用于将config的表单格式展开成字段,以便二开 formProps.schemas.forEach((prop) => { appendTmpl(prop); }); function appendTmpl(prop) { let schema = `schemaMap['${prop.key}']`; // 栅格布局 tmpl += `\n`; if (prop.component == 'Grid') { prop.children.forEach((child) => { child.list.forEach((listChild) => { appendTmpl(listChild); }); }); return; } else if (prop.component == 'Tab') { appendTabTmpl(prop); } else if (prop.component == 'TableLayout') { appendTableLayoutTmpl(prop); } else if (prop.component == 'Card') { appendCardTmpl(prop); } else if (prop.component == 'SubForm') { return appendSubFormTmpl(prop); } tmpl += ` `; } function appendSubFormTmpl(prop){ let schema = `schemaMap['${prop.key}']`; tableCount += 1; createSubFormFile(); tmpl += ` ` importList.push(`import CustomDevTableItem${tableCount} from './CustomDevTableItem${tableCount}.vue';`); components.push(`CustomDevTableItem${tableCount}`); } function createSubFormFile(){ const subFormStr = templates.subFormTmpl; fs.writeFile(`./CustomDevSubForm${tableCount}.vue`, subFormStr, (err) => { if (err) throw err; console.log(`Write SubForm CustomDevSubForm${tabCount}.vue success`); }); const formItemStr = templates.subFormItem.replace('[count]', tableCount); fs.writeFile(`./CustomDevTableItem${tableCount}.vue`, formItemStr, (err) => { if (err) throw err; console.log(`Write FormItem CustomDevTableItem${tabCount}.vue success`); }); } function appendTabTmpl(prop) { let schema = `schemaMap['${prop.key}']`; tabCount += 1; refList.push(`const activeKey${tabCount} = ref(0);`); tmpl += `\n`; prop.children.forEach((tabChild) => { tmpl += `\n`; tabChild.list.forEach((listChild) => { appendTmpl(listChild); }); tmpl += `\n`; }); tmpl += `\n`; } function appendTableLayoutTmpl(prop) { let schema = `schemaMap['${prop.key}']`; tmpl += `\n`; tmpl += ` \n`; tmpl += `\n`; } function appendCardTmpl(prop) { let schema = `schemaMap['${prop.key}']`; tmpl += `\n`; prop.children[0].list.forEach((lChild) => { appendTmpl(lChild); }); tmpl += `\n`; } const fullVue = ` `; fs.writeFile('./CustomDevForm.vue', fullVue, (err) => { if (err) throw err; console.log('The file has been saved!'); });