Skip to content

Try no node #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
]
}
37 changes: 37 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## version 1.1.6
- 尝试不需要安装node环境

## version 1.1.5
- 修复PreView Problem 题目元数据显示出错

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- 增加精选分类
- 增加剑指Offer、面试金典相关内容
- 增加一键提交全部题目测试用例功能
- 尝试不需要额外安装node环境,使用vscode自带的node版本

# 关于本项目
- [项目地址:https://github.com/ccagml/vscode-leetcode-problem-rating/](https://github.com/ccagml/vscode-leetcode-problem-rating/)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,14 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise<vo
if (input instanceof LeetCodeNode) { // Triggerred from explorer
problemInput = input.qid;
} else if (input instanceof vscode.Uri) { // Triggerred from Code Lens/context menu
problemInput = `"${input.fsPath}"`;
if (wsl.useVscodeNode()) {
problemInput = `${input.fsPath}`;
} else {
problemInput = `"${input.fsPath}"`;
if (wsl.useWsl()) {
problemInput = await wsl.toWslPath(input.fsPath);
}
}
} else if (!input) { // Triggerred from command
problemInput = await getActiveFilePath();
}
Expand Down
3 changes: 3 additions & 0 deletions src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export async function testSolutionDefault(uri?: vscode.Uri, allCase?: boolean):

function parseTestString(test: string): string {
if (wsl.useWsl() || !isWindows()) {
if (wsl.useVscodeNode()) {
return `${test}`;
}
return `'${test}'`;
}

Expand Down
14 changes: 12 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ import { leetCodeSolutionProvider } from "./webview/leetCodeSolutionProvider";
import { leetCodeSubmissionProvider } from "./webview/leetCodeSubmissionProvider";
import { markdownEngine } from "./webview/markdownEngine";
import { ISubmitEvent } from "./shared";
import * as wsl from "./utils/wslUtils";


export async function activate(context: vscode.ExtensionContext): Promise<void> {
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();
Expand Down Expand Up @@ -91,4 +97,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

export function deactivate(): void {
// Do nothing.
if (0) {
var a = 0;
console.log(a);
}
}
29 changes: 25 additions & 4 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -33,10 +35,14 @@ class LeetCodeExecutor implements Disposable {
}

public async getLeetCodeBinaryPath(): Promise<string> {
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<boolean> {
Expand Down Expand Up @@ -195,6 +201,9 @@ class LeetCodeExecutor implements Disposable {

public async submitSolution(filePath: string): Promise<string> {
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) {
Expand All @@ -206,11 +215,20 @@ class LeetCodeExecutor implements Disposable {

public async testSolution(filePath: string, testString?: string, allCase?: boolean): Promise<string> {
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}"`]);
}

Expand Down Expand Up @@ -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<string>("nodePath", "node" /* default value */);
}
Expand Down
20 changes: 15 additions & 5 deletions src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`);
Expand Down
37 changes: 35 additions & 2 deletions src/utils/cpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,8 +13,21 @@ interface IExecError extends Error {
export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
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();
Expand Down Expand Up @@ -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);
// });

});
}

Expand Down
7 changes: 7 additions & 0 deletions src/utils/wslUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>("useVscodeNode") === true;
}

export function useWsl(): boolean {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode-problem-rating");
return isWindows() && leetCodeConfig.get<boolean>("useWsl") === true;
Expand Down