8000 Pass http proxy as env to command (fix #136, #117) · edvardchen/vscode-leetcode@e4cb64c · GitHub
[go: up one dir, main page]

Skip to content

Commit e4cb64c

Browse files
author
chenjianhua
committed
Pass http proxy as env to command (fix LeetCode-OpenSource#136, LeetCode-OpenSource#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?
1 parent b781eef commit e4cb64c

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/leetCodeManager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { leetCodeChannel } from "./leetCodeChannel";
88
import { leetCodeExecutor } from "./leetCodeExecutor";
99
import { UserStatus } from "./shared";
1010
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
11+
import { createEnvOption } from "./utils/workspaceUtils";
1112
import * as wsl from "./utils/wslUtils";
1213

1314
class LeetCodeManager extends EventEmitter {
@@ -42,7 +43,10 @@ class LeetCodeManager extends EventEmitter {
4243

4344
const childProc: cp.ChildProcess = wsl.useWsl()
4445
? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true })
45-
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
46+
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], {
47+
shell: true,
48+
env: createEnvOption(),
49+
});
4650

4751
childProc.stdout.on("data", (data: string | Buffer) => {
4852
data = data.toString();

src/utils/cpUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import * as cp from "child_process";
55
import * as vscode from "vscode";
66
import { leetCodeChannel } from "../leetCodeChannel";
7+
import { createEnvOption } from "./workspaceUtils";
78

89
export async function executeCommand(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
910
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
1011
let result: string = "";
1112

12-
const childProc: cp.ChildProcess = cp.spawn(command, args, options);
13+
const childProc: cp.ChildProcess = cp.spawn(command, args, { ...options, env: createEnvOption() });
1314

1415
childProc.stdout.on("data", (data: string | Buffer) => {
1516
data = data.toString();

src/utils/workspaceUtils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise<string | unde
4444
export function getWorkspaceConfiguration(): vscode.WorkspaceConfiguration {
4545
return vscode.workspace.getConfiguration("leetcode");
4646
}
47+
48+
function getHttpAgent(): string | undefined {
49+
return vscode.workspace.getConfiguration("http").get<string>("proxy");
50+
}
51+
52+
// clone process.env and add http proxy
53+
export function createEnvOp 6EAD tion(): {} {
54+
const proxy: string | undefined = getHttpAgent();
55+
if (proxy) {
56+
const env: any = Object.create(process.env);
57+
env.http_proxy = proxy;
58+
return env;
59+
}
60+
return process.env;
61+
}

0 commit comments

Comments
 (0)
0