UNPKG

dingtalk-design-cli

Version:

dingtalk design cli

251 lines (250 loc) 12 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EPerformanceEvent = void 0; const cac = __importStar(require("cac")); const chalk_1 = __importDefault(require("chalk")); const logger_1 = require("../lib/cli-shared-utils/lib/logger"); const types_1 = require("../lib/common/types"); const command_1 = __importDefault(require("./command")); const utils_1 = require("./utils"); const context_1 = __importDefault(require("./context")); const suggestCommands_1 = __importDefault(require("../lib/util/suggestCommands")); const getCurrentPkgInfo_1 = __importDefault(require("../lib/util/getCurrentPkgInfo")); const performance_1 = __importDefault(require("../lib/util/performance")); const framework_monitor_1 = __importDefault(require("../lib/cli-shared-utils/lib/monitor/framework-monitor")); const config_1 = __importDefault(require("../lib/common/config")); const spinner_1 = require("../lib/cli-shared-utils/lib/spinner"); const commandsConfig_1 = __importDefault(require("../commands/commandsConfig")); const monitor = (0, framework_monitor_1.default)(config_1.default.yuyanId); var EPerformanceEvent; (function (EPerformanceEvent) { EPerformanceEvent["START"] = "start"; EPerformanceEvent["SPLASH_SCREEN"] = "splashscreen"; })(EPerformanceEvent = exports.EPerformanceEvent || (exports.EPerformanceEvent = {})); class Scheduler { constructor(opts) { performance_1.default.tick(EPerformanceEvent.START); this.opts = opts; this.commandList = []; this.registedCommandList = []; this.program = cac.cac(); this.commandContext = new context_1.default(opts.cwd || process.cwd(), opts.yuyanId, opts.commandArgs, opts.commandOptions, !!opts.verbose); logger_1.logger.debug('scheduler factory opts', this.opts); logger_1.logger.debug('scheduler commandContext', this.commandContext); } /** * 初始化全局参数默认值 * @param originOpts 命令行传入的参数 * @returns */ generateActionOptions(originOpts) { return Object.assign(Object.assign({}, originOpts), { cwd: this.commandContext.cwd, verbose: this.opts.verbose }); } getWrappedCommandAction(command, commandName, action) { return (...callbackArgs) => __awaiter(this, void 0, void 0, function* () { monitor.logPv(); let args; let opts; if (callbackArgs.length === 1) { args = []; opts = callbackArgs[0]; } else { args = callbackArgs.slice(0, callbackArgs.length - 1); opts = callbackArgs[callbackArgs.length - 1]; } opts = this.generateActionOptions(opts); logger_1.logger.debug(`command ${commandName} action options`, opts); this.trackSplashScreen(); return yield action(opts, command.commandContext); }); } loadCommand(name) { const command = new command_1.default({ name, ctx: this.commandContext, root: this.opts.commandRoot, }); this.commandList.push(command); return command; } registerCommandsWithoutAction() { const { options, } = this.program.parse(process.argv, { run: false, }); Object.keys(commandsConfig_1.default).forEach(commandName => { if (commandsConfig_1.default[commandName].needRegister && options) { if (commandsConfig_1.default[commandName].needRegister(options)) { this.registerCommandOption(commandsConfig_1.default[commandName]); } } else { this.registerCommandOption(commandsConfig_1.default[commandName]); } }); } registerCommandOption(commandConfig) { const { command, options, } = commandConfig; const { program, } = this; const currentCommand = program.command(command.name, command.description); if (options) { Object.keys(options).forEach(optionName => { const { name: _name, description, config, } = (0, utils_1.serializeCommandOption)(optionName, options[optionName] || {}); currentCommand.option(_name, description, config); }); } this.registedCommandList.push(currentCommand); return currentCommand; } registerCommand(commandInst, commandConfigs) { const commandName = commandConfigs.command.name; let currentCommand = this.registedCommandList.find(cacCommand => cacCommand.name === commandName); if (!currentCommand) { currentCommand = this.registerCommandOption(commandConfigs); logger_1.logger.debug('cannot find registed command', commandName); } currentCommand.allowUnknownOptions(); currentCommand.action(this.getWrappedCommandAction(commandInst, commandName, commandConfigs.action)); return this.program; } registerCommandsAction() { this.commandList.forEach(commandInst => { if (typeof commandInst.getHook(types_1.ECommandConfigProperty.registerCommand) === 'function') { const commandConfigs = commandInst.applyHook(types_1.ECommandConfigProperty.registerCommand); this.registerCommand(commandInst, commandConfigs); } }); } applyCommandsHook(name) { switch (name) { case types_1.ECommandConfigProperty.registerCommand: this.registerCommandsAction(); break; default: break; } } trackSplashScreen() { performance_1.default.tick(EPerformanceEvent.SPLASH_SCREEN); const splashScreenDuration = performance_1.default.interval(EPerformanceEvent.START, EPerformanceEvent.SPLASH_SCREEN); logger_1.logger.debug(chalk_1.default.bgRedBright('splashScreenDuration'), splashScreenDuration); monitor.logSplashScreen(splashScreenDuration); } logVersion() { return __awaiter(this, void 0, void 0, function* () { const pkgInfo = (0, getCurrentPkgInfo_1.default)(); let deps = [{ name: types_1.EDtdCLIKeyDep.cli, version: pkgInfo.version, }]; if (this.opts.verbose) { deps = deps.concat([{ name: types_1.EDtdCLIKeyDep.generator, version: '', }, { name: types_1.EDtdCLIKeyDep.opensdk, version: '', }, { name: types_1.EDtdCLIKeyDep.validateScript, version: '', }]); } const depInfo = yield (0, utils_1.getVersionLog)(deps); console.log(`${depInfo.map(dep => `- ${dep.name}: ${chalk_1.default.yellow(dep.version)} ${dep.path ? `[${dep.path}]` : ''}`).join('\n')}`); }); } /** * 登记全局options * 登记命令 * 初始化配置和监控等实例 */ bootstrap() { return __awaiter(this, void 0, void 0, function* () { const pkgInfo = (0, getCurrentPkgInfo_1.default)(); const pkgName = pkgInfo.name; const pkgVersion = pkgInfo.version; logger_1.logger.debug(`${pkgName}@${pkgVersion}`); logger_1.logger.debug('origin process args', process.argv); this.program.name = 'ding'; this.program.option('--cwd [cwd]', `[可选] 当前的工作目录, 默认值是 ${chalk_1.default.yellow('process.cwd()')}`); this.program.option('--verbose', '[可选] 打开框架日志调试'); this.registerCommandsWithoutAction(); const { options, } = this.program.parse(process.argv, { run: false, }); if (options.v || options.version) { this.logVersion(); } else if (options.help || options.h) { this.trackSplashScreen(); this.program.outputHelp(); } else if (this.program.matchedCommand) { /** 1. loadCommand */ (0, spinner_1.logWithSpinner)('DingTalk Design CLI 启动中 \n'); yield this.loadCommand(types_1.ECommandName.init); yield this.loadCommand(types_1.ECommandName.upload); yield this.loadCommand(types_1.ECommandName.lint); yield this.loadCommand(types_1.ECommandName.dev); yield this.loadCommand(types_1.ECommandName.preview); yield this.loadCommand(types_1.ECommandName.ngrok); // 特供于支付宝-钉钉小程序 // 参考:https://yuque.antfin.com/docs/share/f5fdcc3d-e6ec-4f00-b598-0bda87a55aa0?# if (options.inside) { yield this.loadCommand(types_1.ECommandName.login); yield this.loadCommand(types_1.ECommandName.remoteDebug); yield this.loadCommand(types_1.ECommandName.previewInside); } (0, spinner_1.successSpinner)('启动成功'); /** 2. registerCommandWithAction */ yield this.applyCommandsHook(types_1.ECommandConfigProperty.registerCommand); logger_1.logger.debug('matchedCommand', this.program.matchedCommand.name); /** 3. runMatched command */ try { yield this.program.runMatchedCommand(); } catch (error) { console.log(chalk_1.default.gray(`Error occurred on "${this.program.matchedCommandName}" command`)); throw error; } } else { this.program.outputHelp(); const commandName = this.commandContext.commandArgs[0]; commandName && (0, suggestCommands_1.default)(commandName, this.commandList.map(v => v.commandContext.commandName || '')); } }); } } exports.default = Scheduler;