File tree 3 files changed +94
-0
lines changed
3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1
1
var fs = require ( 'fs' ) ;
2
+ var path = require ( 'path' ) ;
2
3
3
4
var h = require ( './helper' ) ;
4
5
@@ -29,4 +30,19 @@ cache.del = function(k) {
29
30
return true ;
30
31
} ;
31
32
33
+ cache . list = function ( ) {
34
+ var dir = h . getCacheDir ( ) ;
35
+ if ( ! fs . existsSync ( dir ) ) return [ ] ;
36
+
37
+ return fs . readdirSync ( dir ) . map ( function ( filename ) {
38
+ var k = path . basename ( filename , '.json' ) ;
39
+ var stat = fs . statSync ( h . getCacheFile ( k ) ) ;
40
+ return {
41
+ name : k ,
42
+ size : stat . size ,
43
+ mtime : stat . mtimeMs
44
+ } ;
45
+ } ) ;
46
+ } ;
47
+
32
48
module . exports = cache ;
Original file line number Diff line number Diff line change
1
+ var log = require ( 'loglevel' ) ;
2
+ var sprintf = require ( 'sprintf-js' ) . sprintf ;
3
+ var _ = require ( 'underscore' ) ;
4
+
5
+ var cache = require ( '../cache' ) ;
6
+ var h = require ( '../helper' ) ;
7
+
8
+ var cmd = {
9
+ command : 'cache' ,
10
+ desc : 'show cached problems' ,
11
+ builder : {
12
+ all : {
13
+ alias : 'a' ,
14
+ type : 'boolean' ,
15
+ describe : 'Delete all cached problems' ,
16
+ default : false
17
+ } ,
18
+ delete : {
19
+ alias : 'd' ,
20
+ type : 'string' ,
21
+ describe : 'Delete specific cached problem'
22
+ }
23
+ }
24
+ } ;
25
+
26
+ cmd . handler = function ( argv ) {
27
+ if ( argv . delete === undefined ) {
28
+ _ . sortBy ( cache . list ( ) , function ( f ) {
29
+ var x = parseInt ( f . name . split ( '.' ) [ 0 ] , 10 ) ;
30
+ if ( _ . isNaN ( x ) ) x = 0 ;
31
+ return x ;
32
+ } )
33
+ . forEach ( function ( f ) {
34
+ log . info ( sprintf ( '%-50s %8s %s ago' ,
35
+ f . name ,
36
+ h . prettySize ( f . size ) ,
37
+ h . prettyTime ( f . mtime ) ) ) ;
38
+ } ) ;
39
+ } else if ( argv . all ) {
40
+ cache . list ( ) . forEach ( function ( f ) {
41
+ if ( f . name === '.user' ) return ;
42
+ cache . del ( f . name ) ;
43
+ } ) ;
44
+ } else {
45
+ cache . del ( argv . delete ) ;
46
+ }
47
+ } ;
48
+
49
+ module . exports = cmd ;
Original file line number Diff line number Diff line change @@ -23,6 +23,35 @@ h.prettyText = function(text, yesNo) {
23
23
}
24
24
} ;
25
25
26
+ h . prettySize = function ( size ) {
27
+ var units = 'BKMG' ;
28
+ var i = 0 ;
29
+ while ( size >= 1024 && i < units . length ) {
30
+ size /= 1024.0 ;
31
+ ++ i ;
32
+ }
33
+ return size . toFixed ( 2 ) + units [ i ] ;
34
+ } ;
35
+
36
+ h . prettyTime = function ( t ) {
37
+ var units = [
38
+ [ 60 , 'secs' ] ,
39
+ [ 60 , 'mins' ] ,
40
+ [ 24 , 'hours' ] ,
41
+ [ 7 , 'weeks' ] ,
42
+ [ 4 , 'months' ] ,
43
+ [ 12 , 'years' ]
44
+ ] ;
45
+
46
+ var d = ( Date . now ( ) - t ) / 1000 ;
47
+ var i = 0 ;
48
+ while ( d > units [ i ] [ 0 ] && i < units . length ) {
49
+ d /= units [ i ] [ 0 ] ;
50
+ ++ i ;
51
+ }
52
+ return d . toFixed ( 0 ) + ' ' + units [ i ] [ 1 ] ;
53
+ } ;
54
+
26
55
h . levelToName = function ( level ) {
27
56
switch ( level ) {
28
57
case 1 : return 'Easy' ;
You can’t perform that action at this time.
0 commit comments