Skip to content

Pass http proxy as env to command (fix #136, #117) #147

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 4 commits into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { leetCodeChannel } from "./leetCodeChannel";
import { leetCodeExecutor } from "./leetCodeExecutor";
import { UserStatus } from "./shared";
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
import { createEnvOption } from "./utils/workspaceUtils";
import * as wsl from "./utils/wslUtils";

class LeetCodeManager extends EventEmitter {
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
3 changes: 2 additions & 1 deletion src/utils/cpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import * as cp from "child_process";
import * as vscode from "vscode";
import { leetCodeChannel } from "../leetCodeChannel";
import { createEnvOption } from "./workspaceUtils";

export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
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
15 changes: 15 additions & 0 deletions src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise<string | unde
export function getWorkspaceConfiguration(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration("leetcode");
}

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

// clone process.env and add http proxy
export function createEnvOption(): {} {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like cpUtils.ts is a better place to put this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'll move it there.

const proxy: string | undefined = getHttpAgent();
if (proxy) {
const env: any = Object.create(process.env);
env.http_proxy = proxy;
return env;
}
return process.env;
}