From d330913ad843179e12d0248a155f8a1f570e9f6f Mon Sep 17 00:00:00 2001 From: purocean Date: Sat, 21 Jul 2018 14:11:31 +0800 Subject: [PATCH 1/5] support WSL --- package.json | 6 ++++++ src/commands/show.ts | 5 ++++- src/leetCodeManager.ts | 7 ++++++- src/shared.ts | 9 ++++++++- src/utils/cpUtils.ts | 6 +++++- src/utils/workspaceUtils.ts | 8 ++++++-- src/utils/wslUtils.ts | 18 ++++++++++++++++++ 7 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 src/utils/wslUtils.ts diff --git a/package.json b/package.json index b41bb759..79a5cc17 100644 --- a/package.json +++ b/package.json @@ -201,6 +201,12 @@ "default": true, "scope": "window", "description": "Show a hint to set the default language." + }, + "leetcode.useWsl": { + "type": "boolean", + "default": false, + "scope": "window", + "description": "Show locked problems." } } } diff --git a/src/commands/show.ts b/src/commands/show.ts index c42df84e..90aa70fd 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -9,6 +9,7 @@ import { executeCommand } from "../utils/cpUtils"; import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils"; import { selectWorkspaceFolder } from "../utils/workspaceUtils"; import * as list from "./list"; +import * as wsl from '../utils/wslUtils'; export async function showProblem(channel: vscode.OutputChannel, node?: LeetCodeNode): Promise { if (!node) { @@ -53,7 +54,9 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P const reg: RegExp = /\* Source Code:\s*(.*)/; const match: RegExpMatchArray | null = result.match(reg); if (match && match.length >= 2) { - await vscode.window.showTextDocument(vscode.Uri.file(match[1].trim()), { preview: false }); + const filePath = wsl.useWsl() ? wsl.toWinPath(match[1].trim()) : match[1].trim() + + await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false }); } else { throw new Error("Failed to fetch the problem information."); } diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 313fe714..90f99536 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -7,6 +7,7 @@ import { UserStatus } from "./shared"; import { leetCodeBinaryPath } from "./shared"; import { executeCommand } from "./utils/cpUtils"; import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; +import * as wsl from './utils/wslUtils' export interface ILeetCodeManager extends EventEmitter { getLoginStatus(channel: vscode.OutputChannel): void; @@ -43,7 +44,11 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager { try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { let result: string = ""; - const childProc: cp.ChildProcess = cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true }); + + const childProc: cp.ChildProcess = wsl.useWsl() + ? cp.spawn("wsl", ['--', 'node', leetCodeBinaryPath, "user", "-l"], { shell: true }) + : cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true }); + childProc.stdout.on("data", (data: string | Buffer) => { data = data.toString(); result = result.concat(data); diff --git a/src/shared.ts b/src/shared.ts index e3eee105..5af44171 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -2,8 +2,15 @@ import * as path from "path"; import * as vscode from "vscode"; +import * as wsl from './utils/wslUtils' -export const leetCodeBinaryPath: string = `"${path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode")}"`; +let binPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode") + +if (wsl.useWsl()) { + binPath = wsl.toWslPath(binPath) +} + +export const leetCodeBinaryPath: string = `"${binPath}"`; export interface IQuickItemEx extends vscode.QuickPickItem { value: T; diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index 8faddf05..62f3cd6a 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -2,11 +2,15 @@ import * as cp from "child_process"; import * as vscode from "vscode"; +import * as wsl from './wslUtils' export async function executeCommand(channel: vscode.OutputChannel, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise { return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => { let result: string = ""; - const childProc: cp.ChildProcess = cp.spawn(command, args, options); + + const childProc: cp.ChildProcess = wsl.useWsl() + ? cp.spawn('wsl', ['--', command].concat(args), options) + : cp.spawn(command, args, options); childProc.stdout.on("data", (data: string | Buffer) => { data = data.toString(); diff --git a/src/utils/workspaceUtils.ts b/src/utils/workspaceUtils.ts index f8958a54..902c4e67 100644 --- a/src/utils/workspaceUtils.ts +++ b/src/utils/workspaceUtils.ts @@ -3,6 +3,7 @@ import * as os from "os"; import * as path from "path"; import * as vscode from "vscode"; +import * as wsl from "./wslUtils"; export async function selectWorkspaceFolder(): Promise { let folder: vscode.WorkspaceFolder | undefined; @@ -15,7 +16,10 @@ export async function selectWorkspaceFolder(): Promise { folder = vscode.workspace.workspaceFolders[0]; } } - return folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode"); + + const workFolder = folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode"); + + return wsl.useWsl() ? wsl.toWslPath(workFolder) : workFolder } export async function getActivefilePath(uri?: vscode.Uri): Promise { @@ -33,5 +37,5 @@ export async function getActivefilePath(uri?: vscode.Uri): Promise('useWsl') === true +} + +export function toWslPath(path: string): string { + return cp.execFileSync('wsl', ['--', 'wslpath', '-u', `${path.replace(/\\/g, '/')}`]).toString().trim() +} + +export function toWinPath(path: string): string { + return cp.execFileSync('wsl', ['--', 'wslpath', '-w', path]).toString().trim() +} From 024fd74b6feac4918858d1e3bc3efe6b91e47990 Mon Sep 17 00:00:00 2001 From: purocean Date: Sat, 21 Jul 2018 14:20:46 +0800 Subject: [PATCH 2/5] fix ci --- src/commands/show.ts | 4 ++-- src/leetCodeManager.ts | 4 ++-- src/shared.ts | 6 +++--- src/utils/cpUtils.ts | 4 ++-- src/utils/workspaceUtils.ts | 2 +- src/utils/wslUtils.ts | 6 +++--- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/commands/show.ts b/src/commands/show.ts index 90aa70fd..be8dcf7d 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -8,8 +8,8 @@ import { IQuickItemEx, languages, leetCodeBinaryPath, ProblemState } from "../sh import { executeCommand } from "../utils/cpUtils"; import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils"; import { selectWorkspaceFolder } from "../utils/workspaceUtils"; +import * as wsl from "../utils/wslUtils"; import * as list from "./list"; -import * as wsl from '../utils/wslUtils'; export async function showProblem(channel: vscode.OutputChannel, node?: LeetCodeNode): Promise { if (!node) { @@ -54,7 +54,7 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P const reg: RegExp = /\* Source Code:\s*(.*)/; const match: RegExpMatchArray | null = result.match(reg); if (match && match.length >= 2) { - const filePath = wsl.useWsl() ? wsl.toWinPath(match[1].trim()) : match[1].trim() + const filePath = wsl.useWsl() ? wsl.toWinPath(match[1].trim()) : match[1].trim(); await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false }); } else { diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 90f99536..06ffd664 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -7,7 +7,7 @@ import { UserStatus } from "./shared"; import { leetCodeBinaryPath } from "./shared"; import { executeCommand } from "./utils/cpUtils"; import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils"; -import * as wsl from './utils/wslUtils' +import * as wsl from "./utils/wslUtils"; export interface ILeetCodeManager extends EventEmitter { getLoginStatus(channel: vscode.OutputChannel): void; @@ -46,7 +46,7 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager { let result: string = ""; const childProc: cp.ChildProcess = wsl.useWsl() - ? cp.spawn("wsl", ['--', 'node', leetCodeBinaryPath, "user", "-l"], { shell: true }) + ? cp.spawn("wsl", ["--", "node", leetCodeBinaryPath, "user", "-l"], { shell: true }) : cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true }); childProc.stdout.on("data", (data: string | Buffer) => { diff --git a/src/shared.ts b/src/shared.ts index 5af44171..d9c64f7b 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -2,12 +2,12 @@ import * as path from "path"; import * as vscode from "vscode"; -import * as wsl from './utils/wslUtils' +import * as wsl from "./utils/wslUtils"; -let binPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode") +let binPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode"); if (wsl.useWsl()) { - binPath = wsl.toWslPath(binPath) + binPath = wsl.toWslPath(binPath); } export const leetCodeBinaryPath: string = `"${binPath}"`; diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index 62f3cd6a..082b0690 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -2,14 +2,14 @@ import * as cp from "child_process"; import * as vscode from "vscode"; -import * as wsl from './wslUtils' +import * as wsl from "./wslUtils"; export async function executeCommand(channel: vscode.OutputChannel, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise { return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => { let result: string = ""; const childProc: cp.ChildProcess = wsl.useWsl() - ? cp.spawn('wsl', ['--', command].concat(args), options) + ? cp.spawn("wsl", ["--", command].concat(args), options) : cp.spawn(command, args, options); childProc.stdout.on("data", (data: string | Buffer) => { diff --git a/src/utils/workspaceUtils.ts b/src/utils/workspaceUtils.ts index 902c4e67..089282ff 100644 --- a/src/utils/workspaceUtils.ts +++ b/src/utils/workspaceUtils.ts @@ -19,7 +19,7 @@ export async function selectWorkspaceFolder(): Promise { const workFolder = folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode"); - return wsl.useWsl() ? wsl.toWslPath(workFolder) : workFolder + return wsl.useWsl() ? wsl.toWslPath(workFolder) : workFolder; } export async function getActivefilePath(uri?: vscode.Uri): Promise { diff --git a/src/utils/wslUtils.ts b/src/utils/wslUtils.ts index 7665e291..f6c09f3e 100644 --- a/src/utils/wslUtils.ts +++ b/src/utils/wslUtils.ts @@ -6,13 +6,13 @@ import * as vscode from "vscode"; export function useWsl(): boolean { const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); - return process.platform === 'win32' && leetCodeConfig.get('useWsl') === true + return process.platform === "win32" && leetCodeConfig.get("useWsl") === true; } export function toWslPath(path: string): string { - return cp.execFileSync('wsl', ['--', 'wslpath', '-u', `${path.replace(/\\/g, '/')}`]).toString().trim() + return cp.execFileSync("wsl", ["--", "wslpath", "-u", `${path.replace(/\\/g, "/")}`]).toString().trim(); } export function toWinPath(path: string): string { - return cp.execFileSync('wsl', ['--', 'wslpath', '-w', path]).toString().trim() + return cp.execFileSync("wsl", ["--", "wslpath", "-w", path]).toString().trim(); } From d19d2c40891f8bcb20b3d5b5b18340e02b358ab6 Mon Sep 17 00:00:00 2001 From: purocean Date: Mon, 23 Jul 2018 09:46:51 +0800 Subject: [PATCH 3/5] fix configuration description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 79a5cc17..015a6578 100644 --- a/package.json +++ b/package.json @@ -206,7 +206,7 @@ "type": "boolean", "default": false, "scope": "window", - "description": "Show locked problems." + "description": "Use nodejs inside the Windows Subsystem for Linux." } } } From 6fe46843e35cf91c21f28caa02f1cf89c07219ce Mon Sep 17 00:00:00 2001 From: purocean Date: Tue, 24 Jul 2018 14:51:25 +0800 Subject: [PATCH 4/5] adjust wsl parameter --- src/leetCodeManager.ts | 2 +- src/utils/cpUtils.ts | 2 +- src/utils/wslUtils.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 06ffd664..33c098b1 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -46,7 +46,7 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager { let result: string = ""; const childProc: cp.ChildProcess = wsl.useWsl() - ? cp.spawn("wsl", ["--", "node", leetCodeBinaryPath, "user", "-l"], { shell: true }) + ? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true }) : cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true }); childProc.stdout.on("data", (data: string | Buffer) => { diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index 082b0690..a8f6fd9a 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -9,7 +9,7 @@ export async function executeCommand(channel: vscode.OutputChannel, command: str let result: string = ""; const childProc: cp.ChildProcess = wsl.useWsl() - ? cp.spawn("wsl", ["--", command].concat(args), options) + ? cp.spawn("wsl", [command].concat(args), options) : cp.spawn(command, args, options); childProc.stdout.on("data", (data: string | Buffer) => { diff --git a/src/utils/wslUtils.ts b/src/utils/wslUtils.ts index f6c09f3e..d2b0d566 100644 --- a/src/utils/wslUtils.ts +++ b/src/utils/wslUtils.ts @@ -10,9 +10,9 @@ export function useWsl(): boolean { } export function toWslPath(path: string): string { - return cp.execFileSync("wsl", ["--", "wslpath", "-u", `${path.replace(/\\/g, "/")}`]).toString().trim(); + return cp.execFileSync("wsl", ["wslpath", "-u", `${path.replace(/\\/g, "/")}`]).toString().trim(); } export function toWinPath(path: string): string { - return cp.execFileSync("wsl", ["--", "wslpath", "-w", path]).toString().trim(); + return cp.execFileSync("wsl", ["wslpath", "-w", path]).toString().trim(); } From bf291f2b99e02e575316b2b3b46c121d56250be1 Mon Sep 17 00:00:00 2001 From: purocean Date: Tue, 24 Jul 2018 15:03:05 +0800 Subject: [PATCH 5/5] fix configuration description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 015a6578..81fbb217 100644 --- a/package.json +++ b/package.json @@ -206,7 +206,7 @@ "type": "boolean", "default": false, "scope": "window", - "description": "Use nodejs inside the Windows Subsystem for Linux." + "description": "Use Node.js inside the Windows Subsystem for Linux." } } }