UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

145 lines (120 loc) 6.19 kB
#!/usr/bin/env node "use strict";var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));var _commander = _interopRequireDefault(require("commander")); var _didyoumean = _interopRequireDefault(require("didyoumean")); var _index = require("./cli-shared-utils/index.js"); var _index2 = require("./cli-packager/index.js"); var _index3 = require("./cli-server/index.js"); var _index4 = require("./cli-weixin/index.js"); var _enhanceErrorMessages = _interopRequireDefault(require("./cli-shared-utils/lib/enhanceErrorMessages.js")); var _create = _interopRequireDefault(require("./cli-shared-utils/create.js")); _didyoumean["default"].threshold = 0.6; var pkg = require('../package.json'); function suggestCommands(cmd) { var availableCommands = _commander["default"].commands.map(function (cmd) { return cmd._name; }); var suggestion = (0, _didyoumean["default"])(cmd, availableCommands); if (suggestion) { (0, _index.log)(" " + _index.chalk.red("Did you mean ".concat(_index.chalk.yellow(suggestion), "?"))); } }function main() {return _main.apply(this, arguments);}function _main() {_main = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {return _regenerator["default"].wrap(function _callee$(_context) {while (1) switch (_context.prev = _context.next) {case 0: _commander["default"]. version(pkg.version, '-v, --version'). usage('<command> [options]'); _commander["default"]. command('init <app-name>'). description('init a new project'). option('-t, --template <template>', 'Choose a template for this project', 'default'). option('-c, --cwd <dir>', 'input the target dir for initing'). option('-f, --force', 'force overwrite the minigame project if existed'). action(function (name, cmd) { var options = cleanArgs(cmd); (0, _create["default"])(name, options); }); // output help information on unknown commands _commander["default"]. arguments('<command>'). action(function (cmd) { var args = (0, _index.minimist)(process.argv.slice(2), { string: ['buildType'] }); var cleanedArgs = {}; for (var key in args) { var cleanedKey = key.replace(/--/g, ''); cleanedArgs[cleanedKey] = args[key]; } (0, _index.info)("\u5F53\u524D\u7248\u672C\u4E3A:".concat(pkg.version)); if (cmd === 'build') { // debug打包命令 console.log('buildType:', cleanedArgs.buildType); (0, _index.info)('qg build', cleanedArgs); (0, _index2.build)(cleanedArgs); } else if (cmd === 'release') { // release打包命令 console.log('buildType:', cleanedArgs.buildType); (0, _index.info)('qg release', cleanedArgs); (0, _index2.release)(cleanedArgs); } else if (cmd === 'transfer') { // 微小转换命令 console.log('buildType:', cleanedArgs.buildType); (0, _index.info)('qg transfer', cleanedArgs); (0, _index4.transfer)(cleanedArgs); } else if (cmd === 'server') { // 调试服务启动命令 console.log('buildType:', cleanedArgs.buildType); (0, _index.info)('qg server', cleanedArgs); (0, _index3.initServer)(cleanedArgs); } else { _commander["default"].outputHelp(); (0, _index.log)(" " + _index.chalk.red("Unknown command ".concat(_index.chalk.yellow(cmd), "."))); (0, _index.log)(); suggestCommands(cmd); } }); // add some useful info on help _commander["default"].on('--help', function () { (0, _index.log)(); (0, _index.log)(" Run ".concat(_index.chalk.cyan("qg <command> --help"), " for detailed usage of given command.")); (0, _index.log)(); }); _commander["default"].commands.forEach(function (c) {return c.on('--help', function () {return (0, _index.log)();});}); // enhance common error messages (0, _enhanceErrorMessages["default"])('missingArgument', function (argName) { return "Missing required argument ".concat(_index.chalk.yellow("<".concat(argName, ">")), "."); }); (0, _enhanceErrorMessages["default"])('unknownOption', function (optionName) { return "Unknown option ".concat(_index.chalk.yellow(optionName), "."); }); (0, _enhanceErrorMessages["default"])('optionMissingArgument', function (option, flag) { return "Missing required argument for option ".concat(_index.chalk.yellow(option.flags)) + ( flag ? ", got ".concat(_index.chalk.yellow(flag)) : ""); }); _commander["default"].parse(process.argv); // 如果没有输入命令,输出提示信息 if (!process.argv.slice(2).length) { _commander["default"].outputHelp(); }case 10:case "end":return _context.stop();}}, _callee);}));return _main.apply(this, arguments);} ; function camelize(str) { return str.replace(/-(\w)/g, function (_, c) {return c ? c.toUpperCase() : '';}); } // commander passes the Command object itself as options, // extract only actual options into a fresh object. function cleanArgs(cmd) { var args = {}; cmd.options.forEach(function (o) { var 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; } // 开始服务 try { main(); } catch (err) { }