Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc6c4e9c

Browse files
authored
Can specify the node path (LeetCode-OpenSource#233)
1 parenta6367f5 commitc6c4e9c

File tree

5 files changed

+65
-27
lines changed

5 files changed

+65
-27
lines changed

‎package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
"type":"boolean",
272272
"default":false,
273273
"scope":"application",
274-
"description":"UseNode.js insidethe Windows Subsystem for Linux."
274+
"description":"Use the Windows Subsystem for Linux."
275275
},
276276
"leetcode.endpoint": {
277277
"type":"string",
@@ -286,13 +286,19 @@
286286
"leetcode.outputFolder": {
287287
"type":"string",
288288
"scope":"application",
289-
"description":"Specify the relative path to save the problem files."
289+
"description":"The relative path to save the problem files."
290290
},
291291
"leetcode.enableStatusBar": {
292292
"type":"boolean",
293293
"default":true,
294294
"scope":"application",
295-
"description":"Specify whether the LeetCode status bar will be shown or not."
295+
"description":"Show the LeetCode status bar or not."
296+
},
297+
"leetcode.nodePath": {
298+
"type":"string",
299+
"default":"node",
300+
"scope":"application",
301+
"description":"The Node.js executable path."
296302
}
297303
}
298304
}

‎src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4141
leetCodePreviewProvider,
4242
leetCodeResultProvider,
4343
leetCodeSolutionProvider,
44+
leetCodeExecutor,
4445
vscode.window.createTreeView("leetCodeExplorer",{treeDataProvider:leetCodeTreeDataProvider,showCollapseAll:true}),
4546
vscode.languages.registerCodeLensProvider({scheme:"file"},codeLensProvider),
4647
vscode.commands.registerCommand("leetcode.deleteCache",()=>cache.deleteCache()),

‎src/leetCodeExecutor.ts

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ import * as cp from "child_process";
55
import*asfsefrom"fs-extra";
66
import*aspathfrom"path";
77
import*asrequireFromStringfrom"require-from-string";
8-
import*asvscodefrom"vscode";
8+
import{ConfigurationChangeEvent,Disposable,MessageItem,window,workspace,WorkspaceConfiguration}from"vscode";
99
import{Endpoint,IProblem,supportedPlugins}from"./shared";
1010
import{executeCommand,executeCommandWithProgress}from"./utils/cpUtils";
1111
import{genFileName}from"./utils/problemUtils";
1212
import{DialogOptions,openUrl}from"./utils/uiUtils";
1313
import*aswslfrom"./utils/wslUtils";
14+
import{toWslPath,useWsl}from"./utils/wslUtils";
1415

15-
classLeetCodeExecutor{
16+
classLeetCodeExecutorimplementsDisposable{
1617
privateleetCodeRootPath:string;
1718
privateleetCodeRootPathInWsl:string;
19+
privatenodeExecutable:string;
20+
privateconfigurationChangeListener:Disposable;
1821

1922
constructor(){
2023
this.leetCodeRootPath=path.join(__dirname,"..","..","node_modules","vsc-leetcode-cli");
2124
this.leetCodeRootPathInWsl="";
25+
this.nodeExecutable=this.getNodePath();
26+
this.configurationChangeListener=workspace.onDidChangeConfiguration((event:ConfigurationChangeEvent)=>{
27+
if(event.affectsConfiguration("leetcode.nodePath")){
28+
this.nodeExecutable=this.getNodePath();
29+
}
30+
},this);
2231
}
2332

2433
publicasyncgetLeetCodeRootPath():Promise<string>{// not wrapped by ""
@@ -36,10 +45,18 @@ class LeetCodeExecutor {
3645
}
3746

3847
publicasyncmeetRequirements():Promise<boolean>{
48+
if(this.nodeExecutable!=="node"){
49+
if(!awaitfse.pathExists(this.nodeExecutable)){
50+
thrownewError(`The Node.js executable does not exist on path${this.nodeExecutable}`);
51+
}
52+
if(useWsl()){
53+
this.nodeExecutable=awaittoWslPath(this.nodeExecutable);
54+
}
55+
}
3956
try{
40-
awaitthis.executeCommandEx("node",["-v"]);
57+
awaitthis.executeCommandEx(this.nodeExecutable,["-v"]);
4158
}catch(error){
42-
constchoice:vscode.MessageItem|undefined=awaitvscode.window.showErrorMessage(
59+
constchoice:MessageItem|undefined=awaitwindow.showErrorMessage(
4360
"LeetCode extension needs Node.js installed in environment path",
4461
DialogOptions.open,
4562
);
@@ -50,28 +67,28 @@ class LeetCodeExecutor {
5067
}
5168
for(constpluginofsupportedPlugins){
5269
try{// Check plugin
53-
awaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"plugin","-e",plugin]);
70+
awaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"plugin","-e",plugin]);
5471
}catch(error){// Download plugin and activate
55-
awaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"plugin","-i",plugin]);
72+
awaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"plugin","-i",plugin]);
5673
}
5774
}
5875
returntrue;
5976
}
6077

6178
publicasyncdeleteCache():Promise<string>{
62-
returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"cache","-d"]);
79+
returnawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"cache","-d"]);
6380
}
6481

6582
publicasyncgetUserInfo():Promise<string>{
66-
returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"user"]);
83+
returnawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"user"]);
6784
}
6885

6986
publicasyncsignOut():Promise<string>{
70-
returnawaitawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"user","-L"]);
87+
returnawaitawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"user","-L"]);
7188
}
7289

7390
publicasynclistProblems(showLocked:boolean):Promise<string>{
74-
returnawaitthis.executeCommandEx("node",showLocked ?
91+
returnawaitthis.executeCommandEx(this.nodeExecutable,showLocked ?
7592
[awaitthis.getLeetCodeBinaryPath(),"list"] :
7693
[awaitthis.getLeetCodeBinaryPath(),"list","-q","L"],
7794
);
@@ -82,37 +99,37 @@ class LeetCodeExecutor {
8299
constfilePath:string=path.join(outDir,fileName);
83100

84101
if(!awaitfse.pathExists(filePath)){
85-
constcodeTemplate:string=awaitthis.executeCommandWithProgressEx("Fetching problem data...","node",[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"-cx","-l",language]);
102+
constcodeTemplate:string=awaitthis.executeCommandWithProgressEx("Fetching problem data...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"-cx","-l",language]);
86103
awaitfse.writeFile(filePath,codeTemplate);
87104
}
88105

89106
returnfilePath;
90107
}
91108

92109
publicasyncshowSolution(problemNode:IProblem,language:string):Promise<string>{
93-
constsolution:string=awaitthis.executeCommandWithProgressEx("Fetching top voted solution from discussions...","node",[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"--solution","-l",language]);
110+
constsolution:string=awaitthis.executeCommandWithProgressEx("Fetching top voted solution from discussions...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"--solution","-l",language]);
94111
returnsolution;
95112
}
96113

97114
publicasyncgetDescription(problemNode:IProblem):Promise<string>{
98-
returnawaitthis.executeCommandWithProgressEx("Fetching problem description...","node",[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"-x"]);
115+
returnawaitthis.executeCommandWithProgressEx("Fetching problem description...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"-x"]);
99116
}
100117

101118
publicasynclistSessions():Promise<string>{
102-
returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"session"]);
119+
returnawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"session"]);
103120
}
104121

105122
publicasyncenableSession(name:string):Promise<string>{
106-
returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"session","-e",name]);
123+
returnawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"session","-e",name]);
107124
}
108125

109126
publicasynccreateSession(name:string):Promise<string>{
110-
returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"session","-c",name]);
127+
returnawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"session","-c",name]);
111128
}
112129

113130
publicasyncsubmitSolution(filePath:string):Promise<string>{
114131
try{
115-
returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...","node",[awaitthis.getLeetCodeBinaryPath(),"submit",`"${filePath}"`]);
132+
returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"submit",`"${filePath}"`]);
116133
}catch(error){
117134
if(error.result){
118135
returnerror.result;
@@ -123,18 +140,18 @@ class LeetCodeExecutor {
123140

124141
publicasynctestSolution(filePath:string,testString?:string):Promise<string>{
125142
if(testString){
126-
returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...","node",[awaitthis.getLeetCodeBinaryPath(),"test",`"${filePath}"`,"-t",`${testString}`]);
143+
returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"test",`"${filePath}"`,"-t",`${testString}`]);
127144
}
128-
returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...","node",[awaitthis.getLeetCodeBinaryPath(),"test",`"${filePath}"`]);
145+
returnawaitthis.executeCommandWithProgressEx("Submitting to LeetCode...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"test",`"${filePath}"`]);
129146
}
130147

131148
publicasyncswitchEndpoint(endpoint:string):Promise<string>{
132149
switch(endpoint){
133150
caseEndpoint.LeetCodeCN:
134-
returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"plugin","-e","leetcode.cn"]);
151+
returnawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"plugin","-e","leetcode.cn"]);
135152
caseEndpoint.LeetCode:
136153
default:
137-
returnawaitthis.executeCommandEx("node",[awaitthis.getLeetCodeBinaryPath(),"plugin","-d","leetcode.cn"]);
154+
returnawaitthis.executeCommandEx(this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"plugin","-d","leetcode.cn"]);
138155
}
139156
}
140157

@@ -149,6 +166,19 @@ class LeetCodeExecutor {
149166
return{companies:COMPONIES,tags:TAGS};
150167
}
151168

169+
publicgetnode():string{
170+
returnthis.nodeExecutable;
171+
}
172+
173+
publicdispose():void{
174+
this.configurationChangeListener.dispose();
175+
}
176+
177+
privategetNodePath():string{
178+
constextensionConfig:WorkspaceConfiguration=workspace.getConfiguration("leetcode",null);
179+
returnextensionConfig.get<string>("nodePath","node"/* default value */);
180+
}
181+
152182
privateasyncexecuteCommandEx(command:string,args:string[],options:cp.SpawnOptions={shell:true}):Promise<string>{
153183
if(wsl.useWsl()){
154184
returnawaitexecuteCommand("wsl",[command].concat(args),options);

‎src/leetCodeManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class LeetCodeManager extends EventEmitter {
4242
constleetCodeBinaryPath:string=awaitleetCodeExecutor.getLeetCodeBinaryPath();
4343

4444
constchildProc:cp.ChildProcess=wsl.useWsl()
45-
?cp.spawn("wsl",["node",leetCodeBinaryPath,"user","-l"],{shell:true})
46-
:cp.spawn("node",[leetCodeBinaryPath,"user","-l"],{
45+
?cp.spawn("wsl",[leetCodeExecutor.node,leetCodeBinaryPath,"user","-l"],{shell:true})
46+
:cp.spawn(leetCodeExecutor.node,[leetCodeBinaryPath,"user","-l"],{
4747
shell:true,
4848
env:createEnvOption(),
4949
});

‎src/utils/wslUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
import*asvscodefrom"vscode";
55
import{executeCommand}from"./cpUtils";
6+
import{isWindows}from"./osUtils";
67

78
exportfunctionuseWsl():boolean{
89
constleetCodeConfig:vscode.WorkspaceConfiguration=vscode.workspace.getConfiguration("leetcode");
9-
returnprocess.platform==="win32"&&leetCodeConfig.get<boolean>("useWsl")===true;
10+
returnisWindows()&&leetCodeConfig.get<boolean>("useWsl")===true;
1011
}
1112

1213
exportasyncfunctiontoWslPath(path:string):Promise<string>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp