forked from leetcode-tools/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
126 lines (107 loc) · 2.78 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
const _ = require('underscore');
const chalk = require('./chalk');
const cache = require('./cache');
const config = require('./config');
const h = require('./helper');
const file = require('./file');
const icon = require('./icon');
const log = require('./log');
const Plugin = require('./plugin');
// We are expecting a tier configuration like:
// global config < local config < cli params
// Color is a tricky one so we manually handle it here.
function initColor() {
chalk.enabled = config.color.enable && chalk.enabled;
chalk.init();
chalk.setTheme(config.color.theme);
}
function initIcon() {
icon.init();
icon.setTheme(config.icon.theme);
}
function initLogLevel() {
log.init();
let level = 'INFO';
if (process.argv.includes('-v')) level = 'DEBUG';
if (process.argv.includes('-vv')) level = 'TRACE';
// print HTTP details in TRACE
if (level === 'TRACE') {
const request = require('request');
request.debug = true;
// eslint-disable-next-line no-console
console.error = _.wrap(console.error, (func, ...args) => {
// FIXME: hack HTTP request log, hope no one else uses it...
if (args.length > 0 && args[0].startsWith('REQUEST ')) {
args = args.map((x) => h.printSafeHTTP(x));
log.trace(...args);
} else {
log.info(...args);
}
});
}
log.setLevel(level);
}
function initDir() {
file.init();
file.mkdir(file.homeDir());
}
function initPlugins(cb) {
if (Plugin.init()) {
Plugin.save();
return cb();
} else {
Plugin.installMissings((e) => {
if (e) return cb(e);
Plugin.init();
return cb();
});
}
}
const cli = {};
const commands = [
require('./commands/cache'),
require('./commands/config'),
require('./commands/list'),
require('./commands/plugin'),
require('./commands/session'),
require('./commands/show'),
require('./commands/star'),
require('./commands/stat'),
require('./commands/submission'),
require('./commands/submit'),
require('./commands/test'),
require('./commands/user'),
require('./commands/version'),
];
function runCommand() {
const yargs = require('yargs');
h.width = yargs.terminalWidth();
commands.forEach((command) => yargs.command(command));
// eslint-disable-next-line no-unused-expressions
yargs
.completion()
.help('h')
.alias('h', 'help')
.version(false)
.epilog(
'Seek more help at https://skygragon.github.io/leetcode-cli/commands'
)
.wrap(Math.min(h.width, 120)).argv;
}
cli.run = function () {
process.stdout.on('error', (e) => {
if (e.code === 'EPIPE') process.exit();
});
config.init();
initColor();
initIcon();
initLogLevel();
initDir();
initPlugins((e) => {
if (e) return log.fatal(e);
cache.init();
runCommand();
});
};
module.exports = cli;