Skip to content

Commit

Permalink
feat: add leetcode.signinByCookie but simple change (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong0618 authored and jdneo committed Dec 5, 2019
1 parent eb77cd9 commit 4979a0f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/sign in (by cookie)/out:
- **LeetCode: Sign in**
- **LeetCode: Sign in (by cookie)**
- **LeetCode: Sign out**

---
Expand Down
3 changes: 2 additions & 1 deletion docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@

- 点击 `LeetCode Explorer` 中的 `Sign in to LeetCode` 即可登入。

- 你也可以使用下来命令登入或登出:
- 你也可以使用下来命令登入或利用cookie登入或登出:
- **LeetCode: Sign in**
- **LeetCode: Sign in (by cookie)**
- **LeetCode: Sign out**

---
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@
"onCommand:leetcode.testSolution",
"onCommand:leetcode.submitSolution",
"onCommand:leetcode.switchDefaultLanguage",
"onCommand:leetcode.signinByCookie",
"onView:leetCodeExplorer"
],
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "leetcode.signinByCookie",
"title": "Sign In by Cookie",
"category": "LeetCode"
},
{
"command": "leetcode.deleteCache",
"title": "Delete Cache",
Expand Down Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
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.signout", () => leetCodeManager.signOut()),
vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()),
vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)),
Expand Down
26 changes: 15 additions & 11 deletions src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ class LeetCodeManager extends EventEmitter {
}
}

public async signIn(): Promise<void> {
public async signIn(isByCookie: boolean = false): Promise<void> {
const loginArg: string = "-l";
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<void> => {
let result: string = "";

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(),
});
Expand All @@ -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: isByCookie ? "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 : isByCookie ? "Cookie must not be empty" : "Password must not be empty",
});
if (!pwd) {
childProc.kill();
Expand All @@ -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);
}

}
Expand Down

0 comments on commit 4979a0f

Please sign in to comment.