UNPKG

2.18 kBJavaScriptView Raw
1'use strict';
2
3require('./global');
4const version = require('../package.json').version;
5const optimist = require('optimist');
6const rightPad = require('right-pad');
7
8let Manager = require('./modules/manager.js');
9
10let helpTitle = `\n===================== YKit ${version} ====================\n`;
11
12let initOptions = (cmd) => {
13 if (cmd.setOptions) {
14 cmd.setOptions(optimist);
15 } else if (cmd.set_options) {
16 cmd.set_options(optimist);
17 }
18 optimist.alias('h', 'help');
19 optimist.describe('h', '查看帮助');
20 let options = optimist.argv;
21 options.cwd = process.cwd();
22 return options;
23};
24
25let cli = module.exports = {
26 run: (option) => {
27 if (option === '-v' || option === '--version') {
28 log(version);
29 return;
30 } else if (option === '-h' || option === '--help' || !option) {
31 cli.help();
32 return;
33 }
34
35 let project = Manager.getProject(process.cwd());
36 let command = project.commands.filter((command) => command.name === option || command.abbr === option)[0];
37 if (!command) {
38 error('Command ' + option + ' not found.');
39 return;
40 }
41 let module = command.module;
42 let options = initOptions(module);
43 if (options.h || options.help) {
44 info(helpTitle);
45 info('命令:', option);
46 info('说明:', module.usage || '');
47 info();
48 optimist.showHelp();
49 info(' 如果需要帮助, 请使用 ykit {命令名} --help ');
50 } else {
51 module.run.call({project}, options);
52 }
53 },
54 help: () => {
55 info(helpTitle);
56 Manager.getProject(process.cwd()).commands.forEach((command) => {
57 const commandStr = rightPad(rightPad(command.name, 8) + (command.abbr || ''), 25);
58 info(` ${commandStr} # ${command.module.usage || ''}`);
59 });
60 info();
61 info(' 可用的全局配置有:', (Manager.readRC().configs || []).map((item) => item.name.substring(12)).join(', '));
62 info();
63 info(' 如果需要帮助, 请使用 ykit {命令名} --help\n');
64 }
65};