- Notifications
You must be signed in to change notification settings - Fork669
Can specify the node path#233
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
12 changes: 9 additions & 3 deletionspackage.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionssrc/extension.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
72 changes: 51 additions & 21 deletionssrc/leetCodeExecutor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5,20 +5,29 @@ import * as cp from "child_process"; | ||
import * as fse from "fs-extra"; | ||
import * as path from "path"; | ||
import * as requireFromString from "require-from-string"; | ||
import{ ConfigurationChangeEvent, Disposable, MessageItem, window, workspace, WorkspaceConfiguration } from "vscode"; | ||
import { Endpoint, IProblem, supportedPlugins } from "./shared"; | ||
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils"; | ||
import { genFileName } from "./utils/problemUtils"; | ||
import { DialogOptions, openUrl } from "./utils/uiUtils"; | ||
import * as wsl from "./utils/wslUtils"; | ||
import { toWslPath, useWsl } from "./utils/wslUtils"; | ||
class LeetCodeExecutorimplements Disposable{ | ||
private leetCodeRootPath: string; | ||
private leetCodeRootPathInWsl: string; | ||
private nodeExecutable: string; | ||
private configurationChangeListener: Disposable; | ||
constructor() { | ||
this.leetCodeRootPath = path.join(__dirname, "..", "..", "node_modules", "vsc-leetcode-cli"); | ||
this.leetCodeRootPathInWsl = ""; | ||
this.nodeExecutable = this.getNodePath(); | ||
jdneo marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => { | ||
if (event.affectsConfiguration("leetcode.nodePath")) { | ||
this.nodeExecutable = this.getNodePath(); | ||
} | ||
}, this); | ||
} | ||
public async getLeetCodeRootPath(): Promise<string> { // not wrapped by "" | ||
@@ -36,10 +45,18 @@ class LeetCodeExecutor { | ||
} | ||
public async meetRequirements(): Promise<boolean> { | ||
if (this.nodeExecutable !== "node") { | ||
if (!await fse.pathExists(this.nodeExecutable)) { | ||
throw new Error(`The Node.js executable does not exist on path ${this.nodeExecutable}`); | ||
} | ||
if (useWsl()) { | ||
this.nodeExecutable = await toWslPath(this.nodeExecutable); | ||
} | ||
} | ||
try { | ||
await this.executeCommandEx(this.nodeExecutable, ["-v"]); | ||
} catch (error) { | ||
const choice: MessageItem | undefined = await window.showErrorMessage( | ||
"LeetCode extension needs Node.js installed in environment path", | ||
DialogOptions.open, | ||
); | ||
@@ -50,28 +67,28 @@ class LeetCodeExecutor { | ||
} | ||
for (const plugin of supportedPlugins) { | ||
try { // Check plugin | ||
await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-e", plugin]); | ||
} catch (error) { // Download plugin and activate | ||
await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-i", plugin]); | ||
} | ||
} | ||
return true; | ||
} | ||
public async deleteCache(): Promise<string> { | ||
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "cache", "-d"]); | ||
} | ||
public async getUserInfo(): Promise<string> { | ||
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "user"]); | ||
} | ||
public async signOut(): Promise<string> { | ||
return await await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "user", "-L"]); | ||
} | ||
public async listProblems(showLocked: boolean): Promise<string> { | ||
return await this.executeCommandEx(this.nodeExecutable, showLocked ? | ||
[await this.getLeetCodeBinaryPath(), "list"] : | ||
[await this.getLeetCodeBinaryPath(), "list", "-q", "L"], | ||
); | ||
@@ -82,37 +99,37 @@ class LeetCodeExecutor { | ||
const filePath: string = path.join(outDir, fileName); | ||
if (!await fse.pathExists(filePath)) { | ||
const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...",this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNode.id, "-cx", "-l", language]); | ||
await fse.writeFile(filePath, codeTemplate); | ||
} | ||
return filePath; | ||
} | ||
public async showSolution(problemNode: IProblem, language: string): Promise<string> { | ||
const solution: string = await this.executeCommandWithProgressEx("Fetching top voted solution from discussions...",this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNode.id, "--solution", "-l", language]); | ||
return solution; | ||
} | ||
public async getDescription(problemNode: IProblem): Promise<string> { | ||
return await this.executeCommandWithProgressEx("Fetching problem description...",this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNode.id, "-x"]); | ||
} | ||
public async listSessions(): Promise<string> { | ||
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "session"]); | ||
} | ||
public async enableSession(name: string): Promise<string> { | ||
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "session", "-e", name]); | ||
} | ||
public async createSession(name: string): Promise<string> { | ||
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "session", "-c", name]); | ||
} | ||
public async submitSolution(filePath: string): Promise<string> { | ||
try { | ||
return await this.executeCommandWithProgressEx("Submitting to LeetCode...",this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "submit", `"${filePath}"`]); | ||
} catch (error) { | ||
if (error.result) { | ||
return error.result; | ||
@@ -123,18 +140,18 @@ class LeetCodeExecutor { | ||
public async testSolution(filePath: string, testString?: string): Promise<string> { | ||
if (testString) { | ||
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}"`]); | ||
} | ||
public async switchEndpoint(endpoint: string): Promise<string> { | ||
switch (endpoint) { | ||
case Endpoint.LeetCodeCN: | ||
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-e", "leetcode.cn"]); | ||
case Endpoint.LeetCode: | ||
default: | ||
return await this.executeCommandEx(this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "plugin", "-d", "leetcode.cn"]); | ||
} | ||
} | ||
@@ -149,6 +166,19 @@ class LeetCodeExecutor { | ||
return { companies: COMPONIES, tags: TAGS }; | ||
} | ||
public get node(): string { | ||
return this.nodeExecutable; | ||
} | ||
public dispose(): void { | ||
this.configurationChangeListener.dispose(); | ||
} | ||
private getNodePath(): string { | ||
const extensionConfig: WorkspaceConfiguration = workspace.getConfiguration("leetcode", null); | ||
return extensionConfig.get<string>("nodePath", "node" /* default value */); | ||
} | ||
private async executeCommandEx(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> { | ||
if (wsl.useWsl()) { | ||
return await executeCommand("wsl", [command].concat(args), options); | ||
4 changes: 2 additions & 2 deletionssrc/leetCodeManager.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletionsrc/utils/wslUtils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.