From af76a8b3927c79ebfa1bcd5b486416637ca755e3 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Tue, 3 Dec 2019 16:16:18 +0800 Subject: [PATCH 01/12] add leetcode.cookieIn but simple change --- README.md | 3 ++- package.json | 8 +++++++- src/extension.ts | 1 + src/leetCodeManager.ts | 26 +++++++++++++++----------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 67fb4df0..3e727458 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,9 @@ - Simply click `Sign in to LeetCode` in the `LeetCode Explorer` will let you **sign in** with your LeetCode account. -- You can also use the following command to sign in/out: +- You can also use the following command to sign in/cookie in/out: - **LeetCode: Sign in** + - **LeetCode: Cookie in** - **LeetCode: Sign out** --- diff --git a/package.json b/package.json index 91bc6e59..b14fb6a8 100644 --- a/package.json +++ b/package.json @@ -38,11 +38,17 @@ "onCommand:leetcode.testSolution", "onCommand:leetcode.submitSolution", "onCommand:leetcode.switchDefaultLanguage", + "onCommand:leetcode.cookieIn", "onView:leetCodeExplorer" ], "main": "./out/src/extension", "contributes": { "commands": [ + { + "command": "leetcode.cookieIn", + "title": "Cookie In", + "category": "LeetCode" + }, { "command": "leetcode.deleteCache", "title": "Delete Cache", @@ -683,6 +689,6 @@ "markdown-it": "^8.4.2", "require-from-string": "^2.0.2", "unescape-js": "^1.1.1", - "vsc-leetcode-cli": "2.6.16" + "vsc-leetcode-cli": "2.6.17" } } diff --git a/src/extension.ts b/src/extension.ts index 9bb3ad41..0e7d7224 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -51,6 +51,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()), vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()), vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()), + vscode.commands.registerCommand("leetcode.cookieIn", () => leetCodeManager.signIn(true)), vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()), vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()), vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)), diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 96e3c0b0..1bbcf72f 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -34,7 +34,11 @@ class LeetCodeManager extends EventEmitter { } } - public async signIn(): Promise { + public async signIn(isCookieIn = false): Promise { + const loginArg = "-l"; + const cookieInArg = "-c"; + const commandArg = isCookieIn ? cookieInArg : loginArg; + const inMessage = isCookieIn ? "cookie in" : "sign in" try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { let result: string = ""; @@ -42,8 +46,8 @@ class LeetCodeManager extends EventEmitter { const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath(); const childProc: cp.ChildProcess = wsl.useWsl() - ? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", "-l"], { shell: true }) - : cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", "-l"], { + ? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true }) + : cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], { shell: true, env: createEnvOption(), }); @@ -67,9 +71,9 @@ class LeetCodeManager extends EventEmitter { } childProc.stdin.write(`${name}\n`); const pwd: string | undefined = await vscode.window.showInputBox({ - prompt: "Enter password.", + prompt: isCookieIn ? "Enter cookie" : "Enter password.", password: true, - validateInput: (s: string): string | undefined => s ? undefined : "Password must not be empty", + validateInput: (s: string): string | undefined => s ? undefined : isCookieIn ? "Cookie must not be empty" : "Password must not be empty", }); if (!pwd) { childProc.kill(); @@ -78,22 +82,22 @@ class LeetCodeManager extends EventEmitter { 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]); + const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login) as (.*)/i); + if (match && match[2]) { + resolve(match[2]); } else { - reject(new Error("Failed to sign in.")); + reject(new Error(`Failed to ${inMessage}.`)); } }); }); if (userName) { - vscode.window.showInformationMessage("Successfully signed in."); + vscode.window.showInformationMessage(`Successfully ${inMessage}.`); 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); + promptForOpenOutputChannel(`Failed to ${inMessage}. Please open the output channel for details`, DialogType.error); } } From 312479082871e41a1331806f18431073dceb98ad Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Tue, 3 Dec 2019 16:33:26 +0800 Subject: [PATCH 02/12] try to fix ci first time --- src/leetCodeManager.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 1bbcf72f..aefd9be6 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -34,11 +34,11 @@ class LeetCodeManager extends EventEmitter { } } - public async signIn(isCookieIn = false): Promise { - const loginArg = "-l"; - const cookieInArg = "-c"; - const commandArg = isCookieIn ? cookieInArg : loginArg; - const inMessage = isCookieIn ? "cookie in" : "sign in" + public async signIn(isCookieIn: boolean = false): Promise { + const loginArg: string = "-l"; + const cookieInArg: string = "-c"; + const commandArg: string = isCookieIn ? cookieInArg : loginArg; + const inMessage: string = isCookieIn ? "cookie in" : "sign in" try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { let result: string = ""; From 6ab8a3bb455591c24038c6eabe6577d565a2d7ae Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Tue, 3 Dec 2019 16:36:10 +0800 Subject: [PATCH 03/12] try to fix ci second time --- src/leetCodeManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index aefd9be6..d1819e2a 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -38,7 +38,7 @@ class LeetCodeManager extends EventEmitter { const loginArg: string = "-l"; const cookieInArg: string = "-c"; const commandArg: string = isCookieIn ? cookieInArg : loginArg; - const inMessage: string = isCookieIn ? "cookie in" : "sign in" + const inMessage: string = isCookieIn ? "cookie in" : "sign in"; try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { let result: string = ""; From 8d24d57a41df7b08d5635df31ed3c072b78b2250 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Wed, 4 Dec 2019 20:53:49 +0800 Subject: [PATCH 04/12] rename cookie in to Sign in (by cookie) --- README.md | 2 +- package.json | 6 +++--- src/extension.ts | 2 +- src/leetCodeManager.ts | 12 ++++++------ 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3e727458..84370516 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ - You can also use the following command to sign in/cookie in/out: - **LeetCode: Sign in** - - **LeetCode: Cookie in** + - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** --- diff --git a/package.json b/package.json index b14fb6a8..f2a0d75d 100644 --- a/package.json +++ b/package.json @@ -38,15 +38,15 @@ "onCommand:leetcode.testSolution", "onCommand:leetcode.submitSolution", "onCommand:leetcode.switchDefaultLanguage", - "onCommand:leetcode.cookieIn", + "onCommand:leetcode.signinByCookie", "onView:leetCodeExplorer" ], "main": "./out/src/extension", "contributes": { "commands": [ { - "command": "leetcode.cookieIn", - "title": "Cookie In", + "command": "leetcode.signinByCookie", + "title": "Sign In By Cookie", "category": "LeetCode" }, { diff --git a/src/extension.ts b/src/extension.ts index 0e7d7224..45de4a70 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -51,7 +51,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()), vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()), vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()), - vscode.commands.registerCommand("leetcode.cookieIn", () => leetCodeManager.signIn(true)), + vscode.commands.registerCommand("leetcode.signinByCookie", () => leetCodeManager.signIn(true)), vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()), vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()), vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)), diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index d1819e2a..0b70080e 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -34,11 +34,11 @@ class LeetCodeManager extends EventEmitter { } } - public async signIn(isCookieIn: boolean = false): Promise { + public async signIn(isByCookie: boolean = false): Promise { const loginArg: string = "-l"; - const cookieInArg: string = "-c"; - const commandArg: string = isCookieIn ? cookieInArg : loginArg; - const inMessage: string = isCookieIn ? "cookie in" : "sign in"; + const cookieArg: string = "-c"; + const commandArg: string = isByCookie ? cookieArg : loginArg; + const inMessage: string = isByCookie ? "sign in by cookie" : "sign in"; try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { let result: string = ""; @@ -71,9 +71,9 @@ class LeetCodeManager extends EventEmitter { } childProc.stdin.write(`${name}\n`); const pwd: string | undefined = await vscode.window.showInputBox({ - prompt: isCookieIn ? "Enter cookie" : "Enter password.", + prompt: isByCookie ? "Enter cookie" : "Enter password.", password: true, - validateInput: (s: string): string | undefined => s ? undefined : isCookieIn ? "Cookie must not be empty" : "Password must not be empty", + validateInput: (s: string): string | undefined => s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty", }); if (!pwd) { childProc.kill(); From ae2fa0490fefa56eb8831806656338143d2f2e3a Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Wed, 4 Dec 2019 21:17:31 +0800 Subject: [PATCH 05/12] also change readme-cn --- README.md | 2 +- docs/README_zh-CN.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84370516..740f9913 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ - Simply click `Sign in to LeetCode` in the `LeetCode Explorer` will let you **sign in** with your LeetCode account. -- You can also use the following command to sign in/cookie in/out: +- You can also use the following command to sign in/sign in (by cookie)/out: - **LeetCode: Sign in** - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 4c9348a1..f7570685 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -40,8 +40,9 @@ - 点击 `LeetCode Explorer` 中的 `Sign in to LeetCode` 即可登入。 -- 你也可以使用下来命令登入或登出: +- 你也可以使用下来命令登入或利用cookie登入或登出: - **LeetCode: Sign in** + - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** --- From ed51cb5503b41c7c084ba99ee5325b23df0b0366 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Thu, 5 Dec 2019 10:07:51 +0800 Subject: [PATCH 06/12] fix preposition typo --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f2a0d75d..149059de 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "commands": [ { "command": "leetcode.signinByCookie", - "title": "Sign In By Cookie", + "title": "Sign In by Cookie", "category": "LeetCode" }, { From e4606fc430fda239a6ce0223c541e4983c6f9dc0 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 20 Dec 2019 16:57:04 +0800 Subject: [PATCH 07/12] add thrid party login -- GitHub and LinkedIn --- README.md | 3 +++ docs/README_zh-CN.md | 3 +++ package.json | 22 +++++++++++++++++----- src/extension.ts | 2 ++ src/leetCodeManager.ts | 11 +++++++---- src/shared.ts | 6 ++++++ 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b8aac1e3..72fc60a6 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** +- You can also use the following command to sign in by third party: + - **LeetCode: Sign in by GitHub** + - **LeetCode: Sign in by LinkedIn** --- ### Switch Endpoint diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 81c98fe7..0ea779ce 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -56,6 +56,9 @@ - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** +- 你也可以使用下列命令第三方登入: + - **LeetCode: Sign in by GitHub** + - **LeetCode: Sign in by LinkedIn** --- ### 切换 LeetCode 版本 diff --git a/package.json b/package.json index 23ab4685..b9bfcd75 100644 --- a/package.json +++ b/package.json @@ -39,16 +39,13 @@ "onCommand:leetcode.submitSolution", "onCommand:leetcode.switchDefaultLanguage", "onCommand:leetcode.signinByCookie", + "onCommand:leetcode.signinByGitHub", + "onCommand:leetcode.signinByLinkedIn", "onView:leetCodeExplorer" ], "main": "./out/src/extension", "contributes": { "commands": [ - { - "command": "leetcode.signinByCookie", - "title": "Sign In by Cookie", - "category": "LeetCode" - }, { "command": "leetcode.deleteCache", "title": "Delete Cache", @@ -72,6 +69,21 @@ "dark": "resources/dark/signin.svg" } }, + { + "command": "leetcode.signinByCookie", + "title": "Sign In by Cookie", + "category": "LeetCode" + }, + { + "command": "leetcode.signinByGitHub", + "title": "Sign In by GitHub", + "category": "LeetCode" + }, + { + "command": "leetcode.signinByLinkedIn", + "title": "Sign In by LinkedIn", + "category": "LeetCode" + }, { "command": "leetcode.signout", "title": "Sign Out", diff --git a/src/extension.ts b/src/extension.ts index 45de4a70..ec14359c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -52,6 +52,8 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()), vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()), vscode.commands.registerCommand("leetcode.signinByCookie", () => leetCodeManager.signIn(true)), + vscode.commands.registerCommand("leetcode.signinByGitHub", () => leetCodeManager.signIn(false, "GitHub")), + vscode.commands.registerCommand("leetcode.signinByLinkedIn", () => leetCodeManager.signIn(false, "LinkedIn")), vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()), vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()), vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)), diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 0b70080e..5d8c3e12 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -6,7 +6,7 @@ import { EventEmitter } from "events"; import * as vscode from "vscode"; import { leetCodeChannel } from "./leetCodeChannel"; import { leetCodeExecutor } from "./leetCodeExecutor"; -import { UserStatus } from "./shared"; +import { UserStatus, LoginCommand } from "./shared"; import { createEnvOption } from "./utils/cpUtils"; import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; @@ -34,8 +34,11 @@ class LeetCodeManager extends EventEmitter { } } - public async signIn(isByCookie: boolean = false): Promise { - const loginArg: string = "-l"; + public async signIn(isByCookie: boolean = false, thirdParty: string = "Default"): Promise { + const loginArg: string | undefined = LoginCommand.get(thirdParty); + if (!loginArg) { + throw new Error(`The third party "${thirdParty}" is not supported.`); + } const cookieArg: string = "-c"; const commandArg: string = isByCookie ? cookieArg : loginArg; const inMessage: string = isByCookie ? "sign in by cookie" : "sign in"; @@ -82,7 +85,7 @@ class LeetCodeManager extends EventEmitter { childProc.stdin.write(`${pwd}\n`); childProc.stdin.end(); childProc.on("close", () => { - const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login) as (.*)/i); + const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login|third party login) as (.*)/i); if (match && match[2]) { resolve(match[2]); } else { diff --git a/src/shared.ts b/src/shared.ts index 78518454..54debf30 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -12,6 +12,12 @@ export enum UserStatus { SignedOut = 2, } +export const LoginCommand: Map = new Map([ + ["Default", "-l"], + ["GitHub", "-g"], + ["LinkedIn", "-i"], +]); + export const languages: string[] = [ "bash", "c", From ae9463e31f7c16a0c1f4278c44917c7ee2a7dbbd Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 20 Dec 2019 17:08:59 +0800 Subject: [PATCH 08/12] fix lint bug --- src/leetCodeManager.ts | 4 ++-- src/shared.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 5d8c3e12..870b8f1a 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -6,7 +6,7 @@ import { EventEmitter } from "events"; import * as vscode from "vscode"; import { leetCodeChannel } from "./leetCodeChannel"; import { leetCodeExecutor } from "./leetCodeExecutor"; -import { UserStatus, LoginCommand } from "./shared"; +import { loginCommand, UserStatus } from "./shared"; import { createEnvOption } from "./utils/cpUtils"; import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; @@ -35,7 +35,7 @@ class LeetCodeManager extends EventEmitter { } public async signIn(isByCookie: boolean = false, thirdParty: string = "Default"): Promise { - const loginArg: string | undefined = LoginCommand.get(thirdParty); + const loginArg: string | undefined = loginCommand.get(thirdParty); if (!loginArg) { throw new Error(`The third party "${thirdParty}" is not supported.`); } diff --git a/src/shared.ts b/src/shared.ts index 54debf30..a50ffd68 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -12,7 +12,7 @@ export enum UserStatus { SignedOut = 2, } -export const LoginCommand: Map = new Map([ +export const loginCommand: Map = new Map([ ["Default", "-l"], ["GitHub", "-g"], ["LinkedIn", "-i"], From 4b0577e0806b81cef403516154398330a2b6a9c0 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 27 Dec 2019 17:48:01 +0800 Subject: [PATCH 09/12] make login methods in one pop up --- package.json | 20 +---------------- src/extension.ts | 3 --- src/leetCodeManager.ts | 51 ++++++++++++++++++++++++++++++++++++------ src/shared.ts | 3 ++- 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index b9bfcd75..743b2803 100644 --- a/package.json +++ b/package.json @@ -38,9 +38,6 @@ "onCommand:leetcode.testSolution", "onCommand:leetcode.submitSolution", "onCommand:leetcode.switchDefaultLanguage", - "onCommand:leetcode.signinByCookie", - "onCommand:leetcode.signinByGitHub", - "onCommand:leetcode.signinByLinkedIn", "onView:leetCodeExplorer" ], "main": "./out/src/extension", @@ -69,21 +66,6 @@ "dark": "resources/dark/signin.svg" } }, - { - "command": "leetcode.signinByCookie", - "title": "Sign In by Cookie", - "category": "LeetCode" - }, - { - "command": "leetcode.signinByGitHub", - "title": "Sign In by GitHub", - "category": "LeetCode" - }, - { - "command": "leetcode.signinByLinkedIn", - "title": "Sign In by LinkedIn", - "category": "LeetCode" - }, { "command": "leetcode.signout", "title": "Sign Out", @@ -701,6 +683,6 @@ "markdown-it": "^8.4.2", "require-from-string": "^2.0.2", "unescape-js": "^1.1.1", - "vsc-leetcode-cli": "2.6.19" + "vsc-leetcode-cli": "2.6.20" } } diff --git a/src/extension.ts b/src/extension.ts index ec14359c..9bb3ad41 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -51,9 +51,6 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()), vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()), vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()), - vscode.commands.registerCommand("leetcode.signinByCookie", () => leetCodeManager.signIn(true)), - vscode.commands.registerCommand("leetcode.signinByGitHub", () => leetCodeManager.signIn(false, "GitHub")), - vscode.commands.registerCommand("leetcode.signinByLinkedIn", () => leetCodeManager.signIn(false, "LinkedIn")), vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()), vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()), vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)), diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 870b8f1a..fa7efd0d 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -6,7 +6,7 @@ import { EventEmitter } from "events"; import * as vscode from "vscode"; import { leetCodeChannel } from "./leetCodeChannel"; import { leetCodeExecutor } from "./leetCodeExecutor"; -import { loginCommand, UserStatus } from "./shared"; +import { IQuickItemEx, loginCommand, UserStatus } from "./shared"; import { createEnvOption } from "./utils/cpUtils"; import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; @@ -34,13 +34,50 @@ class LeetCodeManager extends EventEmitter { } } - public async signIn(isByCookie: boolean = false, thirdParty: string = "Default"): Promise { - const loginArg: string | undefined = loginCommand.get(thirdParty); - if (!loginArg) { - throw new Error(`The third party "${thirdParty}" is not supported.`); + public async signIn(): Promise { + /* + LeetCode Account + LeetCode Cookie + Third-Party: GitHub + Third-Party: LinkedIn + */ + const picks: Array> = []; + picks.push( + { + label: "LeetCode Account", + description: "", + detail: "Use LeetCode account to login", + value: "LeetCode", + }, + { + label: "LeetCode Cookie", + description: "", + detail: "Use LeetCode cookie that copy from browser to login", + value: "Cookie", + }, + { + label: "Third-Party: GitHub", + description: "", + detail: "Use third party GitHub account to login", + value: "GitHub", + }, + { + label: "Third-Party: LinkedIn", + description: "", + detail: "Use third party LinkedIn account to login", + value: "LinkedIn", + }, + ); + const choice: IQuickItemEx | undefined = await vscode.window.showQuickPick(picks); + if (!choice) { + return; } - const cookieArg: string = "-c"; - const commandArg: string = isByCookie ? cookieArg : loginArg; + const loginMethod: string = choice.value; + const commandArg: string | undefined = loginCommand.get(loginMethod); + if (!commandArg) { + throw new Error(`The login method "${loginMethod}" is not supported.`); + } + const isByCookie: boolean = loginMethod === "Cookie"; const inMessage: string = isByCookie ? "sign in by cookie" : "sign in"; try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { diff --git a/src/shared.ts b/src/shared.ts index a50ffd68..9093affa 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -13,7 +13,8 @@ export enum UserStatus { } export const loginCommand: Map = new Map([ - ["Default", "-l"], + ["LeetCode", "-l"], + ["Cookie", "-c"], ["GitHub", "-g"], ["LinkedIn", "-i"], ]); From f52cde37878bb384ed1c3d6594a670b0b9611400 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 27 Dec 2019 21:16:05 +0800 Subject: [PATCH 10/12] delete comments and change cli version for ci --- package.json | 2 +- src/leetCodeManager.ts | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/package.json b/package.json index 743b2803..5776eb15 100644 --- a/package.json +++ b/package.json @@ -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.19" } } diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index fa7efd0d..387478ca 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -35,12 +35,6 @@ class LeetCodeManager extends EventEmitter { } public async signIn(): Promise { - /* - LeetCode Account - LeetCode Cookie - Third-Party: GitHub - Third-Party: LinkedIn - */ const picks: Array> = []; picks.push( { From a7f2ff0adedbac9f69f0d09ed0683be53611def5 Mon Sep 17 00:00:00 2001 From: yihong Date: Sun, 29 Dec 2019 19:15:10 +0800 Subject: [PATCH 11/12] make naming more formal by review --- README.md | 7 +------ docs/README_zh-CN.md | 5 ----- package.json | 2 +- src/leetCodeManager.ts | 14 +++++--------- src/shared.ts | 2 +- 5 files changed, 8 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 72fc60a6..1d8d21f3 100644 --- a/README.md +++ b/README.md @@ -51,14 +51,9 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh - Simply click `Sign in to LeetCode` in the `LeetCode Explorer` will let you **sign in** with your LeetCode account. -- You can also use the following command to sign in/sign in (by cookie)/out: +- You can also use the following command to sign in/out: - **LeetCode: Sign in** - - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** - -- You can also use the following command to sign in by third party: - - **LeetCode: Sign in by GitHub** - - **LeetCode: Sign in by LinkedIn** --- ### Switch Endpoint diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 0ea779ce..23faf30c 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -53,12 +53,7 @@ - 你也可以使用下来命令登入或利用cookie登入或登出: - **LeetCode: Sign in** - - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** - -- 你也可以使用下列命令第三方登入: - - **LeetCode: Sign in by GitHub** - - **LeetCode: Sign in by LinkedIn** --- ### 切换 LeetCode 版本 diff --git a/package.json b/package.json index 5776eb15..743b2803 100644 --- a/package.json +++ b/package.json @@ -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.19" + "vsc-leetcode-cli": "2.6.20" } } diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 387478ca..8a6a51b9 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -6,7 +6,7 @@ import { EventEmitter } from "events"; import * as vscode from "vscode"; import { leetCodeChannel } from "./leetCodeChannel"; import { leetCodeExecutor } from "./leetCodeExecutor"; -import { IQuickItemEx, loginCommand, UserStatus } from "./shared"; +import { IQuickItemEx, loginArgsMapping, UserStatus } from "./shared"; import { createEnvOption } from "./utils/cpUtils"; import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; @@ -39,26 +39,22 @@ class LeetCodeManager extends EventEmitter { picks.push( { label: "LeetCode Account", - description: "", detail: "Use LeetCode account to login", value: "LeetCode", }, { label: "LeetCode Cookie", - description: "", - detail: "Use LeetCode cookie that copy from browser to login", + detail: "Use LeetCode cookie copied from browser to login", value: "Cookie", }, { label: "Third-Party: GitHub", - description: "", - detail: "Use third party GitHub account to login", + detail: "Use GitHub account to login", value: "GitHub", }, { label: "Third-Party: LinkedIn", - description: "", - detail: "Use third party LinkedIn account to login", + detail: "Use LinkedIn account to login", value: "LinkedIn", }, ); @@ -67,7 +63,7 @@ class LeetCodeManager extends EventEmitter { return; } const loginMethod: string = choice.value; - const commandArg: string | undefined = loginCommand.get(loginMethod); + const commandArg: string | undefined = loginArgsMapping.get(loginMethod); if (!commandArg) { throw new Error(`The login method "${loginMethod}" is not supported.`); } diff --git a/src/shared.ts b/src/shared.ts index 9093affa..5f1039e4 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -12,7 +12,7 @@ export enum UserStatus { SignedOut = 2, } -export const loginCommand: Map = new Map([ +export const loginArgsMapping: Map = new Map([ ["LeetCode", "-l"], ["Cookie", "-c"], ["GitHub", "-g"], From b05f9d42f733631bffc70d55945790763bea125d Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Wed, 8 Jan 2020 22:29:00 +0800 Subject: [PATCH 12/12] add two-factor support for GitHub login --- src/leetCodeManager.ts | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 8a6a51b9..25439048 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -71,7 +71,6 @@ class LeetCodeManager extends EventEmitter { const inMessage: string = isByCookie ? "sign in by cookie" : "sign in"; try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { - let result: string = ""; const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath(); @@ -82,10 +81,27 @@ class LeetCodeManager extends EventEmitter { env: createEnvOption(), }); - childProc.stdout.on("data", (data: string | Buffer) => { + childProc.stdout.on("data", async (data: string | Buffer) => { data = data.toString(); - result = result.concat(data); leetCodeChannel.append(data); + if (data.includes("twoFactorCode")) { + const twoFactor: string | undefined = await vscode.window.showInputBox({ + prompt: "Enter two-factor code.", + 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`); + 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]); + } + } }); childProc.stderr.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString())); @@ -110,13 +126,9 @@ class LeetCodeManager extends EventEmitter { return resolve(undefined); } childProc.stdin.write(`${pwd}\n`); - childProc.stdin.end(); - childProc.on("close", () => { - const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login|third party login) as (.*)/i); - if (match && match[2]) { - resolve(match[2]); - } else { - reject(new Error(`Failed to ${inMessage}.`)); + childProc.on("close", (code: number) => { + if (code !== 0) { + reject(new Error("Failed to login.")); } }); });