import path from 'path'; import fs from 'fs'; import ejs from 'ejs'; export class CodeGen{ templateDir: string; outputDir: string; constructor(){ this.templateDir = path.join(__dirname, '../../template') this.outputDir = process.cwd(); } run(options: any){ this.templateDir = path.join(this.templateDir, options.templateName) this.outputDir = path.join(this.outputDir, options.options.name); if(!fs.existsSync(this.outputDir)){ fs.mkdirSync(this.outputDir); } let currentDir = this.templateDir options = options.options; this.outputTemplate(currentDir, options); } outputTemplate(currentDir: string, options: any){ let files = fs.readdirSync(currentDir); files.map(item=>{ let filePath = path.join(currentDir, item); if(fs.statSync(filePath).isFile()){ let buffer = fs.readFileSync(filePath); let result = ejs.render(buffer.toString(), options) let relativePath = path.relative(this.templateDir, filePath); let outputPath = ejs.render(path.join(this.outputDir, relativePath), options); fs.writeFileSync(outputPath, result); }else if(fs.statSync(filePath).isDirectory()){ let relativePath = path.relative(this.templateDir, filePath); let outputPath = ejs.render(path.join(this.outputDir, relativePath), options); fs.mkdirSync(outputPath); this.outputTemplate(filePath, options); } }) } }