UNPKG

@vivo-minigame/cli

Version:

Command line interface for rapid Vivo minigame development

61 lines (53 loc) 1.6 kB
"use strict";var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _events = _interopRequireDefault(require("events")); var _cliSharedUtils = require("@vivo-minigame/cli-shared-utils"); /** * mg-service 的入口 */ class Service extends _events.default { /** * 构造函数 * @param {String} context 服务启动的当前目录 */ constructor(context) { super(); this.context = context; this.commands = {}; // 要加载的文件 const cmds = [ './commands/packager', './commands/server']; cmds.map((id) => { try { require(id)(this, { context: this.context }); } catch (e) { (0, _cliSharedUtils.error)(e); } }); } /** * 注册到Service上的命令 * @param {String} cmd 注册的命令 * @param {Function} fn 命定对应的逻辑 */ registerCommand(cmd, fn) { this.commands[cmd] = { fn }; } /** * 服务启动入口 * @param {String} cmd 启动命令 * @param {Object} args 命令参数,暂时没有 */ run(cmd, args = {}) { args._ = args._ || []; const command = this.commands[cmd]; if (!command) { (0, _cliSharedUtils.error)(`command "${cmd}" does not exist.`); process.exit(1); } const { fn } = command; return fn(args); } }var _default = exports.default = Service;