File tree 5 files changed +61
-16
lines changed
5 files changed +61
-16
lines changed Original file line number Diff line number Diff line change @@ -2,34 +2,24 @@ var fs = require('fs');
2
2
3
3
var h = require ( './helper' ) ;
4
4
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
-
15
5
var cache = { } ;
16
6
17
7
cache . get = function ( k ) {
18
- var fullpath = getFullPath ( k ) ;
8
+ var fullpath = h . getCacheFile ( k ) ;
19
9
if ( ! fs . existsSync ( fullpath ) ) return null ;
20
10
21
11
var v = JSON . parse ( fs . readFileSync ( fullpath ) ) ;
22
12
return v ;
23
13
} ;
24
14
25
15
cache . set = function ( k , v ) {
26
- var fullpath = getFullPath ( k ) ;
16
+ var fullpath = h . getCacheFile ( k ) ;
27
17
fs . writeFileSync ( fullpath , JSON . stringify ( v ) ) ;
28
18
return true ;
29
19
} ;
30
20
31
21
cache . del = function ( k ) {
32
- var fullpath = getFullPath ( k ) ;
22
+ var fullpath = h . getCacheFile ( k ) ;
33
23
if ( ! fs . existsSync ( fullpath ) ) return false ;
34
24
35
25
fs . unlinkSync ( fullpath ) ;
Original file line number Diff line number Diff line change @@ -9,9 +9,18 @@ function setColorMode() {
9
9
require ( 'chalk' ) . enabled = useColor ;
10
10
}
11
11
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
+
12
20
var cli = { } ;
13
21
14
22
cli . run = function ( ) {
23
+ checkCache ( ) ;
15
24
setColorMode ( ) ;
16
25
17
26
require ( 'yargs' )
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ h.extToLang = function(fullpath) {
62
62
}
63
63
} ;
64
64
65
- h . fileData = function ( fullpath ) {
65
+ h . getFileData = function ( fullpath ) {
66
66
return fs . readFileSync ( fullpath ) . toString ( ) ;
67
67
} ;
68
68
@@ -74,6 +74,18 @@ h.getHomeDir = function() {
74
74
return process . env . HOME || process . env . USERPROFILE ;
75
75
} ;
76
76
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
+
77
89
h . readStdin = function ( ) {
78
90
// FIXME: not work for win32
79
91
return fs . readFileSync ( '/dev/stdin' ) . toString ( ) ;
Original file line number Diff line number Diff line change @@ -147,7 +147,7 @@ leetcodeClient.testProblem = function(problem, cb) {
147
147
'lang' : h . extToLang ( problem . file ) ,
148
148
'question_id' : parseInt ( problem . id , 10 ) ,
149
149
'test_mode' : false ,
150
- 'typed_code' : h . fileData ( problem . file )
150
+ 'typed_code' : h . getFileData ( problem . file )
151
151
} ;
152
152
153
153
request . post ( opts , function ( e , resp , body ) {
@@ -177,7 +177,7 @@ leetcodeClient.submitProblem = function(problem, cb) {
177
177
'lang' : h . extToLang ( problem . file ) ,
178
178
'question_id' : parseInt ( problem . id , 10 ) ,
179
179
'test_mode' : false ,
180
- 'typed_code' : h . fileData ( problem . file )
180
+ 'typed_code' : h . getFileData ( problem . file )
181
181
} ;
182
182
183
183
request . post ( opts , function ( e , resp , body ) {
You can’t perform that action at this time.
0 commit comments