Skip to content

Commit 33f6808

Browse files
committed
[Config] refactor config.
Signed-off-by: Eric Wang <[email protected]>
1 parent 6985072 commit 33f6808

18 files changed

+194
-160
lines changed

docs/advanced.md

+33-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ Leetcode.com is restricting only one session alive in the same time, which means
1717

1818
The good news is leetcode-cli will help a lot on this by trying re-login transparently and automatically without interrupting your current work whenever it detects your current session is expired. To enable this feature you could add following in your config then login again:
1919

20-
"AUTO_LOGIN": true
20+
{
21+
"autologin": {
22+
"enable": true
23+
}
24+
}
2125

2226
**NOTE: once enabled, your PASSWORD will be persisted locally for further using, so PLEASE be careful to ONLY enable this on your OWN computer for the sake of security!**
2327

@@ -53,30 +57,42 @@ Create a JSON file named `.lcconfig` in your home directory, e.g.
5357
$ cat ~/.lcconfig
5458

5559
{
56-
"LANG": "java",
57-
"EDITOR": "vim",
58-
"USE_COLOR": true,
59-
"COLOR_THEME": "default",
60-
"AUTO_LOGIN": false,
61-
"PLUGINS": {}
60+
"auto_login": {
61+
"enable": false
62+
},
63+
"code": {
64+
"editor": "vim",
65+
"lang": "cpp"
66+
},
67+
"color": {
68+
"enable": true,
69+
"theme": "default"
70+
},
71+
"icon": {
72+
"theme": ""
73+
},
74+
"network": {
75+
"concurrency": 10
76+
},
77+
"plugins": {}
6278
}
6379

6480
Here are some useful settings:
6581

66-
* `AUTO_LOGIN` to enable auto login feature, see [Auto Login](#auto-login).
67-
* `COLOR_THEME` to set color theme used in output, see [Color Theme](#color-theme).
68-
* `EDITOR` to set editor used to open generated source file.
69-
* `ICON_THEME` to set icon them used in output.
70-
* `LANG` to set your default language used in coding.
71-
* `USE_COLOR` to enable colorful output.
72-
* `PLUGINS` to config each installed plugins, see [Plugins](#plugins).
82+
* `autologin:enable` to enable auto login feature, see [Auto Login](#auto-login).
83+
* `code:editor` to set editor used to open generated source file.
84+
* `code:lang` to set your default language used in coding.
85+
* `color:enable` to enable colorful output.
86+
* `color:theme` to set color theme used in output, see [Color Theme](#color-theme).
87+
* `icon:theme` to set icon them used in output.
88+
* `plugins` to config each installed plugins, see [Plugins](#plugins).
7389

7490
*Example*
7591

7692
Config for `github.js` and `cpp.lint.js` plugins:
7793

7894
{
79-
"PLUGINS": {
95+
"plugins": {
8096
"github": {
8197
"repo": "https://github.com/skygragon/test",
8298
"token": "abcdefghijklmnopqrstuvwxyz"
@@ -95,9 +111,9 @@ You can choose to use colorful output or not.
95111
* `--color` to enable color.
96112
* `--no-color` to disable it.
97113

98-
Or use configuration setting to avoid typing it repeatedly, see [USE_COLOR](#configuration).
114+
Or use configuration setting to avoid typing it repeatedly, see [color:enable](#configuration).
99115

100-
When color is enabled, you can choose your favor color theme as well, see [COLOR_THEME](#configuration).
116+
When color is enabled, you can choose your favor color theme as well, see [color:theme](#configuration).
101117

102118
Following are available themes:
103119

docs/commands.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Install plugin from local file:
146146
List all the plugins, `` means the plugin is disabled.
147147

148148
$ leetcode plugin
149-
✔ retry default Plugin to retry last failed request if AUTO_LOGIN is on.
149+
✔ retry default Plugin to retry last failed request if autologin is on.
150150
✔ cache default Plugin to provide local cache.
151151
✔ leetcode default Plugin to talk with leetcode APIs.
152152

@@ -385,16 +385,17 @@ Verbose:
385385
|_|\___|\___|\__|\___|\___/ \__,_|\___| CLI v2.1.1
386386

387387
[Environment]
388-
Cache: /Users/skygragon/.lc/
389-
Config: /Users/skygragon/.lcconfig
388+
Node v8.1.4
389+
OS darwin 16.5.0
390+
Cache: /Users/skygragon/.lc/
391+
Config: /Users/skygragon/.lcconfig
390392

391393
[Configuration]
392-
AUTO_LOGIN true
393-
COLOR_THEME orange
394-
ICON_THEME default
395-
LANG cpp
396-
MAX_WORKERS 10
397-
USE_COLOR true
394+
autologin {"enable":false}
395+
code {"editor":"vim","lang":"haha"}
396+
color {"enable":false,"theme":"default"}
397+
icon {"theme":""}
398+
network {"concurrency":10}
398399

399400
[Themes]
400401
Colors blue,dark,default,orange,pink

lib/cli.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ var Plugin = require('./plugin');
1212
// global config < local config < cli params
1313
// Color is a tricky one so we manually handle it here.
1414
function initColor() {
15-
chalk.enabled = config.USE_COLOR && chalk.enabled;
15+
chalk.enabled = config.color.enable && chalk.enabled;
1616
chalk.init();
17-
chalk.setTheme(config.COLOR_THEME);
17+
chalk.setTheme(config.color.theme);
1818
}
1919

2020
function initIcon() {
2121
icon.init();
22-
icon.setTheme(config.ICON_THEME);
22+
icon.setTheme(config.icon.theme);
2323
}
2424

2525
function initLogLevel() {

lib/commands/plugin.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ var cmd = {
3939
}
4040
};
4141

42-
var URL_PLUGIN = 'https://github.com/skygragon/leetcode-cli-plugins/raw/master/plugins/$name.js';
43-
4442
function install(src) {
43+
var config = require('../config');
4544
// assume to be a raw plugin name if not js file.
4645
if (path.extname(src) !== '.js') {
47-
src = URL_PLUGIN.replace('$name', src);
46+
src = config.sys.urls.plugin.replace('$name', src);
4847
}
4948

5049
// copy to plugins folder

lib/commands/show.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ var cmd = {
2525
lang: {
2626
alias: 'l',
2727
type: 'string',
28-
default: config.LANG,
28+
default: config.code.lang,
2929
describe: 'Program language to use',
30-
choices: config.LANGS
30+
choices: config.sys.langs
3131
},
3232
extra: {
3333
alias: 'x',
@@ -96,7 +96,7 @@ cmd.handler = function(argv) {
9696
fs.writeFileSync(filename, code);
9797

9898
if (argv.editor !== undefined) {
99-
childProcess.spawn(argv.editor || config.EDITOR, [filename], {
99+
childProcess.spawn(argv.editor || config.code.editor, [filename], {
100100
// in case your editor of choice is vim or emacs
101101
stdio: 'inherit'
102102
});

lib/commands/stat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var cmd = {
2323
type: 'string',
2424
default: 'all',
2525
describe: 'Show statistics on given tag',
26-
choices: ['all'].concat(config.CATEGORIES)
26+
choices: ['all'].concat(config.sys.categories)
2727
}
2828
}
2929
};

lib/commands/version.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ cmd.handler = function(argv) {
5656
printLine('Config', h.getConfigFile());
5757

5858
log.info('\n[Configuration]');
59-
_.each(config.getUserConfig(), function(v, k) {
60-
printLine(k, v);
59+
_.each(config.getAll(true), function(v, k) {
60+
if (k === 'plugins') return;
61+
printLine(k, JSON.stringify(v));
6162
});
6263

6364
log.info('\n[Themes]');

lib/config.js

+75-63
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,91 @@
11
var _ = require('underscore');
2+
var nconf = require('nconf');
23

34
var h = require('./helper');
45

5-
// usually you don't wanna change those
6-
var DEFAULT_SYS_CONFIG = {
7-
URL_BASE: 'https://leetcode.com',
8-
URL_LOGIN: 'https://leetcode.com/accounts/login/',
9-
URL_PROBLEMS: 'https://leetcode.com/api/problems/$category/',
10-
URL_PROBLEM: 'https://leetcode.com/problems/$slug/description/',
11-
URL_PROBLEM_DETAIL: 'https://leetcode.com/graphql',
12-
URL_TEST: 'https://leetcode.com/problems/$slug/interpret_solution/',
13-
URL_SUBMIT: 'https://leetcode.com/problems/$slug/submit/',
14-
URL_SUBMISSIONS: 'https://leetcode.com/api/submissions/$slug',
15-
URL_SUBMISSION: 'https://leetcode.com/submissions/detail/$id/',
16-
URL_VERIFY: 'https://leetcode.com/submissions/detail/$id/check/',
17-
URL_FAVORITES: 'https://leetcode.com/list/api/questions',
18-
URL_FAVORITE_DELETE: 'https://leetcode.com/list/api/questions/$hash/$id',
6+
var DEFAULT_CONFIG = {
7+
// usually you don't wanna change those
8+
sys: {
9+
categories: [
10+
'algorithms',
11+
'database',
12+
'shell'
13+
],
14+
langs: [
15+
'bash',
16+
'c',
17+
'cpp',
18+
'csharp',
19+
'golang',
20+
'java',
21+
'javascript',
22+
'kotlin',
23+
'mysql',
24+
'python',
25+
'python3',
26+
'ruby',
27+
'scala',
28+
'swift'
29+
],
30+
urls: {
31+
base: 'https://leetcode.com',
32+
login: 'https://leetcode.com/accounts/login/',
33+
problems: 'https://leetcode.com/api/problems/$category/',
34+
problem: 'https://leetcode.com/problems/$slug/description/',
35+
problem_detail: 'https://leetcode.com/graphql',
36+
test: 'https://leetcode.com/problems/$slug/interpret_solution/',
37+
submit: 'https://leetcode.com/problems/$slug/submit/',
38+
submissions: 'https://leetcode.com/api/submissions/$slug',
39+
submission: 'https://leetcode.com/submissions/detail/$id/',
40+
verify: 'https://leetcode.com/submissions/detail/$id/check/',
41+
favorites: 'https://leetcode.com/list/api/questions',
42+
favorite_delete: 'https://leetcode.com/list/api/questions/$hash/$id',
43+
plugin: 'https://github.com/skygragon/leetcode-cli-plugins/raw/master/plugins/$name.js'
44+
}
45+
},
1946

20-
LANGS: [
21-
'bash',
22-
'c',
23-
'cpp',
24-
'csharp',
25-
'golang',
26-
'java',
27-
'javascript',
28-
'kotlin',
29-
'mysql',
30-
'python',
31-
'python3',
32-
'ruby',
33-
'scala',
34-
'swift'
35-
],
36-
37-
CATEGORIES: [
38-
'algorithms',
39-
'database',
40-
'shell'
41-
],
42-
43-
PLUGINS: {}
44-
};
45-
46-
// but you will want change these
47-
var DEFAULT_USER_CONFIG = {
48-
AUTO_LOGIN: false,
49-
COLOR_THEME: 'default',
50-
EDITOR: 'vim',
51-
ICON_THEME: '',
52-
LANG: 'cpp',
53-
MAX_WORKERS: 10,
54-
USE_COLOR: true
47+
// but you will want change these
48+
autologin: {
49+
enable: false
50+
},
51+
code: {
52+
editor: 'vim',
53+
lang: 'cpp'
54+
},
55+
color: {
56+
enable: true,
57+
theme: 'default'
58+
},
59+
icon: {
60+
theme: ''
61+
},
62+
network: {
63+
concurrency: 10
64+
},
65+
plugins: {}
5566
};
5667

5768
function Config() {}
5869

5970
Config.prototype.init = function() {
60-
// check local config: ~/.lcconfig
61-
var localConfig = JSON.parse(h.getFileData(h.getConfigFile())) || {};
62-
_.extendOwn(this, this.getDefaultConfig());
63-
_.extendOwn(this, localConfig);
64-
};
71+
nconf.file(h.getConfigFile())
72+
.defaults(DEFAULT_CONFIG);
6573

66-
Config.prototype.getDefaultConfig = function() {
67-
var cfg = {};
68-
_.extendOwn(cfg, DEFAULT_SYS_CONFIG);
69-
_.extendOwn(cfg, DEFAULT_USER_CONFIG);
70-
return cfg;
74+
var cfg = nconf.get();
75+
// HACK: remove old style configs
76+
for (var x in cfg) {
77+
if (x === x.toUpperCase()) delete cfg[x];
78+
}
79+
delete DEFAULT_CONFIG.type;
80+
delete cfg.type;
81+
82+
_.extendOwn(this, cfg);
7183
};
7284

73-
Config.prototype.getUserConfig = function() {
74-
return _.pick(this, function(v, k) {
75-
return k in DEFAULT_USER_CONFIG;
76-
});
85+
Config.prototype.getAll = function(useronly) {
86+
var cfg = _.extendOwn({}, this);
87+
if (useronly) delete cfg.sys;
88+
return cfg;
7789
};
7890

7991
module.exports = new Config();

lib/plugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Plugin(id, name, ver, desc, deps) {
1616
}
1717

1818
Plugin.prototype.init = function() {
19-
this.config = config.PLUGINS[this.name] || {};
19+
this.config = config.plugins[this.name] || {};
2020
this.next = null;
2121
};
2222

0 commit comments

Comments
 (0)