-
Notifications
You must be signed in to change notification settings - Fork 666
Enhanced Tree view #94
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
Changes from 13 commits
e90b44e
c1a58b2
ce204b9
236a020
64badc5
d648f8a
21723fe
39071bf
3cb8743
86f17ac
7e00062
0153d84
d9abd5f
7bffd54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the MIT license. | ||
|
||
import * as cp from "child_process"; | ||
import * as fs from "fs"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
import * as path from "path"; | ||
import * as vscode from "vscode"; | ||
import { Endpoint } from "./shared"; | ||
|
@@ -10,28 +11,31 @@ import { DialogOptions, openUrl } from "./utils/uiUtils"; | |
import * as wsl from "./utils/wslUtils"; | ||
|
||
class LeetCodeExecutor { | ||
private leetCodeBinaryPath: string; | ||
private leetCodeBinaryPathInWsl: string; | ||
private leetCodeRootPath: string; | ||
private leetCodeRootPathInWsl: string; | ||
|
||
constructor() { | ||
this.leetCodeBinaryPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode"); | ||
this.leetCodeBinaryPathInWsl = ""; | ||
this.leetCodeRootPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli"); | ||
this.leetCodeRootPathInWsl = ""; | ||
} | ||
|
||
public async getLeetCodeBinaryPath(): Promise<string> { | ||
public async getLeetCodeRootPath(): Promise<string> { // not wrapped by "" | ||
if (wsl.useWsl()) { | ||
if (!this.leetCodeBinaryPathInWsl) { | ||
this.leetCodeBinaryPathInWsl = `${await wsl.toWslPath(this.leetCodeBinaryPath)}`; | ||
if (!this.leetCodeRootPathInWsl) { | ||
this.leetCodeRootPathInWsl = `${await wsl.toWslPath(this.leetCodeRootPath)}`; | ||
} | ||
return `"${this.leetCodeBinaryPathInWsl}"`; | ||
return `${this.leetCodeRootPathInWsl}`; | ||
} | ||
return `"${this.leetCodeBinaryPath}"`; | ||
return `${this.leetCodeRootPath}`; | ||
} | ||
|
||
public async getLeetCodeBinaryPath(): Promise<string> { // wrapped by "" | ||
return `"${path.join(await this.getLeetCodeRootPath(), "bin", "leetcode")}"`; | ||
} | ||
|
||
public async meetRequirements(): Promise<boolean> { | ||
try { | ||
await this.executeCommandEx("node", ["-v"]); | ||
return true; | ||
} catch (error) { | ||
const choice: vscode.MessageItem | undefined = await vscode.window.showErrorMessage( | ||
"LeetCode extension needs Node.js installed in environment path", | ||
|
@@ -42,6 +46,12 @@ class LeetCodeExecutor { | |
} | ||
return false; | ||
} | ||
try { // Check company plugin | ||
await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-e", "company"]); | ||
} catch (error) { // Download company plugin and activate | ||
await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "plugin", "-i", "company"]); | ||
} | ||
return true; | ||
} | ||
|
||
public async deleteCache(): Promise<string> { | ||
|
@@ -100,6 +110,19 @@ class LeetCodeExecutor { | |
} | ||
} | ||
|
||
public async getCompaniesAndTags(): Promise<{ companies: { [key: string]: string[] }, tags: { [key: string]: string[] } }> { | ||
// preprocess the plugin source | ||
const componiesTagsPath: string = path.join(await leetCodeExecutor.getLeetCodeRootPath(), "lib", "plugins", "company.js"); | ||
const componiesTagsSrc: string = (await fs.readFileSync(componiesTagsPath, "utf8")).replace( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something went wrong with my usage. |
||
"module.exports = plugin", | ||
"module.exports = { COMPONIES, TAGS }", | ||
); | ||
// require plugin from modified string | ||
const requireFromString: (src: string, path: string) => any = require("require-from-string"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we import the module? For example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the |
||
const { COMPONIES, TAGS } = requireFromString(componiesTagsSrc, componiesTagsPath); | ||
return { companies: COMPONIES, tags: TAGS }; | ||
} | ||
|
||
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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to put the
IProblem
definition andIProblemDefault
to other places instead of in this file.Let's put them into
shared.ts
, I'll refactor this part later.