UNPKG

5.18 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6var command_line_usage_1 = __importDefault(require("command-line-usage"));
7var fs_1 = __importDefault(require("fs"));
8var path_1 = __importDefault(require("path"));
9var chalk_1 = __importDefault(require("chalk"));
10var cross_spawn_1 = __importDefault(require("cross-spawn"));
11var cli_html_c_1 = __importDefault(require("cli-html-c"));
12var marked_1 = __importDefault(require("marked"));
13var constant_1 = require("../../shared/constant");
14var getCommands = function (store) {
15 var arr = [];
16 for (var name_1 in store) {
17 var desc = store[name_1].desc || '';
18 arr.push({
19 colA: name_1,
20 colB: (desc instanceof Function ? desc() : desc).replace(/({|})/g, function (val) { return escape(val); })
21 });
22 }
23 return arr;
24};
25var showHelp = function (commands) {
26 var sections = [
27 {
28 header: 'Usage',
29 content: '$ fef [options] [command]'
30 },
31 {
32 header: 'Commands',
33 content: {
34 data: commands,
35 options: {
36 maxWidth: 60
37 }
38 }
39 },
40 {
41 header: 'Options',
42 optionList: [
43 {
44 name: 'version',
45 description: 'Print version and exit successfully.'
46 },
47 {
48 name: 'help',
49 description: 'Print this help and exit successfully.'
50 },
51 {
52 name: 'disable-check',
53 description: 'Disable @feflow/cli and installed plugins check update'
54 },
55 {
56 name: 'auto-update',
57 description: 'Auto update @feflow/cli and installed plugins'
58 }
59 ]
60 }
61 ];
62 var usage = command_line_usage_1.default(sections);
63 return usage;
64};
65var parseReadme = function (path) {
66 var readmeText;
67 if (fs_1.default.existsSync(path)) {
68 try {
69 readmeText = fs_1.default.readFileSync(path, 'utf8');
70 }
71 catch (e) {
72 throw new Error(e);
73 }
74 }
75 return cli_html_c_1.default(marked_1.default(readmeText));
76};
77module.exports = function (ctx) {
78 ctx.commander.register('help', 'Help messages', function () {
79 var store = ctx.commander.store;
80 var cmd = ctx.args['_'][0];
81 cmd = cmd && String.prototype.toLowerCase.call(cmd);
82 // fef help xxx 的 case
83 if (cmd) {
84 if (Object.prototype.hasOwnProperty.call(store, cmd)) {
85 var commandInfo = store[cmd];
86 // 优先展示组件注册信息
87 if (commandInfo.options && commandInfo.options.length) {
88 var universalUsage = commandInfo.options[0];
89 var _a = universalUsage instanceof Function ?
90 universalUsage() : universalUsage, type = _a.type, content = _a.content;
91 // case 1: 多语言情况下 yml 有 usage 属性时,执行对应的内容
92 if (type === 'usage') {
93 cross_spawn_1.default(content, {
94 stdio: 'inherit',
95 shell: true,
96 windowsHide: true
97 });
98 return;
99 }
100 // case 2: 多语言情况下 yml 没有 usage 属性时,优先读取 readme 展示出来
101 if (type === 'path') {
102 var pluginConfigPath = path_1.default.join(content, constant_1.UNIVERSAL_README_CONFIG);
103 var readmeText = parseReadme(pluginConfigPath);
104 if (readmeText) {
105 console.log(readmeText);
106 return;
107 }
108 // case 3: 多语言情况下既没有 usage,又没有 README.md,则展示插件的 desc
109 console.log(commandInfo.desc);
110 return;
111 }
112 // case 4: nodejs 且有写 options 的情况
113 var usage_1 = command_line_usage_1.default(commandInfo.options);
114 console.log(usage_1);
115 return;
116 }
117 // case 5: nodejs 且没有写 options 的情况,直接展示插件的 desc
118 console.log(commandInfo.desc);
119 return;
120 }
121 console.log(chalk_1.default.yellow("Command '" + cmd + "' not found in feflow. You need to install it first."));
122 console.log("Below is the usage of feflow.");
123 }
124 // 打印 fef 的 usage
125 var commands = getCommands(ctx.commander.store);
126 var usage = showHelp(commands);
127 console.log(usage);
128 return;
129 });
130};
131//# sourceMappingURL=help.js.map
\No newline at end of file