Skip to content

Commit 402e912

Browse files
authored
Dev (#38)
* update new cli * fix error * fix error * fix * fix error * fix error * fix error * fix error * fix error * fix error * fix error * fix error * fix error * fix error * update * fix error
1 parent 824d45e commit 402e912

33 files changed

+5633
-11018
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## version 1.1.1
2+
- 重新写原本的客户端
3+
- 减少许多多余的包引用
4+
15
## version 1.0.24
26
- 增加314期数据
37

package-lock.json

Lines changed: 1414 additions & 10998 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode problem rating",
44
"description": "为LeetCode题目难度进行打分。避免只有简单、中等、困难三种难度",
5-
"version": "1.0.24",
5+
"version": "1.1.1",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",
@@ -766,20 +766,12 @@
766766
"@types/fs-extra": "^9.0.11",
767767
"@types/lodash": "^4.14.170",
768768
"@types/markdown-it": "0.0.7",
769-
"@types/mocha": "^2.2.42",
770769
"@types/node": "^14.14.33",
771770
"@types/require-from-string": "^1.2.0",
772771
"@types/vscode": "1.57.0",
773772
"tslint": "^5.20.1",
774773
"typescript": "^4.3.2",
775-
"chai": "4.2.0",
776-
"eslint": "8.24.0",
777-
"eslint-config-google": "0.14.0",
778-
"mocha": "^8.3.2",
779-
"nock": "10.0.2",
780-
"nyc": "^15.1.0",
781-
"pkg": "^4.5.1",
782-
"rewire": "4.0.1"
774+
"chai": "4.2.0"
783775
},
784776
"dependencies": {
785777
"fs-extra": "^10.0.0",
@@ -797,9 +789,7 @@
797789
"ora": "3.0.0",
798790
"prompt": "^1.2.0",
799791
"request": "2.88.0",
800-
"supports-color": "5.5.0",
801792
"underscore": "1.12.1",
802-
"wordwrap": "1.0.0",
803-
"yargs": "^15.4.1"
793+
"wordwrap": "1.0.0"
804794
}
805795
}

resources/bin/leetcode

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env node
22

3-
require('../../out/src/vsc-leetcode-cli/lib/cli').run();
3+
require('../../out/src/vsc-leetcode-cli/new_lib/cli');

src/leetCodeManager.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class LeetCodeManager extends EventEmitter {
3434
const result: string = await leetCodeExecutor.getUserInfo();
3535
this.currentUser = this.tryParseUserName(result);
3636
this.userStatus = UserStatus.SignedIn;
37+
if (this.currentUser == undefined) {
38+
this.userStatus = UserStatus.SignedOut;
39+
}
3740
} catch (error) {
3841
this.currentUser = undefined;
3942
this.userStatus = UserStatus.SignedOut;
@@ -82,13 +85,15 @@ class LeetCodeManager extends EventEmitter {
8285

8386
const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath();
8487

85-
const childProc: cp.ChildProcess = wsl.useWsl()
86-
? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true })
87-
: cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
88+
var childProc: cp.ChildProcess;
89+
if (wsl.useWsl()) {
90+
childProc = cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true })
91+
} else {
92+
childProc = cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
8893
shell: true,
8994
env: createEnvOption(),
9095
});
91-
96+
}
9297
childProc.stdout?.on("data", async (data: string | Buffer) => {
9398
data = data.toString();
9499
// vscode.window.showInformationMessage(`cc login msg ${data}.`);
@@ -191,7 +196,7 @@ class LeetCodeManager extends EventEmitter {
191196
return this.currentUser;
192197
}
193198

194-
private tryParseUserName(output: string): string {
199+
private tryParseUserName(output: string): string | undefined {
195200
var successMatch;
196201
try {
197202
successMatch = JSON.parse(output);
@@ -201,7 +206,7 @@ class LeetCodeManager extends EventEmitter {
201206
if (successMatch.code == 100) {
202207
return successMatch.user_name;
203208
}
204-
return "Unknown";
209+
return undefined;
205210
}
206211
}
207212

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var path = require('path');
2+
3+
import { file } from "./file";
4+
5+
6+
class Cache {
7+
public init() {
8+
file.mkdir(file.cacheDir());
9+
};
10+
11+
public deleteAll() {
12+
this.list().forEach(value => {
13+
this.del(value.name);
14+
})
15+
};
16+
17+
public get(k) {
18+
const fullpath = file.cacheFile(k);
19+
if (!file.exist(fullpath)) return null;
20+
21+
return JSON.parse(file.data(fullpath));
22+
};
23+
24+
public set(k, v) {
25+
const fullpath = file.cacheFile(k);
26+
file.write(fullpath, JSON.stringify(v));
27+
return true;
28+
};
29+
30+
public del(k) {
31+
const fullpath = file.cacheFile(k);
32+
if (!file.exist(fullpath)) return false;
33+
34+
file.rm(fullpath);
35+
return true;
36+
};
37+
38+
public list(): Array<any> {
39+
return file.list(file.cacheDir())
40+
.filter(x => path.extname(x) === '.json')
41+
.map(function (filename) {
42+
const k = path.basename(filename, '.json');
43+
const stat = file.stat(file.cacheFile(k));
44+
return {
45+
name: k,
46+
size: stat.size,
47+
mtime: stat.mtime
48+
};
49+
});
50+
};
51+
52+
}
53+
54+
export const cache: Cache = new Cache();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
// var h = require('./helper');
3+
4+
5+
import { myPluginBase } from "./my_plugin_base";
6+
import { config } from "./config";
7+
import { log } from "./log";
8+
import { file } from "./file";
9+
class NewCli {
10+
constructor() {
11+
this.run()
12+
}
13+
public run() {
14+
process.stdout.on('error', function (e) {
15+
if (e.code === 'EPIPE') process.exit();
16+
});
17+
config.init();
18+
this.initLogLevel();
19+
this.initDir()
20+
this.initPlugins((e) => {
21+
if (e) return log.fatal(e);
22+
require('./cache').cache.init();
23+
this.runCommand_new();
24+
return;
25+
});
26+
};
27+
private initLogLevel() {
28+
log.init();
29+
}
30+
31+
private initDir() {
32+
file.init();
33+
file.mkdir(file.homeDir())
34+
}
35+
36+
private initPlugins(cb) {
37+
if (myPluginBase.base_init()) {
38+
myPluginBase.save();
39+
return cb();
40+
}
41+
}
42+
43+
private runCommand_new() {
44+
var com_str = process.argv[2]
45+
var auto_js = require("./commands/" + com_str)[com_str + "Command"]
46+
auto_js.handler(auto_js.process_argv(process.argv))
47+
}
48+
}
49+
50+
51+
export const newCli: NewCli = new NewCli();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var underscore = require('underscore');
2+
3+
4+
import { helper } from "../helper";
5+
import { log } from "../log";
6+
import { cache } from "../cache";
7+
import { session } from "../session";
8+
9+
class CacheCommand {
10+
constructor() {
11+
}
12+
13+
process_argv = function (argv) {
14+
var argv_config = helper.base_argv().option('d', {
15+
alias: 'delete',
16+
type: 'boolean',
17+
describe: 'Delete cache by keyword',
18+
default: false
19+
})
20+
.positional('keyword', {
21+
type: 'string',
22+
describe: 'Cache name or question id',
23+
default: ''
24+
})
25+
argv_config.process_argv(argv)
26+
27+
return argv_config.get_result()
28+
}
29+
30+
31+
handler = function (argv) {
32+
session.argv = argv;
33+
34+
const name = argv.keyword;
35+
const isInteger = Number.isInteger(Number(name));
36+
37+
const caches = cache.list()
38+
.filter(function (f) {
39+
return (name.length === 0) ||
40+
(isInteger ? f.name.startsWith(name + '.') : f.name === name);
41+
});
42+
43+
if (argv.delete) {
44+
for (let f of caches) cache.del(f.name);
45+
} else {
46+
log.info(' %s %63s %s', 'Cache', 'Size', 'Created');
47+
log.info('-'.repeat(86));
48+
49+
underscore.sortBy(caches, function (f) {
50+
let x = parseInt(f.name.split('.')[0], 10);
51+
if (Number.isNaN(x)) x = 0;
52+
return x;
53+
})
54+
.forEach(function (f) {
55+
log.info(' %-60s %8s %s ago',
56+
f.name,
57+
helper.prettySize(f.size),
58+
helper.prettyTime((Date.now() - f.mtime) / 1000));
59+
});
60+
}
61+
};
62+
}
63+
64+
65+
66+
export const cacheCommand: CacheCommand = new CacheCommand();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
var underscore = require('underscore');
2+
var nconf = require('nconf');
3+
4+
5+
import { config } from "../config";
6+
import { log } from "../log";
7+
import { file } from "../file";
8+
import { session } from "../session";
9+
import { helper } from "../helper";
10+
11+
class ConfigCommand {
12+
constructor() {
13+
14+
}
15+
16+
17+
process_argv(argv) {
18+
var argv_config = helper.base_argv().option('a', {
19+
alias: 'all',
20+
type: 'boolean',
21+
describe: 'Show all config',
22+
default: false
23+
})
24+
.option('d', {
25+
alias: 'delete',
26+
type: 'boolean',
27+
describe: 'Delete config by key',
28+
default: false
29+
})
30+
.positional('key', {
31+
type: 'string',
32+
describe: 'Config key, delimited by colon',
33+
default: ''
34+
})
35+
.positional('value', {
36+
type: 'string',
37+
describe: 'Config value',
38+
default: ''
39+
})
40+
argv_config.process_argv(argv)
41+
42+
return argv_config.get_result()
43+
}
44+
45+
46+
prettyConfig(cfg) {
47+
return JSON.stringify(cfg, null, 2);
48+
}
49+
50+
loadConfig(showall) {
51+
const cfg = showall ? config.getAll(true) : nconf.get();
52+
return underscore.omit(cfg, 'type');
53+
}
54+
55+
saveConfig() {
56+
file.write(file.configFile(), this.prettyConfig(this.loadConfig(false)));
57+
}
58+
59+
handler(argv) {
60+
session.argv = argv;
61+
nconf.file('local', file.configFile());
62+
63+
// show all
64+
if (argv.key.length === 0)
65+
return log.info(this.prettyConfig(this.loadConfig(argv.all)));
66+
67+
68+
const v = nconf.get(argv.key);
69+
70+
// delete
71+
if (argv.delete) {
72+
if (v === undefined) return log.fatal('Key not found: ' + argv.key);
73+
nconf.clear(argv.key);
74+
return this.saveConfig();
75+
}
76+
77+
// show
78+
if (argv.value.length === 0) {
79+
if (v === undefined) return log.fatal('Key not found: ' + argv.key);
80+
return log.info(this.prettyConfig(v));
81+
}
82+
83+
// set
84+
try {
85+
nconf.set(argv.key, JSON.parse(argv.value));
86+
} catch (e) {
87+
nconf.set(argv.key, JSON.parse('"' + argv.value + '"'));
88+
}
89+
return this.saveConfig();
90+
};
91+
}
92+
93+
94+
95+
96+
export const configCommand: ConfigCommand = new ConfigCommand();
97+

0 commit comments

Comments
 (0)