Skip to content

fixed use wsl feature (issue#509) #510

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 7 commits into from
Feb 22, 2020
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
17 changes: 4 additions & 13 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ import { toWslPath, useWsl } from "./utils/wslUtils";

class LeetCodeExecutor implements 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();
this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("leetcode.nodePath")) {
Expand All @@ -29,18 +27,11 @@ class LeetCodeExecutor implements Disposable {
}, this);
}

public async getLeetCodeRootPath(): Promise<string> { // not wrapped by ""
public async getLeetCodeBinaryPath(): Promise<string> {
if (wsl.useWsl()) {
if (!this.leetCodeRootPathInWsl) {
this.leetCodeRootPathInWsl = `${await wsl.toWslPath(this.leetCodeRootPath)}`;
}
return `${this.leetCodeRootPathInWsl}`;
return `${await wsl.toWslPath(`"${path.join(this.leetCodeRootPath, "bin", "leetcode")}"`)}`;
}
return `${this.leetCodeRootPath}`;
}

public async getLeetCodeBinaryPath(): Promise<string> { // wrapped by ""
return `"${path.join(await this.getLeetCodeRootPath(), "bin", "leetcode")}"`;
return `"${path.join(this.leetCodeRootPath, "bin", "leetcode")}"`;
}

public async meetRequirements(): Promise<boolean> {
Expand Down Expand Up @@ -168,7 +159,7 @@ class LeetCodeExecutor implements Disposable {

public async getCompaniesAndTags(): Promise<{ companies: { [key: string]: string[] }, tags: { [key: string]: string[] } }> {
// preprocess the plugin source
const companiesTagsPath: string = path.join(await leetCodeExecutor.getLeetCodeRootPath(), "lib", "plugins", "company.js");
const companiesTagsPath: string = path.join(this.leetCodeRootPath, "lib", "plugins", "company.js");
const companiesTagsSrc: string = (await fse.readFile(companiesTagsPath, "utf8")).replace(
"module.exports = plugin",
"module.exports = { COMPONIES, TAGS }",
Expand Down
5 changes: 4 additions & 1 deletion src/utils/wslUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ export async function toWslPath(path: string): Promise<string> {
}

export async function toWinPath(path: string): Promise<string> {
return (await executeCommand("wsl", ["wslpath", "-w", `"${path}"`])).trim();
if (path.startsWith("\\mnt\\")) {
return (await executeCommand("wsl", ["wslpath", "-w", `"${path.replace(/\\/g, "/").substr(0, 6)}"`])).trim() + path.substr(7);
}
return (await executeCommand("wsl", ["wslpath", "-w", "/"])).trim() + path;
}