diff --git a/.vscode/launch.json b/.vscode/launch.json index ef151e3..a211681 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -43,6 +43,25 @@ "${workspaceRoot}/out/test/**/*.js" ], "preLaunchTask": "npm" - } + }, + { + "name": "windows_master", + // "type": "extensionHost", + "type": "extensionHost", + "request": "launch", + "runtimeExecutable": "${execPath}", + "args": [ + "--extensionDevelopmentPath=${workspaceRoot}", + "--trace-warnings" + ], + // "autoAttachChildProcesses": true, + // "stopOnEntry": false, + "sourceMaps": true, + "outFiles": [ + "${workspaceRoot}/out/src/**/*.js" + ], + "trace": true, + "preLaunchTask": "window_npm" + }, ] } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 161fb6a..2161a20 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,6 +8,43 @@ "npm_do" ] }, + { + "label": "window_npm", + "dependsOn": [ + "window_npm_rm", + "window_npm_do" + ] + }, + { + "label": "window_npm_rm", + "type": "shell", + "command": "Remove-item", + "args": [ + "out", + "-recurse" + ], + // "isBackground": true, + }, + { + "label": "window_npm_do", + "type": "shell", + "command": "npm", + "group": { + "kind": "build", + "isDefault": true + }, + "args": [ + "run", + "compile", + "--loglevel", + "silent" + ], + "isBackground": true, + "presentation": { + "reveal": "silent" + }, + "problemMatcher": "$tsc-watch" + }, { "label": "npm_rm", "type": "shell", diff --git a/CHANGELOG.md b/CHANGELOG.md index 98f73fd..597d248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## version 1.1.6 +- 尝试不需要安装node环境 + ## version 1.1.5 - 修复PreView Problem 题目元数据显示出错 diff --git a/README.md b/README.md index ef1c7ee..481017b 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ - 增加精选分类 - 增加剑指Offer、面试金典相关内容 - 增加一键提交全部题目测试用例功能 +- 尝试不需要额外安装node环境,使用vscode自带的node版本 # 关于本项目 - [项目地址:https://github.com/ccagml/vscode-leetcode-problem-rating/](https://github.com/ccagml/vscode-leetcode-problem-rating/) diff --git a/package-lock.json b/package-lock.json index 5390494..4cfb038 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vscode-leetcode-problem-rating", - "version": "1.1.1", + "version": "1.1.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "vscode-leetcode-problem-rating", - "version": "1.1.1", + "version": "1.1.6", "license": "MIT", "dependencies": { "ansi-styles": "3.2.1", diff --git a/package.json b/package.json index 3922862..91a5e1d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-leetcode-problem-rating", "displayName": "LeetCode problem rating", "description": "为LeetCode题目难度进行打分。避免只有简单、中等、困难三种难度", - "version": "1.1.5", + "version": "1.1.6", "author": "ccagml", "publisher": "ccagml", "license": "MIT", @@ -382,6 +382,12 @@ "scope": "application", "description": "Show a hint to configure commands key binding." }, + "leetcode-problem-rating.useVscodeNode": { + "type": "boolean", + "default": true, + "scope": "machine", + "description": "Try the node version of vecode." + }, "leetcode-problem-rating.useWsl": { "type": "boolean", "default": false, diff --git a/src/commands/show.ts b/src/commands/show.ts index c89a28b..5e127d8 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -284,7 +284,14 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise { try { - if (!await leetCodeExecutor.meetRequirements(context)) { - throw new Error("The environment doesn't meet requirements."); + + if (!wsl.useVscodeNode()) { + if (!await leetCodeExecutor.meetRequirements(context)) { + throw new Error("The environment doesn't meet requirements."); + } } + leetCodeManager.on("statusChanged", () => { leetCodeStatusBarController.updateStatusBar(leetCodeManager.getStatus(), leetCodeManager.getUser()); leetCodeTreeDataProvider.cleanUserScore(); @@ -91,4 +97,8 @@ export async function activate(context: vscode.ExtensionContext): Promise export function deactivate(): void { // Do nothing. + if (0) { + var a = 0; + console.log(a); + } } diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index fed6c98..f16b200 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -22,7 +22,9 @@ class LeetCodeExecutor implements Disposable { constructor() { // this.leetCodeCliResourcesRootPath = path.join(__dirname, "..", "..", "node_modules", "vsc-leetcode-cli"); - this.leetCodeCliResourcesRootPath = path.join(__dirname, "..", "..", "resources"); + if (!wsl.useVscodeNode()) { + this.leetCodeCliResourcesRootPath = path.join(__dirname, "..", "..", "resources"); + } this.leetCodeCliRootPath = path.join(__dirname, "..", "..", "out", "src", "vsc-leetcode-cli"); this.nodeExecutable = this.getNodePath(); this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => { @@ -33,10 +35,14 @@ class LeetCodeExecutor implements Disposable { } public async getLeetCodeBinaryPath(): Promise { - if (wsl.useWsl()) { - return `${await wsl.toWslPath(`"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`)}`; + if (wsl.useVscodeNode()) { + return `${path.join(this.leetCodeCliRootPath, "new_lib", "cli.js")}`; + } else { + if (wsl.useWsl()) { + return `${await wsl.toWslPath(`"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`)}`; + } + return `"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`; } - return `"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`; } public async meetRequirements(context: ExtensionContext): Promise { @@ -195,6 +201,9 @@ class LeetCodeExecutor implements Disposable { public async submitSolution(filePath: string): Promise { try { + if (wsl.useVscodeNode()) { + return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "submit", `${filePath}`]); + } return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "submit", `"${filePath}"`]); } catch (error) { if (error.result) { @@ -206,11 +215,20 @@ class LeetCodeExecutor implements Disposable { public async testSolution(filePath: string, testString?: string, allCase?: boolean): Promise { if (testString) { + if (wsl.useVscodeNode()) { + return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `${filePath}`, "-t", `${testString}`]); + } return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-t", `${testString}`]); } if (allCase) { + if (wsl.useVscodeNode()) { + return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `${filePath}`, "-a"]); + } return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-a"]); } + if (wsl.useVscodeNode()) { + return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `${filePath}`]); + } return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`]); } @@ -252,6 +270,9 @@ class LeetCodeExecutor implements Disposable { } private getNodePath(): string { + if (wsl.useVscodeNode()) { + return "node" + } const extensionConfig: WorkspaceConfiguration = workspace.getConfiguration("leetcode-problem-rating", null); return extensionConfig.get("nodePath", "node" /* default value */); } diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 6336b2e..2dc42bd 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -86,14 +86,24 @@ class LeetCodeManager extends EventEmitter { const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath(); var childProc: cp.ChildProcess; - if (wsl.useWsl()) { - childProc = cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true }) - } else { - childProc = cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], { - shell: true, + + if (wsl.useVscodeNode()) { + childProc = cp.fork(await leetCodeExecutor.getLeetCodeBinaryPath(), ["user", commandArg], { + silent: true, env: createEnvOption(), }); + } else { + if (wsl.useWsl()) { + childProc = cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true }) + } else { + childProc = cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], { + shell: true, + env: createEnvOption(), + }); + } + } + childProc.stdout?.on("data", async (data: string | Buffer) => { data = data.toString(); // vscode.window.showInformationMessage(`cc login msg ${data}.`); diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index 5811ce9..d3e9acc 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -4,6 +4,7 @@ import * as cp from "child_process"; import * as vscode from "vscode"; import { leetCodeChannel } from "../leetCodeChannel"; +import * as wsl from "../utils/wslUtils"; interface IExecError extends Error { result?: string; @@ -12,8 +13,21 @@ interface IExecError extends Error { export async function executeCommand(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, env: createEnvOption() }); + var childProc: cp.ChildProcess + if (wsl.useVscodeNode() && command == "node") { + var newargs: string[] = [] + command = args[0]; + for (let arg_index = 1; arg_index < args.length; arg_index++) { + newargs.push(args[arg_index]) + } + var new_opt = { silent: true, ...options, env: createEnvOption() } + if (false) { + new_opt["execArgv"] = ['--inspect=43210'] + } + childProc = cp.fork(command, newargs, new_opt); + } else { + childProc = cp.spawn(command, args, { ...options, env: createEnvOption() }); + } childProc.stdout?.on("data", (data: string | Buffer) => { data = data.toString(); @@ -43,6 +57,25 @@ export async function executeCommand(command: string, args: string[], options: c resolve(result); } }); + + // childProc.on("exit", function (code) { + // console.log("disconnect", code) + // if (code !== 0) { + // console.log('child exit code (spawn)', code); + // return reject(new Error(`Command "${command} ${args.toString()}" failed with exit code "${code}".`)) + // } + // resolve(result); + // }); + + // childProc.on("disconnect", function () { + // console.log("disconnect") + // resolve(result); + // }); + // childProc.on("message", function (message) { + // console.log("message", message) + // resolve(result); + // }); + }); } diff --git a/src/utils/wslUtils.ts b/src/utils/wslUtils.ts index 5bba26b..a785c70 100644 --- a/src/utils/wslUtils.ts +++ b/src/utils/wslUtils.ts @@ -5,6 +5,13 @@ import * as vscode from "vscode"; import { executeCommand } from "./cpUtils"; import { isWindows } from "./osUtils"; +// 用wsl命令的时候,好像没办法用vscode的node +// 相当于使用fork,而不是之前的 spawn(node xxx +export function useVscodeNode(): boolean { + const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode-problem-rating"); + return !useWsl() && leetCodeConfig.get("useVscodeNode") === true; +} + export function useWsl(): boolean { const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode-problem-rating"); return isWindows() && leetCodeConfig.get("useWsl") === true;