UNPKG

custom-app

Version:

ITIMS��Ʒ�鿪��ר��React���,�Dz��ý��ּ�dhcc-app���������

269 lines (244 loc) 9.5 kB
#!/usr/bin/env node const chalk = require('chalk'); const commander = require('commander'); const path = require('path') const packageJson = require('../package.json'); let projectName; let isInitEnv = false; const program = new commander.Command("dhcc") .version(packageJson.version) .usage('<command> [options]') program .command('initenv') .description('初始化全局环境,安装开发环境必要的一些插件,包括源管理器,webpack相关,itims4-2ndpage') .action(() => { try { require('../lib/initEnv')() isInitEnv = true } catch (error) { isInitEnv = false } }) program .command('init [module-name] [app-name]') .description('在当前目录下(第四个参数为可选,默认为当前目录),初始化工作空间,并且工作空间名为[module-name],如果工作空间已经存在,执行此命令为初始化配置配置,工作空间在[app-name]部署时会作为路径的一部分\n包括创建package.json,加载开发环境需要的基本插件,生成webpack的配置文件(可选)\n') .option('-i,--install', '只加载脚本,不生产webpack的配置文件') .option('-c,--config', '只生产webpack的配置文件,不加载脚本') .action((moduleName, appName, cmd) => { if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } if (!appName) { const cwd = process.cwd() const list = cwd.split(path.sep); appName = list[list.length - 1] if (appName == 'reactapp') { appName = '.' } } const options = cleanArgs(cmd) require('../lib/init')(moduleName, appName, options) }) program .command('create <module-type> <app-name>') .description('创建模板,模板类型包括spa和plugin两种') .action((moduleType, name, cmd) => { if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } const options = cleanArgs(cmd) if (moduleType == 'spa') { require('../lib/initSpa')(name, options) } else if (moduleType == 'plugin') { require('../lib/initPlugin')(name, options) } else { console.log(chalk.red("module-type参数值不正确,应为spa或plugin")) return; } }) // program // .command('create plugin <app-name>') // .description('初始化插件模板') // .action((name, cmd) => { // if (!checkPwd()) { // console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) // return; // } // const options = cleanArgs(cmd) // require('../lib/initPlugin')(name, options) // }) program .command('build [app-name]') .description('开发环境下打包项目\n, 目录是可选的,如果不出现,则默认以当前目录为准打包,如果当前目录是工作空间(reactapp),则打包src下所有模块;如果当前目录已经是Src下的某个模块,则仅打包当前模块,此时忽略目录参数') .option('-c,--config [config]', '配置文件路径,默认为顶级目录config/config.dev.conf.js') .option('-h,--hash', '生成的文件是否采用hash') .option('-r,--report', '开启打包优化分析') .action((appName, cmd) => { if (!appName) { const cwd = process.cwd() const list = cwd.split(path.sep); appName = list[list.length - 1] if (appName == 'reactapp') { appName = '.' } } if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } const options = cleanArgs(cmd) require('../lib/build')(appName, options) }) program .command('deploy [app-name]') .description('生产环境下打包项目\n, 目录是可选的,如果不出现,则默认以当前目录为准打包,如果当前目录是工作空间(reactapp),则打包src下所有模块;如果当前目录已经是Src下的某个模块,则仅打包当前模块,此时忽略目录参数') .option('-c,--config [config]', '配置文件路径,默认为顶级目录config/config.prod.conf.js') .option('-h,--hash', '生成的文件是否采用hash') .option('-r,--report', '开启打包优化分析') .action((appName, cmd) => { if (!appName) { const cwd = process.cwd() const list = cwd.split(path.sep); appName = list[list.length - 1] if (appName == 'reactapp') { appName = '.' } } if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } const options = cleanArgs(cmd) require('../lib/prod')(appName, options) }) program .command('publish <app-name>') .description('发布项目到服务器,. 为预留字,表示发布整个reactapp项目') .action((name, cmd) => { if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } const options = cleanArgs(cmd) require('../lib/publish/publish')(name, options) }) // program // .command('path') // .description('修改打包的根路径,路径为相对于项目根目录的相对路径') // .option('-s,--set [path]', '修改打包的根路径,路径为相对于项目根目录的相对路径') // .option('-i,--ignore [path]', '发布到服务器时忽略的路径,比如src/main') // .option('-c,--clear', '清除自定义路径') // .action((cmd) => { // if (!checkPwd()) { // console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) // return; // } // const options = cleanArgs(cmd) // require('../lib/rootpath/rootpath')(options) // }) program .command('login') .description('修改npm远程仓库用户信息') .option('-u,--update', '更新用户信息') .option('-c,--clear', '清除用户信息') .action((cmd) => { if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } const options = cleanArgs(cmd) require('../lib/plugin/rebuild')(options) }) program .command('server') .description('修改远程服务器配置') .option('-u,--update', '更新远程服务器配置') .option('-c,--clear', '清除远程服务器配置') .action((cmd) => { if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } const options = cleanArgs(cmd) require('../lib/server/rebuild')(options) }) program .command('dev-server [app-name]') .description('启动指定模块的本地服务器') .option('-p,--port <port>', '指定服务器端口') .option('-r,--report', '开启打包优化分析') .action((appName, cmd) => { if (!checkPwd()) { console.log(chalk.red("命令执行路径不是合法路径,执行目录应为reactapp或者xxx/reactapp/src/xxx")) return; } let result = false; const cwd = process.cwd() const list = cwd.split(path.sep);//.join('/').lastIndexOf("\/"); //兼容两个平台 并获取最后位置index const _name = list[list.length - 1] if (!appName) { if (_name == "reactapp") { console.log(chalk.red("请指定启动的[app-name]")) return; } appName = _name } const options = cleanArgs(cmd) require('../lib/devServer/init')(appName, options) }) // output help information on unknown commands program .arguments('<command>') .action((cmd) => { program.outputHelp() console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)) console.log() }) // add some useful info on help program.on('--help', () => { console.log() console.log(` Run ${chalk.cyan(`dhcc <command> --help`)} for detailed usage of given command.`) console.log() }) program.commands.forEach(c => c.on('--help', () => console.log())) program.parse(process.argv) if (!process.argv.slice(2).length) { program.outputHelp() } function camelize (str) { return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '') } function checkPwd () { const cwd = process.cwd() const list = cwd.split(path.sep);//.join('/').lastIndexOf("\/"); //兼容两个平台 并获取最后位置index const appname = list[list.length - 1]//cwd.substring(index + 1, cwd.length); if (appname != "reactapp") { const len = list.length; if (len >= 3) { if (list[len - 2] == "src" && list[len - 3] == "reactapp") { return true; } } return false; } else { return true; } return true; } // commander passes the Command object itself as options, // extract only actual options into a fresh object. function cleanArgs (cmd) { const args = {} if (cmd.options) cmd.options.forEach(o => { const key = camelize(o.long.replace(/^--/, '')) // if an option is not present and Command has a method with the same name // it should not be copied if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') { args[key] = cmd[key] } }) return args }