UNPKG

@feflow/cli

Version:
140 lines 5.62 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var fs_1 = __importDefault(require("fs")); var path_1 = __importDefault(require("path")); var chalk_1 = __importDefault(require("chalk")); var cross_spawn_1 = __importDefault(require("cross-spawn")); var cli_html_c_1 = __importDefault(require("cli-html-c")); var command_line_usage_1 = __importDefault(require("command-line-usage")); var marked_1 = require("marked"); var constant_1 = require("../../shared/constant"); var getCommands = function (store) { var arr = []; Object.entries(store).forEach(function (_a) { var _b = __read(_a, 2), colA = _b[0], desc = _b[1].desc; arr.push({ colA: colA, colB: (desc instanceof Function ? desc() : desc).replace(/({|})/g, function (val) { return escape(val); }), }); }); return arr; }; var showHelp = function (commands) { var sections = [ { header: 'Usage', content: '$ fef [options] [command]', }, { header: 'Commands', content: { data: commands, options: { maxWidth: 100, }, }, }, { header: 'Options', optionList: [ { name: 'version', description: 'Print version and exit successfully.', }, { name: 'help', description: 'Print this help and exit successfully.', }, { name: 'disable-check', description: 'Disable @feflow/cli and installed plugins check update', }, { name: 'auto-update', description: 'Auto update @feflow/cli and installed plugins', }, ], }, ]; return (0, command_line_usage_1.default)(sections); }; var parseReadme = function (path) { var readmeText = ''; if (fs_1.default.existsSync(path)) { readmeText = fs_1.default.readFileSync(path, 'utf8'); } return (0, cli_html_c_1.default)((0, marked_1.marked)(readmeText)); }; exports.default = (function (ctx) { ctx.commander.register('help', 'Help messages', function () { var _a; var store = ctx.commander.store; var cmd = ctx.args._[0]; cmd = cmd && String.prototype.toLowerCase.call(cmd); // fef help xxx 的 case if (cmd) { if (Object.prototype.hasOwnProperty.call(store, cmd)) { var commandInfo = store[cmd]; // 优先展示组件注册信息 if ((_a = commandInfo.options) === null || _a === void 0 ? void 0 : _a.length) { var universalUsage = commandInfo.options[0]; var _b = universalUsage instanceof Function ? universalUsage() : universalUsage, type = _b.type, content = _b.content; // case 1: 多语言情况下 yml 有 usage 属性时,执行对应的内容 if (type === 'usage') { (0, cross_spawn_1.default)(content, { stdio: 'inherit', shell: true, windowsHide: true, }); return; } // case 2: 多语言情况下 yml 没有 usage 属性时,优先读取 readme 展示出来 if (type === 'path') { var pluginConfigPath = path_1.default.join(content, constant_1.UNIVERSAL_README_CONFIG); var readmeText = parseReadme(pluginConfigPath); if (readmeText) { console.log(readmeText); return; } // case 3: 多语言情况下既没有 usage,又没有 README.md,则展示插件的 desc console.log(commandInfo.desc); return; } // case 4: nodejs 且有写 options 的情况 var usage_1 = (0, command_line_usage_1.default)(commandInfo.options); console.log(usage_1); return; } // case 5: nodejs 且没有写 options 的情况,直接展示插件的 desc console.log(commandInfo.desc); return; } console.log(chalk_1.default.yellow("Command '".concat(cmd, "' not found in feflow. You need to install it first."))); console.log('Below is the usage of feflow.'); } // 打印 fef 的 usage var commands = getCommands(ctx.commander.store); var usage = showHelp(commands); console.log(usage); }); }); //# sourceMappingURL=help.js.map