forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcli.js
56 lines (42 loc) · 1.15 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
var fs = require('fs');
var chalk = require('chalk');
var log = require('loglevel');
var config = require('./config');
var h = require('./helper');
// 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 setColorMode() {
var useColor = config.USE_COLOR || false;
if (process.argv.indexOf('--color') >= 0) useColor = true;
if (process.argv.indexOf('--no-color') >= 0) useColor = false;
chalk.enabled = useColor;
}
function setLogLevel() {
var level = log.levels.INFO;
if (process.argv.indexOf('-v') >= 0) level = log.levels.DEBUG;
if (process.argv.indexOf('-vv') >= 0) level = log.levels.TRACE;
log.setLevel(level);
log.fail = function(msg) {
log.error(chalk.red('ERROR: ' + msg));
};
}
function checkCache() {
var cacheDir = h.getCacheDir();
if (!fs.existsSync(cacheDir))
fs.mkdirSync(cacheDir);
}
var cli = {};
cli.run = function() {
config.init();
checkCache();
setColorMode();
setLogLevel();
require('yargs')
.commandDir('commands')
.completion()
.help()
.strict()
.argv;
};
module.exports = cli;