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 refExportList = [];
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') {
return appendTabTmpl(prop);
} else if (prop.component == 'TableLayout') {
return appendTableLayoutTmpl(prop);
} else if (prop.component == 'Card') {
return 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);`);
refExportList.push(`activeKey${tabCount}`);
tmpl += `\n`;
prop.children.forEach((tabChild, index) => {
tmpl += `\n`;
tabChild.list.forEach((listChild) => {
appendTmpl(listChild);
});
tmpl += `\n`;
});
tmpl += `\n`;
if(components.indexOf('TabPane') < 0){
components.push('TabPane');
components.push('Tabs');
importList.push(`import { TabPane, Tabs } from 'ant-design-vue';`);
}
}
function appendTableLayoutTmpl(prop) {
if (components.indexOf('TableLayoutPreview') < 0) {
components.push('TableLayoutPreview');
importList.push(`import TableLayoutPreview from '/@/components/Form/src/components/TableLayoutPreview.vue';`);
}
let schema = `schemaMap['${prop.key}']`;
tmpl += `\n`;
tmpl += ` \n`;
tmpl += ` \n`;
tmpl += `
\n`;
tmpl += `
`;
tmpl += `
\n`;
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`;
if (components.indexOf('CollapseContainer') < 0) {
components.push('CollapseContainer');
importList.push(`import { CollapseContainer } from '/@/components/Container';`);
}
}
const fullVue = `
`;
fs.writeFile('./CustomDevForm.vue', fullVue, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});