Skip to content

Commit b2deee8

Browse files
puroceanjdneo
authored andcommitted
support WSL (#46)
1 parent 0d27cf7 commit b2deee8

File tree

7 files changed

+53
-6
lines changed

7 files changed

+53
-6
lines changed

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@
201201
"default": true,
202202
"scope": "window",
203203
"description": "Show a hint to set the default language."
204+
},
205+
"leetcode.useWsl": {
206+
"type": "boolean",
207+
"default": false,
208+
"scope": "window",
209+
"description": "Use Node.js inside the Windows Subsystem for Linux."
204210
}
205211
}
206212
}

src/commands/show.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IQuickItemEx, languages, leetCodeBinaryPath, ProblemState } from "../sh
88
import { executeCommand } from "../utils/cpUtils";
99
import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
1010
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
11+
import * as wsl from "../utils/wslUtils";
1112
import * as list from "./list";
1213

1314
export async function showProblem(channel: vscode.OutputChannel, node?: LeetCodeNode): Promise<void> {
@@ -53,7 +54,9 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
5354
const reg: RegExp = /\* Source Code:\s*(.*)/;
5455
const match: RegExpMatchArray | null = result.match(reg);
5556
if (match && match.length >= 2) {
56-
await vscode.window.showTextDocument(vscode.Uri.file(match[1].trim()), { preview: false });
57+
const filePath = wsl.useWsl() ? wsl.toWinPath(match[1].trim()) : match[1].trim();
58+
59+
await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false });
5760
} else {
5861
throw new Error("Failed to fetch the problem information.");
5962
}

src/leetCodeManager.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { UserStatus } from "./shared";
77
import { leetCodeBinaryPath } from "./shared";
88
import { executeCommand } from "./utils/cpUtils";
99
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
10+
import * as wsl from "./utils/wslUtils";
1011

1112
export interface ILeetCodeManager extends EventEmitter {
1213
getLoginStatus(channel: vscode.OutputChannel): void;
@@ -43,7 +44,11 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
4344
try {
4445
const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => {
4546
let result: string = "";
46-
const childProc: cp.ChildProcess = cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
47+
48+
const childProc: cp.ChildProcess = wsl.useWsl()
49+
? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true })
50+
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
51+
4752
childProc.stdout.on("data", (data: string | Buffer) => {
4853
data = data.toString();
4954
result = result.concat(data);

src/shared.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

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

6-
export const leetCodeBinaryPath: string = `"${path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode")}"`;
7+
let binPath = 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}"`;
714

815
export interface IQuickItemEx<T> extends vscode.QuickPickItem {
916
value: T;

src/utils/cpUtils.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
import * as cp from "child_process";
44
import * as vscode from "vscode";
5+
import * as wsl from "./wslUtils";
56

67
export async function executeCommand(channel: vscode.OutputChannel, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
78
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
89
let result: string = "";
9-
const childProc: cp.ChildProcess = cp.spawn(command, args, options);
10+
11+
const childProc: cp.ChildProcess = wsl.useWsl()
12+
? cp.spawn("wsl", [command].concat(args), options)
13+
: cp.spawn(command, args, options);
1014

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

src/utils/workspaceUtils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as os from "os";
44
import * as path from "path";
55
import * as vscode from "vscode";
6+
import * as wsl from "./wslUtils";
67

78
export async function selectWorkspaceFolder(): Promise<string> {
89
let folder: vscode.WorkspaceFolder | undefined;
@@ -15,7 +16,10 @@ export async function selectWorkspaceFolder(): Promise<string> {
1516
folder = vscode.workspace.workspaceFolders[0];
1617
}
1718
}
18-
return folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode");
19+
20+
const workFolder = folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode");
21+
22+
return wsl.useWsl() ? wsl.toWslPath(workFolder) : workFolder;
1923
}
2024

2125
export async function getActivefilePath(uri?: vscode.Uri): Promise<string | undefined> {
@@ -33,5 +37,5 @@ export async function getActivefilePath(uri?: vscode.Uri): Promise<string | unde
3337
vscode.window.showWarningMessage("Please save the solution file first.");
3438
return undefined;
3539
}
36-
return textEditor.document.uri.fsPath;
40+
return wsl.useWsl() ? wsl.toWslPath(textEditor.document.uri.fsPath) : textEditor.document.uri.fsPath;
3741
}

src/utils/wslUtils.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
import * as cp from "child_process";
4+
import * as vscode from "vscode";
5+
6+
export function useWsl(): boolean {
7+
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
8+
9+
return process.platform === "win32" && leetCodeConfig.get<boolean>("useWsl") === true;
10+
}
11+
12+
export function toWslPath(path: string): string {
13+
return cp.execFileSync("wsl", ["wslpath", "-u", `${path.replace(/\\/g, "/")}`]).toString().trim();
14+
}
15+
16+
export function toWinPath(path: string): string {
17+
return cp.execFileSync("wsl", ["wslpath", "-w", path]).toString().trim();
18+
}

0 commit comments

Comments
 (0)