From e4cb64ce66d572df9e5db86c6d91960ea20cd3f2 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Thu, 21 Feb 2019 17:23:33 +0800 Subject: [PATCH 1/3] Pass http proxy as env to command (fix #136, #117) We use leetcode-cli and leetcode-cli uses request to call LeetCode web API. It's lucky that request respects the http proxy env variables(http_proxy, https_proxy), so all we need to do is passing proxy as env variable http_proxy to every command. TODO: do we need to set https_proxy for https request? --- src/leetCodeManager.ts | 6 +++++- src/utils/cpUtils.ts | 3 ++- src/utils/workspaceUtils.ts | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 0456d2d1..43ba4516 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -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 { @@ -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(); diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index eeb7c007..66d58865 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -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 { 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(); diff --git a/src/utils/workspaceUtils.ts b/src/utils/workspaceUtils.ts index 425074df..8a6ab68c 100644 --- a/src/utils/workspaceUtils.ts +++ b/src/utils/workspaceUtils.ts @@ -44,3 +44,18 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise("proxy"); +} + +// 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; +} From ee3f55a742e816c7abb9c08b9f360fa0b8feb841 Mon Sep 17 00:00:00 2001 From: edvardchen Date: Sun, 24 Feb 2019 17:12:59 +0800 Subject: [PATCH 2/3] Move createEnvOption to utils/cpUtils.ts --- src/leetCodeManager.ts | 2 +- src/utils/cpUtils.ts | 16 +++++++++++++++- src/utils/workspaceUtils.ts | 15 --------------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 43ba4516..1db3f5e2 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -7,8 +7,8 @@ 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 { createEnvOption } from "./utils/workspaceUtils"; import * as wsl from "./utils/wslUtils"; class LeetCodeManager extends EventEmitter { diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index 66d58865..a907e7ac 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -4,7 +4,6 @@ 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 { return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => { @@ -46,3 +45,18 @@ export async function executeCommandWithProgress(message: string, command: strin }); return result; } + +function getHttpAgent(): string | undefined { + return vscode.workspace.getConfiguration("http").get("proxy"); +} + +// 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; +} diff --git a/src/utils/workspaceUtils.ts b/src/utils/workspaceUtils.ts index 8a6ab68c..425074df 100644 --- a/src/utils/workspaceUtils.ts +++ b/src/utils/workspaceUtils.ts @@ -44,18 +44,3 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise("proxy"); -} - -// 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; -} From 7b612c37bb045fc88c3e6f15a62809cf97ef1e13 Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Sun, 24 Feb 2019 20:20:18 +0800 Subject: [PATCH 3/3] Modify the order of function --- src/utils/cpUtils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/cpUtils.ts b/src/utils/cpUtils.ts index a907e7ac..6e265e48 100644 --- a/src/utils/cpUtils.ts +++ b/src/utils/cpUtils.ts @@ -46,10 +46,6 @@ export async function executeCommandWithProgress(message: string, command: strin return result; } -function getHttpAgent(): string | undefined { - return vscode.workspace.getConfiguration("http").get("proxy"); -} - // clone process.env and add http proxy export function createEnvOption(): {} { const proxy: string | undefined = getHttpAgent(); @@ -60,3 +56,7 @@ export function createEnvOption(): {} { } return process.env; } + +function getHttpAgent(): string | undefined { + return vscode.workspace.getConfiguration("http").get("proxy"); +}