@quick-game/cli
Version:
Command line interface for rapid qg development
189 lines (169 loc) • 6.42 kB
JavaScript
;var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));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"));
var _requestIde = require("./utils/requestIde.js");
global.AbortController = require('abort-controller');
_didyoumean.default.threshold = 0.6;
const pkg = require('../package.json');
function suggestCommands(cmd) {
const availableCommands = _commander.default.commands.map((cmd) => {
return cmd._name;
});
const suggestion = (0, _didyoumean.default)(cmd, availableCommands);
if (suggestion) {
(0, _index.log)(` ` + _index.chalk.red(`Did you mean ${_index.chalk.yellow(suggestion)}?`));
}
}
async function main() {
_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((name, cmd) => {
const options = cleanArgs(cmd);
(0, _create.default)(name, options);
});
// output help information on unknown commands
_commander.default.
arguments('<command>').
action(async (cmd) => {
const args = (0, _index.minimist)(process.argv.slice(2), {
string: ['buildType']
});
const cleanedArgs = {};
for (const key in args) {
const cleanedKey = key.replace(/--/g, '');
cleanedArgs[cleanedKey] = args[key];
}
(0, _index.info)(`当前版本为:${pkg.version}`);
if (cleanedArgs.port && cleanedArgs.ide) {
(0, _requestIde.setIdePort)(cleanedArgs.port);
}
if (cmd === 'build') {
// debug打包命令
(0, _index.info)('qg build', (0, _stringify.default)(cleanedArgs));
try {
(0, _index2.build)(cleanedArgs).catch(async (err) => {
if (cleanArgs.ide) {
await (0, _requestIde.fetchInfo)({
status: 'error',
message: err.message || '构建失败'
}, 'buildFinish');
}
});
} catch (err) {
if (cleanArgs.ide) {
await (0, _requestIde.fetchInfo)({
status: 'error',
message: err.message || '构建失败'
}, 'buildFinish');
}
}
} else if (cmd === 'release') {
// release打包命令
(0, _index.info)('qg release', (0, _stringify.default)(cleanedArgs));
try {
(0, _index2.release)(cleanedArgs).catch(async (err) => {
if (cleanArgs.ide) {
await (0, _requestIde.fetchInfo)({
status: 'error',
message: err.message || '发布失败'
}, 'releaseFinish');
}
});
} catch (err) {
if (cleanArgs.ide) {
await (0, _requestIde.fetchInfo)({
status: 'error',
message: err.message || '发布失败'
}, 'releaseFinish');
}
}
} else if (cmd === 'transfer') {
// 微小转换命令
(0, _index.info)('qg transfer', (0, _stringify.default)(cleanedArgs));
try {
(0, _index4.transfer)(cleanedArgs).catch(async (err) => {
if (cleanArgs.ide) {
await (0, _requestIde.fetchInfo)({
status: 'error',
message: err.message || '转换失败'
}, 'transFinish');
}
});
} catch (err) {
if (cleanArgs.ide) {
await (0, _requestIde.fetchInfo)({
status: 'error',
message: err.message || '转换失败'
}, 'transFinish');
}
}
} else if (cmd === 'server') {
// 调试服务启动命令
(0, _index.info)('qg server', (0, _stringify.default)(cleanedArgs));
(0, _index3.initServer)(cleanedArgs);
} else {
_commander.default.outputHelp();
(0, _index.log)(` ` + _index.chalk.red(`Unknown command ${_index.chalk.yellow(cmd)}.`));
(0, _index.log)();
suggestCommands(cmd);
}
});
// add some useful info on help
_commander.default.on('--help', () => {
(0, _index.log)();
(0, _index.log)(` Run ${_index.chalk.cyan(`qg <command> --help`)} for detailed usage of given command.`);
(0, _index.log)();
});
_commander.default.commands.forEach((c) => c.on('--help', () => (0, _index.log)()));
// enhance common error messages
(0, _enhanceErrorMessages.default)('missingArgument', (argName) => {
return `Missing required argument ${_index.chalk.yellow(`<${argName}>`)}.`;
});
(0, _enhanceErrorMessages.default)('unknownOption', (optionName) => {
return `Unknown option ${_index.chalk.yellow(optionName)}.`;
});
(0, _enhanceErrorMessages.default)('optionMissingArgument', (option, flag) => {
return `Missing required argument for option ${_index.chalk.yellow(option.flags)}` + (
flag ? `, got ${_index.chalk.yellow(flag)}` : ``);
});
_commander.default.parse(process.argv);
// 如果没有输入命令,输出提示信息
if (!process.argv.slice(2).length) {
_commander.default.outputHelp();
}
};
function camelize(str) {
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '');
}
// commander passes the Command object itself as options,
// extract only actual options into a fresh object.
function cleanArgs(cmd) {
const args = {};
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;
}
// 开始服务
try {
main();
} catch (err) {
}