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