Skip to content

Commit cb4d8c2

Browse files
committed
Implement 'version'.
* refactor helper.js a bit. Signed-off-by: Eric Wang <[email protected]>
1 parent fa2e14d commit cb4d8c2

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

lib/cache.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,24 @@ var fs = require('fs');
22

33
var h = require('./helper');
44

5-
// try to save cache in user home.
6-
var BASE_DIR = h.getHomeDir() + '/.lc/';
7-
8-
function getFullPath(k) {
9-
if (!fs.existsSync(BASE_DIR))
10-
fs.mkdirSync(BASE_DIR);
11-
12-
return BASE_DIR + k + '.json';
13-
}
14-
155
var cache = {};
166

177
cache.get = function(k) {
18-
var fullpath = getFullPath(k);
8+
var fullpath = h.getCacheFile(k);
199
if (!fs.existsSync(fullpath)) return null;
2010

2111
var v = JSON.parse(fs.readFileSync(fullpath));
2212
return v;
2313
};
2414

2515
cache.set = function(k, v) {
26-
var fullpath = getFullPath(k);
16+
var fullpath = h.getCacheFile(k);
2717
fs.writeFileSync(fullpath, JSON.stringify(v));
2818
return true;
2919
};
3020

3121
cache.del = function(k) {
32-
var fullpath = getFullPath(k);
22+
var fullpath = h.getCacheFile(k);
3323
if (!fs.existsSync(fullpath)) return false;
3424

3525
fs.unlinkSync(fullpath);

lib/cli.js

+9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ function setColorMode() {
99
require('chalk').enabled = useColor;
1010
}
1111

12+
function checkCache() {
13+
var cacheDir = require('./helper').getCacheDir();
14+
15+
var fs = require('fs');
16+
if (!fs.existsSync(cacheDir))
17+
fs.mkdirSync(cacheDir);
18+
}
19+
1220
var cli = {};
1321

1422
cli.run = function() {
23+
checkCache();
1524
setColorMode();
1625

1726
require('yargs')

lib/commands/version.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var cmd = {
2+
command: 'version',
3+
desc: 'Show version info.',
4+
builder: {
5+
verbose: {
6+
alias: 'v',
7+
type: 'boolean',
8+
describe: 'Show verbose info.'
9+
}
10+
}
11+
};
12+
13+
cmd.handler = function(argv) {
14+
var version = require('../../package.json').version;
15+
16+
if (!argv.verbose) {
17+
return console.log(version);
18+
}
19+
20+
console.log('leetcode-cli', version);
21+
22+
var h = require('../helper');
23+
console.log();
24+
console.log('Cache:', h.getCacheDir());
25+
console.log('Config:', h.getConfigFile());
26+
27+
var config = require('../config');
28+
console.log();
29+
Object.getOwnPropertyNames(config).forEach(function(k) {
30+
console.log(k, '=', config[k]);
31+
});
32+
};
33+
34+
module.exports = cmd;

lib/helper.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ h.extToLang = function(fullpath) {
6262
}
6363
};
6464

65-
h.fileData = function(fullpath) {
65+
h.getFileData = function(fullpath) {
6666
return fs.readFileSync(fullpath).toString();
6767
};
6868

@@ -74,6 +74,18 @@ h.getHomeDir = function() {
7474
return process.env.HOME || process.env.USERPROFILE;
7575
};
7676

77+
h.getCacheDir = function() {
78+
return this.getHomeDir() + '/.lc/';
79+
};
80+
81+
h.getCacheFile = function(k) {
82+
return this.getCacheDir() + k + '.json';
83+
};
84+
85+
h.getConfigFile = function() {
86+
return this.getHomeDir() + '/.lcconfig';
87+
};
88+
7789
h.readStdin = function() {
7890
// FIXME: not work for win32
7991
return fs.readFileSync('/dev/stdin').toString();

lib/leetcode_client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ leetcodeClient.testProblem = function(problem, cb) {
147147
'lang': h.extToLang(problem.file),
148148
'question_id': parseInt(problem.id, 10),
149149
'test_mode': false,
150-
'typed_code': h.fileData(problem.file)
150+
'typed_code': h.getFileData(problem.file)
151151
};
152152

153153
request.post(opts, function(e, resp, body) {
@@ -177,7 +177,7 @@ leetcodeClient.submitProblem = function(problem, cb) {
177177
'lang': h.extToLang(problem.file),
178178
'question_id': parseInt(problem.id, 10),
179179
'test_mode': false,
180-
'typed_code': h.fileData(problem.file)
180+
'typed_code': h.getFileData(problem.file)
181181
};
182182

183183
request.post(opts, function(e, resp, body) {

0 commit comments

Comments
 (0)