Skip to content
Merged

Dev #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## version 1.1.1
- 重新写原本的客户端
- 减少许多多余的包引用

## version 1.0.24
- 增加314期数据

Expand Down
12,412 changes: 1,414 additions & 10,998 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 3 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-leetcode-problem-rating",
"displayName": "LeetCode problem rating",
"description": "为LeetCode题目难度进行打分。避免只有简单、中等、困难三种难度",
"version": "1.0.24",
"version": "1.1.1",
"author": "ccagml",
"publisher": "ccagml",
"license": "MIT",
Expand Down Expand Up @@ -766,20 +766,12 @@
"@types/fs-extra": "^9.0.11",
"@types/lodash": "^4.14.170",
"@types/markdown-it": "0.0.7",
"@types/mocha": "^2.2.42",
"@types/node": "^14.14.33",
"@types/require-from-string": "^1.2.0",
"@types/vscode": "1.57.0",
"tslint": "^5.20.1",
"typescript": "^4.3.2",
"chai": "4.2.0",
"eslint": "8.24.0",
"eslint-config-google": "0.14.0",
"mocha": "^8.3.2",
"nock": "10.0.2",
"nyc": "^15.1.0",
"pkg": "^4.5.1",
"rewire": "4.0.1"
"chai": "4.2.0"
},
"dependencies": {
"fs-extra": "^10.0.0",
Expand All @@ -797,9 +789,7 @@
"ora": "3.0.0",
"prompt": "^1.2.0",
"request": "2.88.0",
"supports-color": "5.5.0",
"underscore": "1.12.1",
"wordwrap": "1.0.0",
"yargs": "^15.4.1"
"wordwrap": "1.0.0"
}
}
2 changes: 1 addition & 1 deletion resources/bin/leetcode
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require('../../out/src/vsc-leetcode-cli/lib/cli').run();
require('../../out/src/vsc-leetcode-cli/new_lib/cli');
17 changes: 11 additions & 6 deletions src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class LeetCodeManager extends EventEmitter {
const result: string = await leetCodeExecutor.getUserInfo();
this.currentUser = this.tryParseUserName(result);
this.userStatus = UserStatus.SignedIn;
if (this.currentUser == undefined) {
this.userStatus = UserStatus.SignedOut;
}
} catch (error) {
this.currentUser = undefined;
this.userStatus = UserStatus.SignedOut;
Expand Down Expand Up @@ -82,13 +85,15 @@ class LeetCodeManager extends EventEmitter {

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

const childProc: cp.ChildProcess = wsl.useWsl()
? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true })
: cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
var childProc: cp.ChildProcess;
if (wsl.useWsl()) {
childProc = cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true })
} else {
childProc = cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
shell: true,
env: createEnvOption(),
});

}
childProc.stdout?.on("data", async (data: string | Buffer) => {
data = data.toString();
// vscode.window.showInformationMessage(`cc login msg ${data}.`);
Expand Down Expand Up @@ -191,7 +196,7 @@ class LeetCodeManager extends EventEmitter {
return this.currentUser;
}

private tryParseUserName(output: string): string {
private tryParseUserName(output: string): string | undefined {
var successMatch;
try {
successMatch = JSON.parse(output);
Expand All @@ -201,7 +206,7 @@ class LeetCodeManager extends EventEmitter {
if (successMatch.code == 100) {
return successMatch.user_name;
}
return "Unknown";
return undefined;
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/vsc-leetcode-cli/new_lib/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var path = require('path');

import { file } from "./file";


class Cache {
public init() {
file.mkdir(file.cacheDir());
};

public deleteAll() {
this.list().forEach(value => {
this.del(value.name);
})
};

public get(k) {
const fullpath = file.cacheFile(k);
if (!file.exist(fullpath)) return null;

return JSON.parse(file.data(fullpath));
};

public set(k, v) {
const fullpath = file.cacheFile(k);
file.write(fullpath, JSON.stringify(v));
return true;
};

public del(k) {
const fullpath = file.cacheFile(k);
if (!file.exist(fullpath)) return false;

file.rm(fullpath);
return true;
};

public list(): Array<any> {
return file.list(file.cacheDir())
.filter(x => path.extname(x) === '.json')
.map(function (filename) {
const k = path.basename(filename, '.json');
const stat = file.stat(file.cacheFile(k));
return {
name: k,
size: stat.size,
mtime: stat.mtime
};
});
};

}

export const cache: Cache = new Cache();
51 changes: 51 additions & 0 deletions src/vsc-leetcode-cli/new_lib/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

// var h = require('./helper');


import { myPluginBase } from "./my_plugin_base";
import { config } from "./config";
import { log } from "./log";
import { file } from "./file";
class NewCli {
constructor() {
this.run()
}
public run() {
process.stdout.on('error', function (e) {
if (e.code === 'EPIPE') process.exit();
});
config.init();
this.initLogLevel();
this.initDir()
this.initPlugins((e) => {
if (e) return log.fatal(e);
require('./cache').cache.init();
this.runCommand_new();
return;
});
};
private initLogLevel() {
log.init();
}

private initDir() {
file.init();
file.mkdir(file.homeDir())
}

private initPlugins(cb) {
if (myPluginBase.base_init()) {
myPluginBase.save();
return cb();
}
}

private runCommand_new() {
var com_str = process.argv[2]
var auto_js = require("./commands/" + com_str)[com_str + "Command"]
auto_js.handler(auto_js.process_argv(process.argv))
}
}


export const newCli: NewCli = new NewCli();
66 changes: 66 additions & 0 deletions src/vsc-leetcode-cli/new_lib/commands/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var underscore = require('underscore');


import { helper } from "../helper";
import { log } from "../log";
import { cache } from "../cache";
import { session } from "../session";

class CacheCommand {
constructor() {
}

process_argv = function (argv) {
var argv_config = helper.base_argv().option('d', {
alias: 'delete',
type: 'boolean',
describe: 'Delete cache by keyword',
default: false
})
.positional('keyword', {
type: 'string',
describe: 'Cache name or question id',
default: ''
})
argv_config.process_argv(argv)

return argv_config.get_result()
}


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(' %s %63s %s', 'Cache', 'Size', 'Created');
log.info('-'.repeat(86));

underscore.sortBy(caches, function (f) {
let x = parseInt(f.name.split('.')[0], 10);
if (Number.isNaN(x)) x = 0;
return x;
})
.forEach(function (f) {
log.info(' %-60s %8s %s ago',
f.name,
helper.prettySize(f.size),
helper.prettyTime((Date.now() - f.mtime) / 1000));
});
}
};
}



export const cacheCommand: CacheCommand = new CacheCommand();
97 changes: 97 additions & 0 deletions src/vsc-leetcode-cli/new_lib/commands/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
var underscore = require('underscore');
var nconf = require('nconf');


import { config } from "../config";
import { log } from "../log";
import { file } from "../file";
import { session } from "../session";
import { helper } from "../helper";

class ConfigCommand {
constructor() {

}


process_argv(argv) {
var argv_config = helper.base_argv().option('a', {
alias: 'all',
type: 'boolean',
describe: 'Show all config',
default: false
})
.option('d', {
alias: 'delete',
type: 'boolean',
describe: 'Delete config by key',
default: false
})
.positional('key', {
type: 'string',
describe: 'Config key, delimited by colon',
default: ''
})
.positional('value', {
type: 'string',
describe: 'Config value',
default: ''
})
argv_config.process_argv(argv)

return argv_config.get_result()
}


prettyConfig(cfg) {
return JSON.stringify(cfg, null, 2);
}

loadConfig(showall) {
const cfg = showall ? config.getAll(true) : nconf.get();
return underscore.omit(cfg, 'type');
}

saveConfig() {
file.write(file.configFile(), this.prettyConfig(this.loadConfig(false)));
}

handler(argv) {
session.argv = argv;
nconf.file('local', file.configFile());

// show all
if (argv.key.length === 0)
return log.info(this.prettyConfig(this.loadConfig(argv.all)));


const v = nconf.get(argv.key);

// delete
if (argv.delete) {
if (v === undefined) return log.fatal('Key not found: ' + argv.key);
nconf.clear(argv.key);
return this.saveConfig();
}

// show
if (argv.value.length === 0) {
if (v === undefined) return log.fatal('Key not found: ' + argv.key);
return log.info(this.prettyConfig(v));
}

// set
try {
nconf.set(argv.key, JSON.parse(argv.value));
} catch (e) {
nconf.set(argv.key, JSON.parse('"' + argv.value + '"'));
}
return this.saveConfig();
};
}




export const configCommand: ConfigCommand = new ConfigCommand();

Loading