import { AMDataGenerator } from "./datagenerator"; import { AMDataTypeGenerator } from "./datatypegenerator"; import { ExcelModelGenerator } from "./excelmodelgenerator"; import { AMFileMover } from "./filemover"; import { AMIndexGenerator } from "./indexgenerator"; import { AMMetaGenerator } from "./metagenerator"; import { Model2Entity } from "./model2entity"; import { Model2Object } from "./model2object"; import { Model2Sql } from "./model2sql"; import { AMModelManager } from "./modelmanager"; import { AMPageGenerator } from "./pagegenerator"; import { AMServerGenerator } from "./servergenerator"; import { AmUtil } from "./util"; /** * 生成工具 */ export class CodeGenerator{ public static gen(configFile:string){ const fsMdl = require('fs'); const pathMdl = require('path'); let data = fsMdl.readFileSync(configFile, 'utf8'); if (!data) { throw new Error('配置文件不存在'); } let configObj: any; try { configObj = JSON.parse(data); } catch (e) { throw new Error('配置文件格式错误'); } console.log('代码生成中...'); const dstPath = pathMdl.resolve(configObj.dst || './'); //TODO 发布时 const filePath = '../../'; //测试时 // const filePath = '../../../../' //清空目录 // AmUtil.removeFolder(dstPath); const mdlPath = pathMdl.resolve(dstPath,'models'); let sqlPath = pathMdl.resolve(dstPath,'model.sql'); //为excel类型 if(configObj.excel){ //生成模型 new ExcelModelGenerator().gen(pathMdl.resolve(configObj.excel),mdlPath); //生产sql new Model2Sql().genSql(mdlPath,sqlPath); //生成实体 new Model2Entity().genEntity(mdlPath,pathMdl.resolve(dstPath,'server','module','dao','entity','app'),['MdlAm','MdlNf']); //合并sql data = fsMdl.readFileSync(pathMdl.resolve(__dirname,filePath,'files/sql/base.sql'), 'utf8'); const data1 = fsMdl.readFileSync(sqlPath,'utf8'); //写基础sql fsMdl.writeFileSync(sqlPath,data,{encoding:'utf8'}); fsMdl.appendFileSync(sqlPath,"/*amcode 创建的sql*/",{encoding:'utf8'}); //写生成sql fsMdl.appendFileSync(sqlPath,data1,{encoding:'utf8'}); //拷贝基础model AmUtil.copyDir(pathMdl.resolve(__dirname,filePath,'files/model/base'),mdlPath); } //获取model集合 const models = Model2Object.gen(mdlPath); // 初始化model工厂 AMModelManager.init(models); // 生成页面元数据 const metaPath = pathMdl.resolve(dstPath,'meta'); new AMMetaGenerator(metaPath).genMeta(); const dataPath = pathMdl.resolve(dstPath,'datatype'); // 数据类型相关页面(导入,查询,分层) new AMDataTypeGenerator(dataPath).genData(); // 创建sql new AMDataGenerator(dstPath); // // page 路径 const pagePath = pathMdl.resolve(dstPath,'pages'); // // 拷贝页面代码 AmUtil.copyDir(pathMdl.resolve(__dirname,filePath,'files/pages'),pagePath); // 创建页面代码 new AMPageGenerator(pathMdl.resolve(pagePath,'module'),metaPath).genCode(); // 拷贝页面代码 AmUtil.copyDir(pathMdl.resolve(__dirname,filePath,'files/pages'),pagePath); // 首页和header new AMIndexGenerator(pagePath,configObj.title).genCode(); // 创建服务端代码 const serverPath = pathMdl.resolve(dstPath,'server') new AMServerGenerator(serverPath).genCode(); //拷贝server AmUtil.copyDir(pathMdl.resolve(__dirname,filePath,'files/server'),serverPath); // //文件移动 new AMFileMover(dstPath,pagePath,serverPath); //写sql数据 fsMdl.appendFileSync(sqlPath,"/*amcode 创建的sql*/",{encoding:'utf8'}); const data1 = fsMdl.readFileSync(pathMdl.resolve(dstPath,'data.sql'),'utf8'); fsMdl.appendFileSync(sqlPath,data1,{encoding:'utf8'}); //复制node_modules目录 AmUtil.copyDir(pathMdl.resolve(__dirname,filePath,'files/node_modules'),pathMdl.resolve(dstPath,'node_modules')); fsMdl.copyFileSync(pathMdl.resolve(__dirname,filePath,'files/package.json'),pathMdl.resolve(dstPath,'package.json')); fsMdl.copyFileSync(pathMdl.resolve(__dirname,filePath,'files/tsconfig.json'),pathMdl.resolve(dstPath,'tsconfig.json')); //生成relaen.json this.genRelaen(configObj.db,serverPath); //生成app.js this.genAppjs(configObj.port,dstPath); //生成tsconfig文件 // this.genTsConfig(dstPath); // //生产package.json文件 // this.genPackage(dstPath); //删除多余文件 AmUtil.removeFolder('meta'); AmUtil.removeFolder('datatype'); AmUtil.removeFolder('models'); fsMdl.unlinkSync('data.sql'); const {exec} = require('child_process'); exec('tsc',(err,stdout,stderr)=>{ if(err){ console.error(err); }else{ console.log('代码生成结束!'); console.log('请按以下步骤操作:'); console.log(`1.把model.sql导入到数据库"${configObj.db.database}"中。`); console.log('2.在命令行输入"node app.js"并回车。'); } }); } /** * 生成relaen配置文件 * @param cfg * @param serverPath */ private static genRelaen(cfg,serverPath){ const s = AmUtil.formatJson(`{ "package": "noomi-relaen", "options": { "dialect": "${cfg.dialect}", "host": "${cfg.host}", "port": ${cfg.port}, "username": "${cfg.user}", "password": "${cfg.password}", "database": "${cfg.database}", "pool": { "min": 0, "max": 10 }, "entities": [ "/dist/server/module/dao/entity/**/*.js" ], "cache":true, "debug":false, "fullTableOperation":true }, "transaction": { "expressions": [] } }`); //写文件 require('fs').writeFileSync(require('path').resolve(serverPath,'config','relaen.json'),s,{encoding:'utf8'}); } /** * 生产app.js * @param port 启动端口 * @param dstPath 目标路径 */ private static genAppjs(port,dstPath){ const s = AmUtil.formatTs(` "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const noomi_1 = require("noomi"); noomi_1.noomi(${port}, 'server/config'); `) //写文件 require('fs').writeFileSync(require('path').resolve(dstPath,'app.js'),s,{encoding:'utf8'}); } /** * 写tsconfig文件 */ // private static genTsConfig(dstPath){ // const s = AmUtil.formatJson(` // { // "compilerOptions": { // "module":"commonjs", // "target": "es2017", // "sourceMap": true, // "strict":false, // "experimentalDecorators": true, // "emitDecoratorMetadata": true, // "rootDir":"./", // "outDir": "./dist" // }, // "include":["server/**/*.ts"] // } // `) // //写文件 // require('fs').writeFileSync(require('path').resolve(dstPath,'tsconfig.json'),s,{encoding:'utf8'}); // } /** * 写package.json文件 */ // private static genPackage(dstPath){ // const s = AmUtil.formatJson(` // { // "name": "amcode", // "version": "0.0.1", // "description": "", // "main": "index.js", // "scripts": { // "build": "rm -rf dist & tsc" // }, // "author": "amcode group", // "license": "MIT", // "dependencies": { // "adm-zip": "^0.5.9", // "commander": "^11.0.0", // "echarts": "^5.3.3", // "json5": "^2.2.3", // "mysql": "^2.18.1", // "node-xlsx": "^0.21.0", // "noomi-relaen": "^0.0.2-beta2", // "noomiflow": "^0.0.9", // "prettier": "^2.7.1", // "@types/node": "^25.6.0", // "log4js": "^6.5.2", // "nodom3": "^0.2.0-beta20", // "noomi": "^1.0.0-beta17", // "relaen": "^0.3.2-beta6" // } // } // `) // //写文件 // require('fs').writeFileSync(require('path').resolve(dstPath,'package.json'),s,{encoding:'utf8'}); // } }