8000 update · thebrownbox/vscode-leetcode@ce58950 · GitHub
[go: up one dir, main page]

Skip to content
7FFF

Commit ce58950

Browse files
author
Cong Hoang
committed
update
1 parent 754c8b9 commit ce58950

File tree

2 files changed

+53
-95
lines changed

2 files changed

+53
-95
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"tabWidth": 4
2+
"tabWidth": 4,
3+
"printWidth": 120
34
}

src/leetCodeManager.ts

Lines changed: 51 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ 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 =
18-
/(?:.*)Successfully .*login as (.*)/i;
17+
private readonly successRegex: RegExp = /(?:.*)Successfully .*login as (.*)/i;
1918
private readonly failRegex: RegExp = /.*\[ERROR\].*/i;
2019

2120
constructor() {
@@ -62,118 +61,78 @@ class LeetCodeManager extends EventEmitter {
6261
value: "Cookie",
6362
}
6463
);
65-
const choice: IQuickItemEx<string> | undefined =
66-
await vscode.window.showQuickPick(picks);
64+
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
6765
if (!choice) {
6866
return;
6967
}
7068
const loginMethod: string = choice.value;
71-
const commandArg: string | undefined =
72-
loginArgsMapping.get(loginMethod);
69+
const commandArg: string | undefined = loginArgsMapping.get(loginMethod);
7370
if (!commandArg) {
74-
throw new Error(
75-
`The login method "${loginMethod}" is not supported.`
76-
);
71+
throw new Error(`The login method "${loginMethod}" is not supported.`);
7772
}
7873
const isByCookie: boolean = loginMethod === "Cookie";
7974
const inMessage: string = isByCookie ? "sign in by cookie" : "sign in";
8075
try {
8176
const userName: string | undefined = await new Promise(
82-
async (
83-
resolve: (res: string | undefined) => void,
84-
reject: (e: Error) => void
85-
): Promise<void> => {
86-
const leetCodeBinaryPath: string =
87-
await leetCodeExecutor.getLeetCodeBinaryPath();
77+
async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => {
78+
const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath();
8879

8980
const childProc: cp.ChildProcess = wsl.useWsl()
90-
? cp.spawn(
91-
"wsl",
92-
[
93-
leetCodeExecutor.node,
94-
leetCodeBinaryPath,
95-
"user",
96-
commandArg,
97-
],
98-
{ shell: true }
99-
)
100-
: cp.spawn(
101-
leetCodeExecutor.node,
102-
[leetCodeBinaryPath, "user", commandArg],
103-
{
104-
shell: true,
105-
env: createEnvOption(),
106-
}
107-
);
81+
? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], {
82+
shell: true,
83+
})
84+
: cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
85+
shell: true,
86+
env: createEnvOption(),
87+
});
10888

109-
childProc.stdout?.on(
110-
"data",
111-
async (data: string | Buffer) => {
112-
data = data.toString();
113-
leetCodeChannel.append(data);
114-
if (data.includes("twoFactorCode")) {
115-
const twoFactor: string | undefined =
116-
await vscode.window.showInputBox({
117-
prompt: "Enter two-factor code.",
118-
ignoreFocusOut: true,
119-
validateInput: (
120-
s: string
121-
): string | undefined =>
122-
s && s.trim()
123-
? undefined
124-
: "The input must not be empty",
125-
});
126-
if (!twoFactor) {
127-
childProc.kill();
128-
return resolve(undefined);
129-
}
130-
childProc.stdin?.write(`${twoFactor}\n`);
131-
}
132-
const successMatch: RegExpMatchArray | null =
133-
data.match(this.successRegex);
134-
if (successMatch && successMatch[1]) {
135-
childProc.stdin?.end();
136-
return resolve(successMatch[1]);
137-
} else if (data.match(this.failRegex)) {
138-
childProc.stdin?.end();
139-
return reject(new Error("Faile to login"));
89+
childProc.stdout?.on("data", async (data: string | Buffer) => {
90+
data = data.toString();
91+
leetCodeChannel.append(data);
92+
if (data.includes("twoFactorCode")) {
93+
const twoFactor: string | undefined = await vscode.window.showInputBox({
94+
prompt: "Enter two-factor code.",
95+
ignoreFocusOut: true,
96+
validateInput: (s: string): string | undefined =>
97+
s && s.trim() ? undefined : "The input must not be empty",
98+
});
99+
if (!twoFactor) {
100+
childProc.kill();
101+
return resolve(undefined);
140102
}
103+
childProc.stdin?.write(`${twoFactor}\n`);
104+
}
105+
const successMatch: RegExpMatchArray | null = data.match(this.successRegex);
106+
if (successMatch && successMatch[1]) {
107+
childProc.stdin?.end();
108+
return resolve(successMatch[1]);
109+
} else if (data.match(this.failRegex)) {
110+
childProc.stdin?.end();
111+
return reject(new Error("Faile to login"));
141112
}
142-
);
113+
});
143114

144-
childProc.stderr?.on("data", (data: string | Buffer) =>
145-
leetCodeChannel.append(data.toString())
146-
);
115+
childProc.stderr?.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString()));
147116

148117
childProc.on("error", reject);
149-
const name: string | undefined =
150-
await vscode.window.showInputBox({
151-
prompt: "Enter username or E-mail.",
152-
ignoreFocusOut: true,
153-
validateInput: (s: string): string | undefined =>
154-
s && s.trim()
155-
? undefined
156-
: "The input must not be empty",
157-
});
118+
const name: string | undefined = await vscode.window.showInputBox({
119+
prompt: "Enter username or E-mail.",
120+
ignoreFocusOut: true,
121+
validateInput: (s: string): string | undefined =>
122+
s && s.trim() ? undefined : "The input must not be empty",
123+
});
158124
if (!name) {
159125
childProc.kill();
160126
return resolve(undefined);
161127
}
162128
childProc.stdin?.write(`${name}\n`);
163-
const pwd: string | undefined =
164-
await vscode.window.showInputBox({
165-
prompt: isByCookie
166-
? "Enter cookie"
167-
: "Enter password.",
168-
password: true,
169-
ignoreFocusOut: true,
170-
validateInput: (s: string): string | undefined =>
171-
s
172-
? undefined
173-
: isByCookie
174-
? "Cookie must not be empty"
175-
: "Password must not be empty",
176-
});
129+
const pwd: string | undefined = await vscode.window.showInputBox({
130+
prompt: isByCookie ? "Enter cookie" : "Enter password.",
131+
password: true,
132+
ignoreFocusOut: true,
133+
validateInput: (s: string): string | undefined =>
134+
s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty",
135+
});
177136
if (!pwd) {
178137
childProc.kill();
179138
return resolve(undefined);
@@ -182,9 +141,7 @@ class LeetCodeManager extends EventEmitter {
182141
}
183142
);
184143
if (userName) {
185-
vscode.window.showInformationMessage(
186-
`Successfully ${inMessage}.`
187-
);
144+
vscode.window.showInformationMessage(`Successfully ${inMessage}.`);
188145
this.currentUser = userName;
189146
this.userStatus = UserStatus.SignedIn;
190147
this.emit("statusChanged");

0 commit comments

Comments
 (0)
0