forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcache.js
67 lines (59 loc) · 1.82 KB
/
cache.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
'use strict';
var _ = require('underscore');
var h = require('../helper');
var chalk = require('../chalk');
var log = require('../log');
var cache = require('../cache');
var session = require('../session');
var sprintf = require('../sprintf');
const cmd = {
command: 'cache [keyword]',
desc: 'Manage local cache',
builder: function(yargs) {
return yargs
.option('d', {
alias: 'delete',
type: 'boolean',
describe: 'Delete cache by keyword',
default: false
})
.positional('keyword', {
type: 'string',
describe: 'Cache name or question id',
default: ''
})
.example(chalk.yellow('leetcode cache'), 'Show all cache')
.example(chalk.yellow('leetcode cache 1'), 'Show cache of question 1')
.example('', '')
.example(chalk.yellow('leetcode cache -d'), 'Delete all cache')
.example(chalk.yellow('leetcode cache 1 -d'), 'Delete cache of question 1');
}
};
cmd.handler = function(argv) {
session.argv = argv;
const name = argv.keyword;
const isInteger = Number.isInteger(Number(name));
const caches = cache.list()
.filter(function(f) {
return (name.length === 0) ||
(isInteger ? f.name.startsWith(name + '.') : f.name === name);
});
if (argv.delete) {
for (let f of caches) cache.del(f.name);
} else {
log.info(chalk.gray(sprintf(' %s %63s %s', 'Cache', 'Size', 'Created')));
log.info(chalk.gray('-'.repeat(86)));
_.sortBy(caches, function(f) {
let x = parseInt(f.name.split('.')[0], 10);
if (Number.isNaN(x)) x = 0;
return x;
})
.forEach(function(f) {
log.printf(' %-60s %8s %s ago',
chalk.green(f.name),
h.prettySize(f.size),
h.prettyTime((Date.now() - f.mtime) / 1000));
});
}
};
module.exports = cmd;