#!/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'); const fse = require('fs-extra'); var os = require('os'); const utils = require('./utils'); // 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, './hro_template', { filter: (src, dest) => { console.log(src); if (src.indexOf('node_modules') != -1) return false; else return true } }); shell.cd('./hro_template'); shell.exec('yarn install'); shell.exec('workai start'); } }); /* *开发模式启动项目 */ program .command('start [projectPath]') .alias('dev') .description('开发模式启动') .action(option => { _checkEnv().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("当前所在目录不是项目目录"); } }); }); function _checkEnv() { return utils.getPaths(["configs/webpack.**", "!node_modules"]).then(paths => { return paths.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))); }