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