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 * When spawning VS Code tries to preserve the environment but since it's in
* the browser, it doesn't work. * the browser, it doesn't work.
*/ */
export const preserveEnv = (options?: { env?: NodeJS.ProcessEnv } | null): void => { export const withEnv = <T extends { env?: NodeJS.ProcessEnv }>(options?: T): T | undefined => {
if (options && options.env) { return options && options.env ? {
options.env = { ...process.env, ...options.env }; ...options,
} env: {
...process.env, ...options.env,
},
} : options;
}; };

View File

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

View File

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

View File

@ -1,6 +1,6 @@
import { field, logger } from "@coder/logger"; import { field, logger } from "@coder/logger";
import { ServerMessage, SharedProcessActive } from "@coder/protocol/src/proto"; 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 { ChildProcess, fork, ForkOptions } from "child_process";
import { randomFillSync } from "crypto"; import { randomFillSync } from "crypto";
import * as fs from "fs"; import * as fs from "fs";
@ -175,21 +175,12 @@ const bold = (text: string | number): string | number => {
} }
if (options.installExtension) { if (options.installExtension) {
let forkOptions = {
env: {
VSCODE_ALLOW_IO: "true"
}
}
preserveEnv(forkOptions);
const fork = forkModule("vs/code/node/cli", [ const fork = forkModule("vs/code/node/cli", [
"--user-data-dir", dataDir, "--user-data-dir", dataDir,
"--builtin-extensions-dir", builtInExtensionsDir, "--builtin-extensions-dir", builtInExtensionsDir,
"--extensions-dir", extensionsDir, "--extensions-dir", extensionsDir,
"--install-extension", options.installExtension, "--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.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))); 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 { Emitter } from "@coder/events/src";
import { retry } from "@coder/ide/src/retry"; import { retry } from "@coder/ide/src/retry";
import { logger, field, Level } from "@coder/logger"; import { logger, field, Level } from "@coder/logger";
import { preserveEnv } from "@coder/protocol"; import { withEnv } from "@coder/protocol";
export enum SharedProcessState { export enum SharedProcessState {
Stopped, Stopped,
@ -89,15 +89,10 @@ export class SharedProcess {
this.activeProcess.kill(); this.activeProcess.kill();
} }
let forkOptions = { const activeProcess = forkModule(
env: { "vs/code/electron-browser/sharedProcess/sharedProcessMain", [],
VSCODE_ALLOW_IO: "true" withEnv({ env: { VSCODE_ALLOW_IO: "true" } }), this.userDataDir,
} );
}
preserveEnv(forkOptions);
const activeProcess = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", [], forkOptions, this.userDataDir);
this.activeProcess = activeProcess; this.activeProcess = activeProcess;
await new Promise((resolve, reject): void => { await new Promise((resolve, reject): void => {