forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetCodeManager.ts
118 lines (105 loc) · 4.69 KB
/
leetCodeManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict";
import * as cp from "child_process";
import { EventEmitter } from "events";
import * as vscode from "vscode";
import { UserStatus } from "./shared";
import { leetCodeBinaryPath } from "./shared";
import { executeCommand } from "./utils/cpUtils";
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
export interface ILeetCodeManager extends EventEmitter {
getLoginStatus(channel: vscode.OutputChannel): void;
getStatus(): UserStatus;
getUser(): string | undefined;
signIn(channel: vscode.OutputChannel): void;
signOut(channel: vscode.OutputChannel): void;
}
class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
private currentUser: string | undefined;
private userStatus: UserStatus;
constructor() {
super();
this.currentUser = undefined;
this.userStatus = UserStatus.SignedOut;
}
public async getLoginStatus(channel: vscode.OutputChannel): Promise<void> {
try {
const result = await executeCommand(channel, "node", [leetCodeBinaryPath, "user"]);
this.currentUser = result.slice("You are now login as".length).trim();
this.userStatus = UserStatus.SignedIn;
} catch (error) {
this.currentUser = undefined;
this.userStatus = UserStatus.SignedOut;
} finally {
this.emit("statusChanged");
}
}
public async signIn(channel: vscode.OutputChannel): Promise<void> {
try {
const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => {
let result: string = "";
const childProc: cp.ChildProcess = cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
result = result.concat(data);
channel.append(data);
});
childProc.stderr.on("data", (data: string | Buffer) => channel.append(data.toString()));
childProc.on("error", reject);
const name: string | undefined = await vscode.window.showInputBox({
prompt: "Enter user name.",
validateInput: (s: string) => s && s.trim() ? undefined : "User name must not be empty",
});
if (!name) {
childProc.kill();
return resolve(undefined);
}
childProc.stdin.write(`${name}\n`);
const pwd: string | undefined = await vscode.window.showInputBox({
prompt: "Enter password.",
password: true,
validateInput: (s: string) => s ? undefined : "Password must not be empty",
});
if (!pwd) {
childProc.kill();
return resolve(undefined);
}
childProc.stdin.write(`${pwd}\n`);
childProc.stdin.end();
childProc.on("close", () => {
const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully login as (.*)/i);
if (match && match[1]) {
resolve(match[1]);
} else {
reject(new Error("Failed to sigin in."));
}
});
});
if (userName) {
vscode.window.showInformationMessage("Successfully signed in.");
this.currentUser = userName;
this.userStatus = UserStatus.SignedIn;
this.emit("statusChanged");
}
} catch (error) {
promptForOpenOutputChannel("Failed to sign in. Please open the output channel for details", DialogType.error, channel);
}
}
public async signOut(channel: vscode.OutputChannel): Promise<void> {
try {
await executeCommand(channel, "node", [leetCodeBinaryPath, "user", "-L"]);
vscode.window.showInformationMessage("Successfully signed out.");
this.currentUser = undefined;
this.userStatus = UserStatus.SignedOut;
this.emit("statusChanged");
} catch (error) {
promptForOpenOutputChannel("Failed to sign out. Please open the output channel for details", DialogType.error, channel);
}
}
public getStatus(): UserStatus {
return this.userStatus;
}
public getUser(): string | undefined {
return this.currentUser;
}
}
export const leetCodeManager: ILeetCodeManager = new LeetCodeManager();