#!/usr/bin/env node /** * Module dependencies. */ var path = require('path'); var commander = require('commander'); var inquirer = require('inquirer'); var chalk = require('chalk'); var shell = require("shelljs"); var parseArgs = require('minimist'); var fse = require('fs-extra'); var os = require('os'); var utils = require('./utils'); var bundleDll = require('./bundle-dll'); var bundle = require('./bundle'); var async = require('asyncawait/async'); var await = require('asyncawait/await'); var resolveCwd = require('import-cwd'); require("@babel/register"); // import config from '../workai.tools.config'; var program = new commander.Command(); // const promptList = [{ // type: 'input', // message: '设置一个用户名:', // name: 'name', // default: "test_user" // 默认值 // }, { // type: 'input', // message: '请输入手机号:', // name: 'phone', // validate: function (val) { // if (val.match(/\d{11}/g)) { // 校验位数 // return val; // } // return "请输入11位数字"; // } // }]; // inquirer.prompt(promptList).then(answers => { // console.log(answers); // 返回的结果 // }); // program // .version('0.0.1', '-v, --version') // .option('-p, --peppers', 'Add peppers') // .option('-P, --pineapple', 'Add pineapple') // .option('-b, --bbq-sauce', 'Add bbq sauce') // .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble') // .alias('m') // .description('创建新的模块') // .action(function (option) { // console.log('Hello World') // //为什么是Hello World 给你个眼神,自己去体会... // }) // .on('--help', function () { // console.log(' Examples:') // console.log('') // console.log('$ app module moduleName') // console.log('$ app m moduleName') // }) // .parse(process.argv); program .version('0.0.1') .usage('例子') .description('this is a lizi of commander'); // console.log('you ordered a pizza with:'); // if (program.peppers) console.log(' - peppers'); // if (program.pineapple) console.log(' - pineapple'); // if (program.bbqSauce) console.log(' - bbq'); // console.log(' - %s cheese', program.cheese); program .command('hello [st]') .action(function (st, value) { hello(st, value); }) function hello(val, o) { console.log(val); console.log(1); console.log(o) } /*构建生命周期 * init 初始化项目 */ program .command('init [templateName]') .alias('i') .description('初始化项目') .action(option => { // // 该对象用于存储所有与用户交互的数据 // let config = { // // 假设我们需要用户自定义项目名称 // projectName: null // }; // // 使用chalk打印美化的版本信息 // console.log(chalk.default.bold('hello v1.0.0')); // // 用于存储所有的交互步骤,例如让用户输入项目名称就是其中一个步骤 // let promps = []; // if (config.projectName === null) { // promps.push({ // type: 'input', // name: 'projectName', // message: '请输入项目名称', // validate: input => { // if (!input) { // return '项目名称不能为空'; // } // // 更新对象中属性的数据 // config.projectName = input; // return true; // } // }); // } // // 至此,与用户的所有交互均已完成,answers是收集到的用户所填的所有数据 // // 同时,这也是你开始操作的地方,这个cli工具的核心代码应该从这个地方开始 // inquirer.prompt(promps).then(async (answers) => { // // do something here // console.log("::::::", answers); // }); if (option == 'hro') { var hro_template_path = path.resolve(__dirname, '..', 'templates', 'hro_template'); console.log(hro_template_path); fse.copySync(hro_template_path, './', { filter: (src, dest) => { console.log(src); if (src.indexOf('node_modules') != -1) return false; else return true } }); // shell.cd('./'); shell.exec('yarn install'); shell.exec('workai start'); } }); /* * deploy 构建项目 */ program .command('deploy [dll|all]') .alias('d') .description('构建项目') .action(option => { var opject_path = process.cwd(); process.chdir(opject_path);//跳转到项目目录 console.log(path.resolve(__dirname), process.cwd(), path.resolve(process.cwd(), 'configs')); if (option == 'dll') { var hro_template_path = path.resolve(__dirname, '..', 'templates', 'hro_template'); _checkEnv(["configs/webpack.dll.**", "!node_modules"]).then(flag => { if (flag) { var webpackDllConfig = resolveCwd('./configs/webpack.dll.config'); bundleDll(webpackDllConfig).then(data => { console.log("dll file is ok!"); }); // shell.exec('npx babel-node server/development.js'); } else { console.log("当前所在目录不是项目目录"); } }); } else if (option == 'all') { var hro_template_path = path.resolve(__dirname, '..', 'templates', 'hro_template'); _checkEnv(["configs/webpack.production.**", "configs/webpack.dll.**", "!node_modules"]).then(flag => { if (flag) { var webpackDllConfig = resolveCwd('./configs/webpack.dll.config'); var webpackConfig = resolveCwd('./configs/webpack.production.config'); console.log(webpackDllConfig, webpackConfig); // shell.exec('npx babel-node server/development.js'); } else { console.log("当前所在目录不是项目目录"); } }); } else { _checkEnv(["configs/webpack.production.**", "!node_modules"]).then(flag => { if (flag) { var webpackConfig = resolveCwd('./configs/webpack.production.config'); bundle(webpackConfig).then(data => { console.log("bundle file is ok!"); }); // shell.exec('npx babel-node server/development.js'); } else { console.log("当前所在目录不是项目目录"); } }); } }); /* *生产模式启动项目 */ program .command('service [projectPath]') .alias('s') .description('生产模式启动') .action(option => { shell.exec('npx babel-node server/production.js'); }); /* *开发模式启动项目 */ program .command('start [projectPath]') .alias('dev') .description('开发模式启动') .action(option => { _checkEnv(["configs/webpack.develop.**", "!node_modules"]).then(flag => { console.log("222:::", flag, process.cwd(), path.resolve(__dirname), option); if (flag) { shell.exec('npx babel-node server/development.js'); } else { console.log("当前所在目录不是项目目录"); } }); }); /*环境检测 *是否是项目环境 *paths = [] 例:paths = ["configs/webpack.**", "!node_modules"] */ function _checkEnv(paths) { return utils.getPaths(paths).then(matching => { return matching.length > 0; }); } program .option('-s --save [value]', '保存'); program.parse(process.argv); console.log(os.type(), os.platform()); // utils.getPaths(["**", "!node_modules"]).then(paths => { // // console.log("##", paths); // }); if (program.save) { console.log(parseArgs(process.argv.slice(2))); }