forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcli.js
82 lines (65 loc) · 1.82 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
var _ = require('underscore');
var chalk = require('./chalk');
var config = require('./config');
var core = require('./core');
var h = require('./helper');
var icon = require('./icon');
var log = require('./log');
var 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() {
// FIXME: delete this hack when supports-color handles it well.
if (process.env.TERM_PROGRAM === 'iTerm.app') chalk.use256 = true;
chalk.enabled = config.USE_COLOR && chalk.enabled;
chalk.init();
chalk.setTheme(config.COLOR_THEME);
}
function initIcon() {
icon.init();
icon.setTheme(config.ICON_THEME);
}
function initLogLevel() {
log.init();
var level = 'INFO';
if (process.argv.indexOf('-v') >= 0) level = 'DEBUG';
if (process.argv.indexOf('-vv') >= 0) level = 'TRACE';
// print HTTP details in TRACE
if (level === 'TRACE') {
var request = require('request');
request.debug = true;
console.error = _.wrap(console.error, function(func) {
var args = _.toArray(arguments);
args.shift();
// FIXME: hack HTTP request log, hope no one else use it...
if (args.length > 0 && args[0].indexOf('REQUEST ') === 0) {
args = args.map(function(arg) {
return h.printSafeHTTP(arg);
});
log.trace.apply(log, args);
} else {
log.info.apply(log, args);
}
});
}
log.setLevel(level);
}
var cli = {};
cli.run = function() {
config.init();
initColor();
initIcon();
initLogLevel();
Plugin.init(core);
process.stdout.on('error', function(e) {
if (e.code === 'EPIPE') process.exit();
});
require('yargs')
.commandDir('commands')
.completion()
.help()
.strict()
.argv;
};
module.exports = cli;