8000 extract leetcode command - part 1 · JackieHammer/vscode-leetcode@16e6b6c · GitHub
[go: up one dir, main page]

Skip to content

Commit 16e6b6c

Browse files
committed
extract leetcode command - part 1
1 parent ac20b6c commit 16e6b6c

File tree

7 files changed

+77
-23
lines changed

7 files changed

+77
-23
lines changed

src/commands/list.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4+
import { leetCodeExecutor } from "../leetCodeExecutor";
45
import { leetCodeManager } from "../leetCodeManager";
56
import { leetCodeBinaryPath, ProblemState, UserStatus } from "../shared";
67
import { executeCommand } from "../utils/cpUtils";
@@ -22,8 +23,8 @@ export async function listProblems(): Promise<IProblem[]> {
2223
return [];
2324
}
2425
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
25-
const showLocked: boolean | undefined = leetCodeConfig.get<boolean>("showLocked");
26-
const result: string = await executeCommand("node", showLocked ? [leetCodeBinaryPath, "list"] : [leetCodeBinaryPath, "list", "-q", "L"]);
26+
const showLocked: boolean = !!leetCodeConfig.get<boolean>("showLocked");
27+
const result: string = await leetCodeExecutor.listProblems(showLocked);
2728
const problems: IProblem[] = [];
2829
const lines: string[] = result.split("\n");
2930
const reg: RegExp = /^(.)\s(.{1,2})\s(.)\s\[\s*(\d*)\]\s*(.*)\s*(Easy|Medium|Hard)\s*\((\s*\d+\.\d+ %)\)/;

src/commands/session.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4+
import { leetCodeExecutor } from "../leetCodeExecutor";
45
import { leetCodeManager } from "../leetCodeManager";
56
import { IQuickItemEx, leetCodeBinaryPath } from "../shared";
67
import { executeCommand } from "../utils/cpUtils";
@@ -12,7 +13,7 @@ export async function getSessionList(): Promise<ISession[]> {
1213
promptForSignIn();
1314
return [];
1415
}
15-
const result: string = await executeCommand("node", [leetCodeBinaryPath, "session"]);
16+
const result: string = await leetCodeExecutor.listSessions();
1617
const lines: string[] = result.split("\n");
1718
const sessions: ISession[] = [];
1819
const reg: RegExp = /(.?)\s*(\d+)\s+(.*)\s+(\d+ \(\s*\d+\.\d+ %\))\s+(\d+ \(\s*\d+\.\d+ %\))/;
@@ -41,7 +42,7 6D40 @@ export async function selectSession(): Promise<void> {
4142
return;
4243
}
4344
try {
44-
await executeCommand("node", [leetCodeBinaryPath, "session", "-e", choice.value]);
45+
await leetCodeExecutor.enableSession(choice.value);
4546
vscode.window.showInformationMessage(`Successfully switched to session '${choice.label}'.`);
4647
await vscode.commands.executeCommand("leetcode.refreshExplorer");
4748
} catch (error) {
@@ -81,7 +82,7 @@ export async function createSession(): Promise<void> {
8182
return;
8283
}
8384
try {
84-
await executeCommand("node", [leetCodeBinaryPath, "session", "-c", session]);
85+
await leetCodeExecutor.createSession(session);
8586
vscode.window.showInformationMessage("New session created, you can switch to it by clicking the status bar.");
8687
} catch (error) {
8788
await promptForOpenOutputChannel("Failed to create session. Please open the output channel for details.", DialogType.error);

src/leetCodeExecutor.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
import * as path from "path";
4+
import * as wsl from "./utils/wslUtils";
5+
6+
export interface ILeetCodeExecutor {
7+
listProblems(showLocked: boolean): Promise<string>;
8+
9+
listSessions(): Promise<string>;
10+
enableSession(name: string): Promise<string>;
11+
createSession(name: string): Promise<string>;
12+
}
13+
14+
class LeetCodeExecutor implements ILeetCodeExecutor {
15+
private leetCodeBinaryPath: string;
16+
private leetCodeBinaryPathInWsl: string;
17+
18+
constructor() {
19+
this.leetCodeBinaryPath = `"${path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode")}"`;
20+
this.leetCodeBinaryPathInWsl = "";
21+
}
22+ F438
23+
public async getLeetCodeBinaryPath(): Promise<string> {
24+
if (wsl.useWsl()) {
25+
if (!this.leetCodeBinaryPathInWsl) {
26+
this.leetCodeBinaryPathInWsl = await wsl.toWslPath(this.leetCodeBinaryPath);
27+
}
28+
return this.leetCodeBinaryPathInWsl;
29+
}
30+
return this.leetCodeBinaryPath;
31+
}
32+
33+
public async listProblems(showLocked: boolean): Promise<string> {
34+
return await wsl.executeCommandEx("node", showLocked ?
35+
[await this.getLeetCodeBinaryPath(), "list"] :
36+
[await this.getLeetCodeBinaryPath(), "list", "-q", "L"],
37+
);
38+
}
39+
40+
public async listSessions(): Promise<string> {
41+
return await wsl.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session"]);
42+
}
43+
44+
public async enableSession(name: string): Promise<string> {
45+
return await wsl.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session", "-e", name]);
46+
}
47+
48+
public async createSession(name: string): Promise<string> {
49+
return await wsl.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session", "-c", name]);
50+
}
51+
}
52+
53+
export const leetCodeExecutor: ILeetCodeExecutor = new LeetCodeExecutor();

src/leetCodeManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
4747
let result: string = "";
4848

4949
const childProc: cp.ChildProcess = wsl.useWsl()
50-
? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true })
50+
? cp.spawn("wsl", ["node", await wsl.toWslPath(leetCodeBinaryPath), "user", "-l"], { shell: true })
5151
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
5252

5353
childProc.stdout.on("data", (data: string | Buffer) => {

src/shared.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@
22

33
import * as path from "path";
44
import * as vscode from "vscode";
5-
import * as wsl from "./utils/wslUtils";
65

7-
let binPath: string = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode");
8-
9-
if (wsl.useWsl()) {
10-
binPath = wsl.toWslPath(binPath);
11-
}
12-
13-
export const leetCodeBinaryPath: string = `"${binPath}"`;
6+
export const leetCodeBinaryPath: string = `"${path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode")}"`;
147

158
export interface IQuickItemEx<T> extends vscode.QuickPickItem {
169
value: T;

src/utils/cpUtils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
import * as cp from "child_process";
44
import * as vscode from "vscode";
55
import { leetCodeChannel } from "../leetCodeChannel";
6-
import * as wsl from "./wslUtils";
76

87
export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
98
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
109
let result: string = "";
1110

12-
const childProc: cp.ChildProcess = wsl.useWsl()
13-
? cp.spawn("wsl", [command].concat(args), options)
14-
: cp.spawn(command, args, options);
11+
const childProc: cp.ChildProcess = cp.spawn(command, args, options);
1512

1613
childProc.stdout.on("data", (data: string | Buffer) => {
1714
data = data.toString();

src/utils/wslUtils.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22

33
import * as cp from "child_process";
44
import * as vscode from "vscode";
5+
import { executeCommand } from "./cpUtils";
6+
7+
const wslCommand: string = "wsl";
58

69
export function useWsl(): boolean {
710
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
8-
911
return process.platform === "win32" && leetCodeConfig.get<boolean>("useWsl") === true;
1012
}
1113

12-
export function toWslPath(path: string): string {
13-
return cp.execFileSync("wsl", ["wslpath", "-u", `${path.replace(/\\/g, "/")}`]).toString().trim();
14+
export async function toWslPath(path: string): Promise<string> {
15+
return await executeCommand(wslCommand, ["wslpath", "-u", `"${path}"`]).toString().trim();
16+
}
17+
18+
export async function toWinPath(path: string): Promise<string> {
19+
return await executeCommand(wslCommand, ["wslpath", "-w", `"${path}"`]).toString().trim();
1420
}
1521

16-
export function toWinPath(path: string): string {
17-
return cp.execFileSync("wsl", ["wslpath", "-w", path]).toString().trim();
22+
export async function executeCommandEx(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
23+
if (useWsl()) {
24+
return await executeCommand(wslCommand, [command].concat(args), options);
25+
}
26+
return await executeCommand(command, args, options);
1827
}

0 commit comments

Comments
 (0)
0