1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | var command_line_usage_1 = __importDefault(require("command-line-usage"));
|
7 | var fs_1 = __importDefault(require("fs"));
|
8 | var path_1 = __importDefault(require("path"));
|
9 | var chalk_1 = __importDefault(require("chalk"));
|
10 | var cross_spawn_1 = __importDefault(require("cross-spawn"));
|
11 | var cli_html_c_1 = __importDefault(require("cli-html-c"));
|
12 | var marked_1 = __importDefault(require("marked"));
|
13 | var constant_1 = require("../../shared/constant");
|
14 | var 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 | };
|
25 | var 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 | };
|
65 | var 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 | };
|
77 | module.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 |
|
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 |
|
92 | if (type === 'usage') {
|
93 | cross_spawn_1.default(content, {
|
94 | stdio: 'inherit',
|
95 | shell: true,
|
96 | windowsHide: true
|
97 | });
|
98 | return;
|
99 | }
|
100 |
|
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 |
|
109 | console.log(commandInfo.desc);
|
110 | return;
|
111 | }
|
112 |
|
113 | var usage_1 = command_line_usage_1.default(commandInfo.options);
|
114 | console.log(usage_1);
|
115 | return;
|
116 | }
|
117 |
|
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 |
|
125 | var commands = getCommands(ctx.commander.store);
|
126 | var usage = showHelp(commands);
|
127 | console.log(usage);
|
128 | return;
|
129 | });
|
130 | };
|
131 |
|
\ | No newline at end of file |