Skip to content

Commit bc1f640

Browse files
authored
Merge pull request #45 from ccagml/try_no_node
尝试不需要额外安装node环境,使用vscode自带的node版本
2 parents 05efd45 + a2724a1 commit bc1f640

13 files changed

+175
-18
lines changed

.vscode/launch.json

+20-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@
4343
"${workspaceRoot}/out/test/**/*.js"
4444
],
4545
"preLaunchTask": "npm"
46-
}
46+
},
47+
{
48+
"name": "windows_master",
49+
// "type": "extensionHost",
50+
"type": "extensionHost",
51+
"request": "launch",
52+
"runtimeExecutable": "${execPath}",
53+
"args": [
54+
"--extensionDevelopmentPath=${workspaceRoot}",
55+
"--trace-warnings"
56+
],
57+
// "autoAttachChildProcesses": true,
58+
// "stopOnEntry": false,
59+
"sourceMaps": true,
60+
"outFiles": [
61+
"${workspaceRoot}/out/src/**/*.js"
62+
],
63+
"trace": true,
64+
"preLaunchTask": "window_npm"
65+
},
4766
]
4867
}

.vscode/tasks.json

+37
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@
88
"npm_do"
99
]
1010
},
11+
{
12+
"label": "window_npm",
13+
"dependsOn": [
14+
"window_npm_rm",
15+
"window_npm_do"
16+
]
17+
},
18+
{
19+
"label": "window_npm_rm",
20+
"type": "shell",
21+
"command": "Remove-item",
22+
"args": [
23+
"out",
24+
"-recurse"
25+
],
26+
// "isBackground": true,
27+
},
28+
{
29+
"label": "window_npm_do",
30+
"type": "shell",
31+
"command": "npm",
32+
"group": {
33+
"kind": "build",
34+
"isDefault": true
35+
},
36+
"args": [
37+
"run",
38+
"compile",
39+
"--loglevel",
40+
"silent"
41+
],
42+
"isBackground": true,
43+
"presentation": {
44+
"reveal": "silent"
45+
},
46+
"problemMatcher": "$tsc-watch"
47+
},
1148
{
1249
"label": "npm_rm",
1350
"type": "shell",

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## version 1.1.6
2+
- 尝试不需要安装node环境
3+
14
## version 1.1.5
25
- 修复PreView Problem 题目元数据显示出错
36

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- 增加精选分类
1919
- 增加剑指Offer、面试金典相关内容
2020
- 增加一键提交全部题目测试用例功能
21+
- 尝试不需要额外安装node环境,使用vscode自带的node版本
2122

2223
# 关于本项目
2324
- [项目地址:https://github.com/ccagml/vscode-leetcode-problem-rating/](https://github.com/ccagml/vscode-leetcode-problem-rating/)

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode problem rating",
44
"description": "为LeetCode题目难度进行打分。避免只有简单、中等、困难三种难度",
5-
"version": "1.1.5",
5+
"version": "1.1.6",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",
@@ -382,6 +382,12 @@
382382
"scope": "application",
383383
"description": "Show a hint to configure commands key binding."
384384
},
385+
"leetcode-problem-rating.useVscodeNode": {
386+
"type": "boolean",
387+
"default": true,
388+
"scope": "machine",
389+
"description": "Try the node version of vecode."
390+
},
385391
"leetcode-problem-rating.useWsl": {
386392
"type": "boolean",
387393
"default": false,

src/commands/show.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,14 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise<vo
284284
if (input instanceof LeetCodeNode) { // Triggerred from explorer
285285
problemInput = input.qid;
286286
} else if (input instanceof vscode.Uri) { // Triggerred from Code Lens/context menu
287-
problemInput = `"${input.fsPath}"`;
287+
if (wsl.useVscodeNode()) {
288+
problemInput = `${input.fsPath}`;
289+
} else {
290+
problemInput = `"${input.fsPath}"`;
291+
if (wsl.useWsl()) {
292+
problemInput = await wsl.toWslPath(input.fsPath);
293+
}
294+
}
288295
} else if (!input) { // Triggerred from command
289296
problemInput = await getActiveFilePath();
290297
}

src/commands/test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ export async function testSolutionDefault(uri?: vscode.Uri, allCase?: boolean):
122122

123123
function parseTestString(test: string): string {
124124
if (wsl.useWsl() || !isWindows()) {
125+
if (wsl.useVscodeNode()) {
126+
return `${test}`;
127+
}
125128
return `'${test}'`;
126129
}
127130

src/extension.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ import { leetCodeSolutionProvider } from "./webview/leetCodeSolutionProvider";
2525
import { leetCodeSubmissionProvider } from "./webview/leetCodeSubmissionProvider";
2626
import { markdownEngine } from "./webview/markdownEngine";
2727
import { ISubmitEvent } from "./shared";
28+
import * as wsl from "./utils/wslUtils";
29+
2830

2931
export async function activate(context: vscode.ExtensionContext): Promise<void> {
3032
try {
31-
if (!await leetCodeExecutor.meetRequirements(context)) {
32-
throw new Error("The environment doesn't meet requirements.");
33+
34+
if (!wsl.useVscodeNode()) {
35+
if (!await leetCodeExecutor.meetRequirements(context)) {
36+
throw new Error("The environment doesn't meet requirements.");
37+
}
3338
}
3439

40+
3541
leetCodeManager.on("statusChanged", () => {
3642
leetCodeStatusBarController.updateStatusBar(leetCodeManager.getStatus(), leetCodeManager.getUser());
3743
leetCodeTreeDataProvider.cleanUserScore();
@@ -91,4 +97,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
9197

9298
export function deactivate(): void {
9399
// Do nothing.
100+
if (0) {
101+
var a = 0;
102+
console.log(a);
103+
}
94104
}

src/leetCodeExecutor.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class LeetCodeExecutor implements Disposable {
2222

2323
constructor() {
2424
// this.leetCodeCliResourcesRootPath = path.join(__dirname, "..", "..", "node_modules", "vsc-leetcode-cli");
25-
this.leetCodeCliResourcesRootPath = path.join(__dirname, "..", "..", "resources");
25+
if (!wsl.useVscodeNode()) {
26+
this.leetCodeCliResourcesRootPath = path.join(__dirname, "..", "..", "resources");
27+
}
2628
this.leetCodeCliRootPath = path.join(__dirname, "..", "..", "out", "src", "vsc-leetcode-cli");
2729
this.nodeExecutable = this.getNodePath();
2830
this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
@@ -33,10 +35,14 @@ class LeetCodeExecutor implements Disposable {
3335
}
3436

3537
public async getLeetCodeBinaryPath(): Promise<string> {
36-
if (wsl.useWsl()) {
37-
return `${await wsl.toWslPath(`"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`)}`;
38+
if (wsl.useVscodeNode()) {
39+
return `${path.join(this.leetCodeCliRootPath, "new_lib", "cli.js")}`;
40+
} else {
41+
if (wsl.useWsl()) {
42+
return `${await wsl.toWslPath(`"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`)}`;
43+
}
44+
return `"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`;
3845
}
39-
return `"${path.join(this.leetCodeCliResourcesRootPath, "bin", "leetcode")}"`;
4046
}
4147

4248
public async meetRequirements(context: ExtensionContext): Promise<boolean> {
@@ -195,6 +201,9 @@ class LeetCodeExecutor implements Disposable {
195201

196202
public async submitSolution(filePath: string): Promise<string> {
197203
try {
204+
if (wsl.useVscodeNode()) {
205+
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "submit", `${filePath}`]);
206+
}
198207
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "submit", `"${filePath}"`]);
199208
} catch (error) {
200209
if (error.result) {
@@ -206,11 +215,20 @@ class LeetCodeExecutor implements Disposable {
206215

207216
public async testSolution(filePath: string, testString?: string, allCase?: boolean): Promise<string> {
208217
if (testString) {
218+
if (wsl.useVscodeNode()) {
219+
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `${filePath}`, "-t", `${testString}`]);
220+
}
209221
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-t", `${testString}`]);
210222
}
211223
if (allCase) {
224+
if (wsl.useVscodeNode()) {
225+
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `${filePath}`, "-a"]);
226+
}
212227
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-a"]);
213228
}
229+
if (wsl.useVscodeNode()) {
230+
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `${filePath}`]);
231+
}
214232
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`]);
215233
}
216234

@@ -252,6 +270,9 @@ class LeetCodeExecutor implements Disposable {
252270
}
253271

254272
private getNodePath(): string {
273+
if (wsl.useVscodeNode()) {
274+
return "node"
275+
}
255276
const extensionConfig: WorkspaceConfiguration = workspace.getConfiguration("leetcode-problem-rating", null);
256277
return extensionConfig.get<string>("nodePath", "node" /* default value */);
257278
}

src/leetCodeManager.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,24 @@ class LeetCodeManager extends EventEmitter {
8686
const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath();
8787

8888
var childProc: cp.ChildProcess;
89-
if (wsl.useWsl()) {
90-
childProc = cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true })
91-
} else {
92-
childProc = cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
93-
shell: true,
89+
90+
if (wsl.useVscodeNode()) {
91+
childProc = cp.fork(await leetCodeExecutor.getLeetCodeBinaryPath(), ["user", commandArg], {
92+
silent: true,
9493
env: createEnvOption(),
9594
});
95+
} else {
96+
if (wsl.useWsl()) {
97+
childProc = cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true })
98+
} else {
99+
childProc = cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
100+
shell: true,
101+
env: createEnvOption(),
102+
});
103+
}
104+
96105
}
106+
97107
childProc.stdout?.on("data", async (data: string | Buffer) => {
98108
data = data.toString();
99109
// vscode.window.showInformationMessage(`cc login msg ${data}.`);

src/utils/cpUtils.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as cp from "child_process";
55
import * as vscode from "vscode";
66
import { leetCodeChannel } from "../leetCodeChannel";
7+
import * as wsl from "../utils/wslUtils";
78

89
interface IExecError extends Error {
910
result?: string;
@@ -12,8 +13,21 @@ interface IExecError extends Error {
1213
export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
1314
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
1415
let result: string = "";
15-
16-
const childProc: cp.ChildProcess = cp.spawn(command, args, { ...options, env: createEnvOption() });
16+
var childProc: cp.ChildProcess
17+
if (wsl.useVscodeNode() && command == "node") {
18+
var newargs: string[] = []
19+
command = args[0];
20+
for (let arg_index = 1; arg_index < args.length; arg_index++) {
21+
newargs.push(args[arg_index])
22+
}
23+
var new_opt = { silent: true, ...options, env: createEnvOption() }
24+
if (false) {
25+
new_opt["execArgv"] = ['--inspect=43210']
26+
}
27+
childProc = cp.fork(command, newargs, new_opt);
28+
} else {
29+
childProc = cp.spawn(command, args, { ...options, env: createEnvOption() });
30+
}
1731

1832
childProc.stdout?.on("data", (data: string | Buffer) => {
1933
data = data.toString();
@@ -43,6 +57,25 @@ export async function executeCommand(command: string, args: string[], options: c
4357
resolve(result);
4458
}
4559
});
60+
61+
// childProc.on("exit", function (code) {
62+
// console.log("disconnect", code)
63+
// if (code !== 0) {
64+
// console.log('child exit code (spawn)', code);
65+
// return reject(new Error(`Command "${command} ${args.toString()}" failed with exit code "${code}".`))
66+
// }
67+
// resolve(result);
68+
// });
69+
70+
// childProc.on("disconnect", function () {
71+
// console.log("disconnect")
72+
// resolve(result);
73+
// });
74+
// childProc.on("message", function (message) {
75+
// console.log("message", message)
76+
// resolve(result);
77+
// });
78+
4679
});
4780
}
4881

src/utils/wslUtils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import * as vscode from "vscode";
55
import { executeCommand } from "./cpUtils";
66
import { isWindows } from "./osUtils";
77

8+
// 用wsl命令的时候,好像没办法用vscode的node
9+
// 相当于使用fork,而不是之前的 spawn(node xxx
10+
export function useVscodeNode(): boolean {
11+
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode-problem-rating");
12+
return !useWsl() && leetCodeConfig.get<boolean>("useVscodeNode") === true;
13+
}
14+
815
export function useWsl(): boolean {
916
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode-problem-rating");
1017
return isWindows() && leetCodeConfig.get<boolean>("useWsl") === true;

0 commit comments

Comments
 (0)