Skip to content

Commit

Permalink
Pass http proxy as env to command (fix #136, #117) (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen authored and jdneo committed Feb 24, 2019
1 parent 7ca9b8a commit 06e33d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as vscode from "vscode";
import { leetCodeChannel } from "./leetCodeChannel";
import { leetCodeExecutor } from "./leetCodeExecutor";
import { UserStatus } from "./shared";
import { createEnvOption } from "./utils/cpUtils";
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
import * as wsl from "./utils/wslUtils";

Expand Down Expand Up @@ -42,7 +43,10 @@ class LeetCodeManager extends EventEmitter {

const childProc: cp.ChildProcess = wsl.useWsl()
? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true })
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], {
shell: true,
env: createEnvOption(),
});

childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
Expand Down
17 changes: 16 additions & 1 deletion src/utils/cpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function executeCommand(command: string, args: string[], options: c
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
let result: string = "";

const childProc: cp.ChildProcess = cp.spawn(command, args, options);
const childProc: cp.ChildProcess = cp.spawn(command, args, { ...options, env: createEnvOption() });

childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
Expand Down Expand Up @@ -45,3 +45,18 @@ export async function executeCommandWithProgress(message: string, command: strin
});
return result;
}

// clone process.env and add http proxy
export function createEnvOption(): {} {
const proxy: string | undefined = getHttpAgent();
if (proxy) {
const env: any = Object.create(process.env);
env.http_proxy = proxy;
return env;
}
return process.env;
}

function getHttpAgent(): string | undefined {
return vscode.workspace.getConfiguration("http").get<string>("proxy");
}

0 comments on commit 06e33d2

Please sign in to comment.