epmp-cli
Version:
A simple init for epm scaffolding projects.
103 lines (88 loc) • 3.28 kB
JavaScript
/**
* EPMP 微服务前端工程最佳实践页面生成
* @author zhaoyuu(zhaoyuu@yonyou.com)
* @date 2019-05-17 10:44:06
*/
const chalk = require('chalk');
const path = require('path');
const fse = require('fs-extra');
const inquirer = require('inquirer');
const utils = require('./utils');
module.exports = async (app = 'app') => {
// 连接配置文件
let ucfFilePath = path.resolve('.', 'epmp.config.js');
// 目标路径
let ucfApps = path.resolve('.', 'src/epmp-apps');
// 模板路径
let ucfPathTmp = path.resolve(__dirname, './templates');
// 人机交互选择的模板名称
let ucfSelectTempArr = ['APP', 'SPA'];
// 人机交互选择的模块名称
// let ucfSelectModule = ['bcs', 'dcp', 'demo', 'epm_p', 'outsys', 'bp'];
let ucfSelectModule = utils.getModules(ucfApps + '/*');
// 生成模块参数
let ucfParam = {
name: '', // 微应用名字
mode: '', // 选择哪种模板
i18n: false, // 是否需要多语
};
// TO DO : 1. 检测epmp.config.js是否存在,来判断当前目录是否正确
let hasUcfFile = await fse.pathExists(ucfFilePath);
if (!hasUcfFile) {
//TO DO : 2.2 找不到配置文件,说明运行目录不正确给出提示
console.log(chalk.red.bold('😫 Error failed to find epmp.config configuration file'));
process.exit(1);
}
//TO DO : 2.1 确定正确目录下,开始执行下一步模块选择操作
// 所有new操作主逻辑
switch (app) {
case 'app':
console.log(chalk.cyan('🎁 Create App startup...'));
//TO DO : 3 展示人机交互,输入工程模块名,选择模板
// 输入模块名
let inquirerTempName = await inquirer.prompt([
{
type: 'input',
name: 'inputName',
message: '请输入应用名称:',
default: function () {
return 'app-demo';
},
},
]);
ucfParam.name = inquirerTempName.inputName;
// 选择哪种方式的页面: 暂时默认APP
// let inquirerTempModule = await inquirer.prompt([{
// type: 'list',
// name: 'selectTemplates',
// message: 'EPMP Templates Please Select:',
// choices: ucfSelectTempArr
// }]);
// 选择目标模块
let chooseModule = await inquirer.prompt([
{
type: 'list',
name: 'selectModule',
message: '请选择应用所属的模块:',
choices: ucfSelectModule,
},
]);
// 如果目录选择的是'demo',则从templates/DEMO目录下读取将要创建的模板
if (chooseModule.selectModule === 'demo') {
ucfParam.mode = 'DEMO'; //inquirerTempModule.selectTemplates;
} else {
ucfParam.mode = 'APP'; //inquirerTempModule.selectTemplates;
}
ucfParam.targetModule = chooseModule.selectModule; //inquirerTempModule.selectTemplates;
ucfParam.i18n = false;
// console.log(ucfParam);
// process.exit(0);
ucfApps = path.resolve(ucfApps, ucfParam.targetModule);
// 复制微应用模板到客户指定位置
await fse.copy(path.resolve(ucfPathTmp, ucfParam.mode), path.resolve(ucfApps, ucfParam.name));
console.log(chalk.green(`🤗 APP Creation Successfully to \n💪 ${path.resolve(ucfApps, ucfParam.name)}`));
break;
default:
break;
}
};