UNPKG

hm-react-cli

Version:

Create a Huimei React project by module

112 lines (96 loc) 4.31 kB
const path = require('path'); const fs = require('fs'); const inquirer = require('inquirer'); const log = require('../utils/log'); const ejs = require('ejs'); const prettier = require('prettier'); module.exports = function create(appName) { const projectList = ['dunshan', 'mayson', '独立项目']; inquirer .prompt([ { name: 'project', type: 'list', message: '在什么项目中使用 hm-react 服务?', choices: projectList, default: 'dunshan' } ]) .then((answer) => { let destDir; if (answer.project === 'mayson') return log('yellow', '暂未支持 mayson 项目'); if (answer.project === '独立项目') destDir = process.cwd(); if (answer.project == 'dunshan') destDir = getDescPath(); if(!destDir) return; const tmplDir = path.join(__dirname, '../', 'template/app'); isExist(path.resolve(destDir, appName)); updateConfigFile(appName); copyFile(tmplDir, path.resolve(destDir, appName)); }); }; function getDescPath() { let processPath = process.cwd(); const curPathGraphList = processPath.split('/').slice(1); const isLastIndex = (index) => index == curPathGraphList.length - 1; const curPathGraph = curPathGraphList.reduce((pre, cur, index) => ((pre[cur] = index), pre), {}); const isTaishan = curPathGraph['taishan'] || curPathGraph['taishan_new']; if (isTaishan && isLastIndex(curPathGraph['modules'])) return processPath; if (!isTaishan && isLastIndex(curPathGraph['WebRoot'])) return processPath; log('yellow', isTaishan ? '请进入 WebRoot/app/modules 目录创建模板' : '请进入WebRoot目录创建模板'); } const isExist = (path) => { // 判断文件夹是否存在, 不存在创建一个 if (!fs.existsSync(path)) { fs.mkdirSync(path); } }; const copyFile = (sourcePath, targetPath) => { const sourceFile = fs.readdirSync(sourcePath, { withFileTypes: true }); sourceFile.forEach((file) => { const newSourcePath = path.resolve(sourcePath, file.name); const newTargetPath = path.resolve(targetPath, file.name); if (file.isDirectory()) { // 目录 isExist(newTargetPath); copyFile(newSourcePath, newTargetPath); } else { // 文件 let data = {}; ejs.renderFile(newSourcePath, data, function (err, str) { // str => 输出渲染后的 HTML 字符串 fs.writeFileSync(newTargetPath, str); }); } }); }; const updateConfigFile = (appName) => { let processPath = process.cwd(); let projectBasePath = processPath.replace(/(.*)WebRoot(.*)/, '$1'); let configFilePath = path.resolve(__dirname, '../', 'template/config/hm.config.js'); let exist = (p) => fs.existsSync(path.join(projectBasePath, p)); const format = (str) => prettier.format(str, { semi: true, singleQuote: true, parser: 'babel', tabWidth: 4 }); if (exist('hm.config.js')) { //已经存在hm.config.js文件、则在文件的基础上添加新模块 configFilePath = path.join(projectBasePath, 'hm.config.js'); const config = require(configFilePath); const isRepeat = config.pages.some((page) => page === appName); if (isRepeat) throw new Error('该项目已存在该 ' + appName + ' Page'); config.pages.push(appName); fs.writeFileSync(configFilePath, format(`module.exports = ${JSON.stringify(config, null, 4)};`)); } else { const config = require(configFilePath); config.pages.push(appName); fs.writeFileSync( path.join(projectBasePath, 'hm.config.js'), format(`module.exports = ${JSON.stringify(config, null, 4)};`) ); } const createConfigFile = (fileName) => { const tmplDir = path.join(__dirname, '../', 'template/config', fileName); if (!exist(fileName)) { fs.writeFileSync(path.join(projectBasePath, fileName), JSON.stringify(require(tmplDir),null,4)); } }; createConfigFile('prod.env.json'); createConfigFile('dev.env.json'); };