forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathconfig.js
87 lines (75 loc) · 2.28 KB
/
config.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
var _ = require('underscore');
var nconf = require('nconf');
var h = require('../helper');
var chalk = require('../chalk');
var config = require('../config');
var log = require('../log');
var session = require('../session');
var cmd = {
command: 'config [key] [value]',
aliases: ['conf', 'cfg', 'setting'],
desc: 'Manage user configs',
builder: function(yargs) {
return yargs
.option('a', {
alias: 'all',
type: 'boolean',
describe: 'Show all config',
default: false
})
.option('d', {
alias: 'delete',
type: 'boolean',
describe: 'Delete config by key',
default: false
})
.positional('key', {
type: 'string',
describe: 'Config key',
default: ''
})
.positional('value', {
type: 'string',
describe: 'Config value',
default: ''
})
.example(chalk.yellow('leetcode config'), 'Show user configs')
.example(chalk.yellow('leetcode config -a'), 'Show all configs = user + default')
.example(chalk.yellow('leetcode config plugins:github:repo "your repo URL"'), 'Set config by key')
.example(chalk.yellow('leetcode config plugins:github'), 'Show config by key')
.example(chalk.yellow('leetcode config plugins:github -d'), 'Delete config by key');
}
};
function prettyConfig(cfg) {
return JSON.stringify(cfg, null, 2);
}
function loadConfig(showall) {
var cfg = showall ? config.getAll(true) : nconf.get();
return _.omit(cfg, 'type');
}
function saveConfig() {
require('fs').writeFileSync(h.getConfigFile(), prettyConfig(loadConfig(false)));
}
cmd.handler = function(argv) {
session.argv = argv;
nconf.file('local', h.getConfigFile());
// show all
if (argv.key.length === 0)
return log.info(prettyConfig(loadConfig(argv.all)));
var v = nconf.get(argv.key);
// delete
if (argv.delete) {
if (v === undefined) return log.error('Key not found: ' + argv.key);
nconf.clear(argv.key);
return saveConfig();
}
// show
if (argv.value.length === 0) {
if (v === undefined) return log.error('Key not found: ' + argv.key);
return log.info(prettyConfig(v));
}
// set
nconf.set(argv.key, JSON.parse(argv.value));
return saveConfig();
};
module.exports = cmd;