Archived
1
0

Make preserveEnv return a new object

Modifying the object didn't feel quite right, plus this makes the code a
bit more compact.
This commit is contained in:
Asher 2019-04-29 11:49:59 -05:00
parent 1622fd4152
commit cdb900aca8
No known key found for this signature in database
GPG Key ID: 7BB4BA9C783D2BBC
5 changed files with 20 additions and 39 deletions

View File

@ -236,8 +236,11 @@ export const isPromise = (value: any): value is Promise<any> => {
* When spawning VS Code tries to preserve the environment but since it's in
* the browser, it doesn't work.
*/
export const preserveEnv = (options?: { env?: NodeJS.ProcessEnv } | null): void => {
if (options && options.env) {
options.env = { ...process.env, ...options.env };
}
export const withEnv = <T extends { env?: NodeJS.ProcessEnv }>(options?: T): T | undefined => {
return options && options.env ? {
...options,
env: {
...process.env, ...options.env,
},
} : options;
};

View File

@ -1,6 +1,6 @@
import * as cp from "child_process";
import { ServerProxy } from "../../common/proxy";
import { preserveEnv } from "../../common/util";
import { withEnv } from "../../common/util";
import { WritableProxy, ReadableProxy } from "./stream";
// tslint:disable completed-docs
@ -71,21 +71,15 @@ export class ChildProcessModuleProxy {
options?: { encoding?: string | null } & cp.ExecOptions | null,
callback?: ((error: cp.ExecException | null, stdin: string | Buffer, stdout: string | Buffer) => void),
): Promise<ChildProcessProxies> {
preserveEnv(options);
return this.returnProxies(cp.exec(command, options, callback));
return this.returnProxies(cp.exec(command, options && withEnv(options), callback));
}
public async fork(modulePath: string, args?: string[], options?: cp.ForkOptions): Promise<ChildProcessProxies> {
preserveEnv(options);
return this.returnProxies((this.forkProvider || cp.fork)(modulePath, args, options));
return this.returnProxies((this.forkProvider || cp.fork)(modulePath, args, withEnv(options)));
}
public async spawn(command: string, args?: string[], options?: cp.SpawnOptions): Promise<ChildProcessProxies> {
preserveEnv(options);
return this.returnProxies(cp.spawn(command, args, options));
return this.returnProxies(cp.spawn(command, args, withEnv(options)));
}
private returnProxies(process: cp.ChildProcess): ChildProcessProxies {

View File

@ -2,7 +2,7 @@
import { EventEmitter } from "events";
import * as pty from "node-pty";
import { ServerProxy } from "../../common/proxy";
import { preserveEnv } from "../../common/util";
import { withEnv } from "../../common/util";
// tslint:disable completed-docs
@ -66,8 +66,6 @@ export class NodePtyProcessProxy extends ServerProxy {
*/
export class NodePtyModuleProxy {
public async spawn(file: string, args: string[] | string, options: pty.IPtyForkOptions): Promise<NodePtyProcessProxy> {
preserveEnv(options);
return new NodePtyProcessProxy(require("node-pty").spawn(file, args, options));
return new NodePtyProcessProxy(require("node-pty").spawn(file, args, withEnv(options)));
}
}

View File

@ -1,6 +1,6 @@
import { field, logger } from "@coder/logger";
import { ServerMessage, SharedProcessActive } from "@coder/protocol/src/proto";
import { preserveEnv } from "@coder/protocol";
import { withEnv } from "@coder/protocol";
import { ChildProcess, fork, ForkOptions } from "child_process";
import { randomFillSync } from "crypto";
import * as fs from "fs";
@ -175,21 +175,12 @@ const bold = (text: string | number): string | number => {
}
if (options.installExtension) {
let forkOptions = {
env: {
VSCODE_ALLOW_IO: "true"
}
}
preserveEnv(forkOptions);
const fork = forkModule("vs/code/node/cli", [
"--user-data-dir", dataDir,
"--builtin-extensions-dir", builtInExtensionsDir,
"--extensions-dir", extensionsDir,
"--install-extension", options.installExtension,
], forkOptions, dataDir);
], withEnv({ env: { VSCODE_ALLOW_IO: "true" } }), dataDir);
fork.stdout.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.info(l)));
fork.stderr.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.error(l)));

View File

@ -7,7 +7,7 @@ import { ParsedArgs } from "vs/platform/environment/common/environment";
import { Emitter } from "@coder/events/src";
import { retry } from "@coder/ide/src/retry";
import { logger, field, Level } from "@coder/logger";
import { preserveEnv } from "@coder/protocol";
import { withEnv } from "@coder/protocol";
export enum SharedProcessState {
Stopped,
@ -89,15 +89,10 @@ export class SharedProcess {
this.activeProcess.kill();
}
let forkOptions = {
env: {
VSCODE_ALLOW_IO: "true"
}
}
preserveEnv(forkOptions);
const activeProcess = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", [], forkOptions, this.userDataDir);
const activeProcess = forkModule(
"vs/code/electron-browser/sharedProcess/sharedProcessMain", [],
withEnv({ env: { VSCODE_ALLOW_IO: "true" } }), this.userDataDir,
);
this.activeProcess = activeProcess;
await new Promise((resolve, reject): void => {