Skip to content

Commit 9017eda

Browse files
authored
Refine the result parsing logic (LeetCode-OpenSource#501)
1 parent c20a2d5 commit 9017eda

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

package-lock.json

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,6 @@
683683
"markdown-it": "^8.4.2",
684684
"require-from-string": "^2.0.2",
685685
"unescape-js": "^1.1.1",
686-
"vsc-leetcode-cli": "2.6.20"
686+
"vsc-leetcode-cli": "2.6.22"
687687
}
688688
}

src/leetCodeManager.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import * as wsl from "./utils/wslUtils";
1414
class LeetCodeManager extends EventEmitter {
1515
private currentUser: string | undefined;
1616
private userStatus: UserStatus;
17+
private readonly successRegex: RegExp = /(?:.*)Successfully .*login as (.*)/i;
18+
private readonly failRegex: RegExp = /.*\[ERROR\].*/i;
1719

1820
constructor() {
1921
super();
@@ -42,11 +44,6 @@ class LeetCodeManager extends EventEmitter {
4244
detail: "Use LeetCode account to login",
4345
value: "LeetCode",
4446
},
45-
{
46-
label: "LeetCode Cookie",
47-
detail: "Use LeetCode cookie copied from browser to login",
48-
value: "Cookie",
49-
},
5047
{
5148
label: "Third-Party: GitHub",
5249
detail: "Use GitHub account to login",
@@ -57,6 +54,11 @@ class LeetCodeManager extends EventEmitter {
5754
detail: "Use LinkedIn account to login",
5855
value: "LinkedIn",
5956
},
57+
{
58+
label: "LeetCode Cookie",
59+
detail: "Use LeetCode cookie copied from browser to login",
60+
value: "Cookie",
61+
},
6062
);
6163
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
6264
if (!choice) {
@@ -87,20 +89,22 @@ class LeetCodeManager extends EventEmitter {
8789
if (data.includes("twoFactorCode")) {
8890
const twoFactor: string | undefined = await vscode.window.showInputBox({
8991
prompt: "Enter two-factor code.",
92+
ignoreFocusOut: true,
9093
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "The input must not be empty",
9194
});
9295
if (!twoFactor) {
9396
childProc.kill();
9497
return resolve(undefined);
9598
}
9699
childProc.stdin.write(`${twoFactor}\n`);
100+
}
101+
const successMatch: RegExpMatchArray | null = data.match(this.successRegex);
102+
if (successMatch && successMatch[1]) {
97103
childProc.stdin.end();
98-
} else {
99-
const match: RegExpMatchArray | null = data.match(/(?:.*)Successfully .*login as (.*)/i);
100-
if (match && match[1]) {
101-
childProc.stdin.end();
102-
return resolve(match[1]);
103-
}
104+
return resolve(successMatch[1]);
105+
} else if (data.match(this.failRegex)) {
106+
childProc.stdin.end();
107+
return reject(new Error("Faile to login"));
104108
}
105109
});
106110

@@ -109,6 +113,7 @@ class LeetCodeManager extends EventEmitter {
109113
childProc.on("error", reject);
110114
const name: string | undefined = await vscode.window.showInputBox({
111115
prompt: "Enter username or E-mail.",
116+
ignoreFocusOut: true,
112117
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "The input must not be empty",
113118
});
114119
if (!name) {
@@ -119,18 +124,14 @@ class LeetCodeManager extends EventEmitter {
119124
const pwd: string | undefined = await vscode.window.showInputBox({
120125
prompt: isByCookie ? "Enter cookie" : "Enter password.",
121126
password: true,
127+
ignoreFocusOut: true,
122128
validateInput: (s: string): string | undefined => s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty",
123129
});
124130
if (!pwd) {
125131
childProc.kill();
126132
return resolve(undefined);
127133
}
128134
childProc.stdin.write(`${pwd}\n`);
129-
childProc.on("close", (code: number) => {
130-
if (code !== 0) {
131-
reject(new Error("Failed to login."));
132-
}
133-
});
134135
});
135136
if (userName) {
136137
vscode.window.showInformationMessage(`Successfully ${inMessage}.`);

0 commit comments

Comments
 (0)