Skip to content

Commit af54ce2

Browse files
committed
merge from upstream
2 parents 9caf9bc + c5f6b89 commit af54ce2

File tree

19 files changed

+1218
-451
lines changed

19 files changed

+1218
-451
lines changed

lib/cache.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ cache.init = function() {
99
file.mkdir(file.cacheDir());
1010
};
1111

12+
cache.deleteAll = function () {
13+
cache.list().forEach(value => {
14+
cache.del(value.name);
15+
})
16+
};
17+
1218
cache.get = function(k) {
1319
const fullpath = file.cacheFile(k);
1420
if (!file.exist(fullpath)) return null;

lib/commands/list.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,40 @@ const cmd = {
1414
desc: 'List questions',
1515
builder: function(yargs) {
1616
return yargs
17-
.option('q', core.filters.query)
18-
.option('s', {
19-
alias: 'stat',
20-
type: 'boolean',
21-
default: false,
22-
describe: 'Show statistics of listed questions'
23-
})
24-
.option('t', core.filters.tag)
25-
.option('x', {
26-
alias: 'extra',
27-
type: 'boolean',
28-
default: false,
29-
describe: 'Show extra details: category, companies, tags.'
30-
})
31-
.option('c', core.filters.category)
32-
.positional('keyword', {
33-
type: 'string',
34-
default: '',
35-
describe: 'Filter questions by keyword'
36-
})
37-
.example(chalk.yellow('leetcode list'), 'List all questions')
38-
.example(chalk.yellow('leetcode list -x'), 'Show extra info of questions, e.g. tags')
39-
.example('', '')
40-
.example(chalk.yellow('leetcode list array'), 'List questions that has "array" in name')
41-
.example(chalk.yellow('leetcode list -q eD'), 'List questions that with easy level and not done')
42-
.example(chalk.yellow('leetcode list -t google'), 'List questions from Google company (require plugin)')
43-
.example(chalk.yellow('leetcode list -t stack'), 'List questions realted to stack (require plugin)')
44-
.example(chalk.yellow('leetcode list -c lcof'), 'List questions from category (require plugin)');
17+
.option('q', core.filters.query)
18+
.option('s', {
19+
alias: 'stat',
20+
type: 'boolean',
21+
default: false,
22+
describe: 'Show statistics of listed questions'
23+
})
24+
.option('t', core.filters.tag)
25+
.option('x', {
26+
alias: 'extra',
27+
type: 'boolean',
28+
default: false,
29+
describe: 'Show extra details: category, companies, tags.'
30+
})
31+
.option('T', {
32+
alias: 'dontTranslate',
33+
type: 'boolean',
34+
default: false,
35+
describe: 'Set to true to disable endpoint\'s translation',
36+
})
37+
.option('c', core.filters.category)
38+
.positional('keyword', {
39+
type: 'string',
40+
default: '',
41+
describe: 'Filter questions by keyword'
42+
})
43+
.example(chalk.yellow('leetcode list'), 'List all questions')
44+
.example(chalk.yellow('leetcode list -x'), 'Show extra info of questions, e.g. tags')
45+
.example('', '')
46+
.example(chalk.yellow('leetcode list array'), 'List questions that has "array" in name')
47+
.example(chalk.yellow('leetcode list -q eD'), 'List questions that with easy level and not done')
48+
.example(chalk.yellow('leetcode list -t google'), 'List questions from Google company (require plugin)')
49+
.example(chalk.yellow('leetcode list -t stack'), 'List questions realted to stack (require plugin)')
50+
.example(chalk.yellow('leetcode list -c lcof'), 'List questions from category (require plugin)');
4551
}
4652
};
4753

lib/commands/show.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ const cmd = {
5757
default: false,
5858
describe: 'Show extra question details in source code'
5959
})
60+
.option('T', {
61+
alias: 'dontTranslate',
62+
type: 'boolean',
63+
default: false,
64+
describe: 'Set to true to disable endpoint\'s translation',
65+
})
6066
.positional('keyword', {
6167
type: 'string',
6268
default: '',
@@ -175,7 +181,7 @@ cmd.handler = function(argv) {
175181
session.argv = argv;
176182
if (argv.keyword.length > 0) {
177183
// show specific one
178-
core.getProblem(argv.keyword, function(e, problem) {
184+
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
179185
if (e) return log.fail(e);
180186
showProblem(problem, argv);
181187
});
@@ -194,7 +200,7 @@ cmd.handler = function(argv) {
194200
if (problems.length === 0) return log.fail('Problem not found!');
195201

196202
const problem = _.sample(problems);
197-
core.getProblem(problem, function(e, problem) {
203+
core.getProblem(problem, !argv.dontTranslate, function(e, problem) {
198204
if (e) return log.fail(e);
199205
showProblem(problem, argv);
200206
});

lib/commands/star.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const cmd = {
2929

3030
cmd.handler = function(argv) {
3131
session.argv = argv;
32-
core.getProblem(argv.keyword, function(e, problem) {
32+
// translation doesn't affect question lookup
33+
core.getProblem(argv.keyword, true, function(e, problem) {
3334
if (e) return log.fail(e);
3435

3536
core.starProblem(problem, !argv.delete, function(e, starred) {

lib/commands/submission.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const cmd = {
4242
default: false,
4343
describe: 'Show extra question details in submission code'
4444
})
45+
.option('T', {
46+
alias: 'dontTranslate',
47+
type: 'boolean',
48+
default: false,
49+
describe: 'Set to true to disable endpoint\'s translation',
50+
})
4551
.positional('keyword', {
4652
type: 'string',
4753
default: '',
@@ -69,7 +75,7 @@ function doTask(problem, queue, cb) {
6975

7076
if (argv.extra) {
7177
// have to get problem details, e.g. problem description.
72-
core.getProblem(problem.fid, function(e, problem) {
78+
core.getProblem(problem.fid, !argv.dontTranslate, function(e, problem) {
7379
if (e) return cb(e);
7480
exportSubmission(problem, argv, onTaskDone);
7581
});
@@ -135,7 +141,7 @@ cmd.handler = function(argv) {
135141
if (!argv.keyword)
136142
return log.fail('missing keyword?');
137143

138-
core.getProblem(argv.keyword, function(e, problem) {
144+
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
139145
if (e) return log.fail(e);
140146
q.addTask(problem).run();
141147
});

lib/commands/submit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ cmd.handler = function(argv) {
4949

5050
const meta = file.meta(argv.filename);
5151

52-
core.getProblem(meta.id, function(e, problem) {
52+
// translation doesn't affect problem lookup
53+
core.getProblem(meta.id, true, function(e, problem) {
5354
if (e) return log.fail(e);
5455

5556
problem.file = argv.filename;

lib/commands/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function runTest(argv) {
6060

6161
const meta = file.meta(argv.filename);
6262

63-
core.getProblem(meta.id, function(e, problem) {
63+
core.getProblem(meta.id, true, function(e, problem) {
6464
if (e) return log.fail(e);
6565

6666
if (!problem.testable)

lib/core.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const QUERY_HANDLERS = {
6767
};
6868

6969
core.filterProblems = function(opts, cb) {
70-
this.getProblems(function(e, problems) {
70+
this.getProblems(!opts.dontTranslate, function(e, problems) {
7171
if (e) return cb(e);
7272
for (const q of (opts.query || '').split('')) {
7373
const f = QUERY_HANDLERS[q];
@@ -93,11 +93,11 @@ core.filterProblems = function(opts, cb) {
9393
});
9494
};
9595

96-
core.getProblem = function(keyword, cb) {
96+
core.getProblem = function(keyword, needTranslation, cb) {
9797
if (keyword.id)
98-
return core.next.getProblem(keyword, cb);
98+
return core.next.getProblem(keyword, needTranslation, cb);
9999

100-
this.getProblems(function(e, problems) {
100+
this.getProblems(needTranslation, function(e, problems) {
101101
if (e) return cb(e);
102102

103103
keyword = Number(keyword) || keyword;
@@ -106,7 +106,7 @@ core.getProblem = function(keyword, cb) {
106106
return x.id + '' === keyword + '' || x.fid + '' === keyword + '' || x.fid + '' === metaFid + '' || x.name === keyword || x.slug === keyword;
107107
});
108108
if (!problem) return cb('Problem not found!');
109-
core.next.getProblem(problem, cb);
109+
core.next.getProblem(problem, needTranslation, cb);
110110
});
111111
};
112112

lib/helper.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ const LANGS = [
5252
const h = {};
5353

5454
h.KEYS = {
55-
user: '../user',
56-
stat: '../stat',
57-
plugins: '../../plugins',
58-
problems: 'problems',
59-
problem: p => p.fid + '.' + p.slug + '.' + p.category
55+
user: '../user',
56+
stat: '../stat',
57+
plugins: '../../plugins',
58+
problems: 'problems',
59+
translation: 'translationConfig',
60+
problem: p => p.id + '.' + p.slug + '.' + p.category
6061
};
6162

6263
h.prettyState = function(state) {

lib/plugins/cache.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,37 @@ var session = require('../session');
99

1010
const plugin = new Plugin(50, 'cache', '', 'Plugin to provide local cache.');
1111

12-
plugin.getProblems = function(cb) {
12+
// this function will clear all caches if needTranslation is different than stored
13+
// it will also store the new needTranslation into cache automatically
14+
function clearCacheIfTchanged(needTranslation) {
15+
const translationConfig = cache.get(h.KEYS.translation);
16+
if (!translationConfig || translationConfig['useEndpointTranslation'] != needTranslation) {
17+
// cache doesn't have the key => old cache version, need to update
18+
// or cache does have the key but it contains a different value
19+
cache.deleteAll();
20+
cache.set(h.KEYS.translation, { useEndpointTranslation: needTranslation });
21+
log.debug('cache cleared: -T option changed');
22+
}
23+
}
24+
25+
plugin.getProblems = function (needTranslation, cb) {
26+
clearCacheIfTchanged(needTranslation);
1327
const problems = cache.get(h.KEYS.problems);
1428
if (problems) {
1529
log.debug('cache hit: problems.json');
1630
return cb(null, problems);
1731
}
1832

19-
plugin.next.getProblems(function(e, problems) {
33+
plugin.next.getProblems(needTranslation, function(e, problems) {
2034
if (e) return cb(e);
2135

2236
cache.set(h.KEYS.problems, problems);
2337
return cb(null, problems);
2438
});
2539
};
2640

27-
plugin.getProblem = function(problem, cb) {
41+
plugin.getProblem = function (problem, needTranslation, cb) {
42+
clearCacheIfTchanged(needTranslation);
2843
const k = h.KEYS.problem(problem);
2944
const _problem = cache.get(k);
3045
if (_problem) {
@@ -42,7 +57,7 @@ plugin.getProblem = function(problem, cb) {
4257
}
4358
}
4459

45-
plugin.next.getProblem(problem, function(e, _problem) {
60+
plugin.next.getProblem(problem, needTranslation, function(e, _problem) {
4661
if (e) return cb(e);
4762

4863
plugin.saveProblem(_problem);

0 commit comments

Comments
 (0)