Skip to content

Refine the result parsing logic #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2020
Merged
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
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,6 @@
"markdown-it": "^8.4.2",
"require-from-string": "^2.0.2",
"unescape-js": "^1.1.1",
"vsc-leetcode-cli": "2.6.20"
"vsc-leetcode-cli": "2.6.22"
}
}
33 changes: 17 additions & 16 deletions src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import * as wsl from "./utils/wslUtils";
class LeetCodeManager extends EventEmitter {
private currentUser: string | undefined;
private userStatus: UserStatus;
private readonly successRegex: RegExp = /(?:.*)Successfully .*login as (.*)/i;
private readonly failRegex: RegExp = /.*\[ERROR\].*/i;

constructor() {
super();
Expand Down Expand Up @@ -42,11 +44,6 @@ class LeetCodeManager extends EventEmitter {
detail: "Use LeetCode account to login",
value: "LeetCode",
},
{
label: "LeetCode Cookie",
detail: "Use LeetCode cookie copied from browser to login",
value: "Cookie",
},
{
label: "Third-Party: GitHub",
detail: "Use GitHub account to login",
Expand All @@ -57,6 +54,11 @@ class LeetCodeManager extends EventEmitter {
detail: "Use LinkedIn account to login",
value: "LinkedIn",
},
{
label: "LeetCode Cookie",
detail: "Use LeetCode cookie copied from browser to login",
value: "Cookie",
},
);
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
if (!choice) {
Expand Down Expand Up @@ -87,20 +89,22 @@ class LeetCodeManager extends EventEmitter {
if (data.includes("twoFactorCode")) {
const twoFactor: string | undefined = await vscode.window.showInputBox({
prompt: "Enter two-factor code.",
ignoreFocusOut: true,
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "The input must not be empty",
});
if (!twoFactor) {
childProc.kill();
return resolve(undefined);
}
childProc.stdin.write(`${twoFactor}\n`);
}
const successMatch: RegExpMatchArray | null = data.match(this.successRegex);
if (successMatch && successMatch[1]) {
childProc.stdin.end();
} else {
const match: RegExpMatchArray | null = data.match(/(?:.*)Successfully .*login as (.*)/i);
if (match && match[1]) {
childProc.stdin.end();
return resolve(match[1]);
}
return resolve(successMatch[1]);
} else if (data.match(this.failRegex)) {
childProc.stdin.end();
return reject(new Error("Faile to login"));
}
});

Expand All @@ -109,6 +113,7 @@ class LeetCodeManager extends EventEmitter {
childProc.on("error", reject);
const name: string | undefined = await vscode.window.showInputBox({
prompt: "Enter username or E-mail.",
ignoreFocusOut: true,
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "The input must not be empty",
});
if (!name) {
Expand All @@ -119,18 +124,14 @@ class LeetCodeManager extends EventEmitter {
const pwd: string | undefined = await vscode.window.showInputBox({
prompt: isByCookie ? "Enter cookie" : "Enter password.",
password: true,
ignoreFocusOut: true,
validateInput: (s: string): string | undefined => s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty",
});
if (!pwd) {
childProc.kill();
return resolve(undefined);
}
childProc.stdin.write(`${pwd}\n`);
childProc.on("close", (code: number) => {
if (code !== 0) {
reject(new Error("Failed to login."));
}
});
});
if (userName) {
vscode.window.showInformationMessage(`Successfully ${inMessage}.`);
Expand Down