Move fork impl into serverProcess

This commit is contained in:
Matt Bierner 2020-07-22 00:17:16 -07:00
parent 2f10b23958
commit c6ce8f26cc
2 changed files with 30 additions and 46 deletions

View file

@ -3,17 +3,17 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { ChildProcess } from 'child_process';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import type { Readable } from 'stream';
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import type * as Proto from '../protocol';
import { TsServerProcess, TsServerProcessKind } from './server';
import { TypeScriptServiceConfiguration } from '../utils/configuration';
import { fork } from '../utils/electron';
import { TypeScriptVersionManager } from './versionManager';
import { Disposable } from '../utils/dispose';
import { TsServerProcess, TsServerProcessKind } from './server';
import { TypeScriptVersionManager } from './versionManager';
const localize = nls.loadMessageBundle();
@ -149,20 +149,36 @@ export class ChildServerProcess extends Disposable implements TsServerProcess {
versionManager.reset();
tsServerPath = versionManager.currentVersion.tsServerPath;
}
const childProcess = fork(tsServerPath, args, this.getForkOptions(kind, configuration));
const childProcess = child_process.fork(tsServerPath, args, {
silent: true,
cwd: undefined,
env: this.generatePatchedEnv(process.env, tsServerPath),
execArgv: this.getExecArgv(kind, configuration),
});
return new ChildServerProcess(childProcess);
}
private static getForkOptions(kind: TsServerProcessKind, configuration: TypeScriptServiceConfiguration) {
private static generatePatchedEnv(env: any, modulePath: string): any {
const newEnv = Object.assign({}, env);
newEnv['ELECTRON_RUN_AS_NODE'] = '1';
newEnv['NODE_PATH'] = path.join(modulePath, '..', '..', '..');
// Ensure we always have a PATH set
newEnv['PATH'] = newEnv['PATH'] || process.env.PATH;
return newEnv;
}
private static getExecArgv(kind: TsServerProcessKind, configuration: TypeScriptServiceConfiguration): string[] {
const debugPort = this.getDebugPort(kind);
const inspectFlag = process.env['TSS_DEBUG_BRK'] ? '--inspect-brk' : '--inspect';
const tsServerForkOptions: any = {
execArgv: [
...(debugPort ? [`${inspectFlag}=${debugPort}`] : []),
...(configuration.maxTsServerMemory ? [`--max-old-space-size=${configuration.maxTsServerMemory}`] : [])
]
};
return tsServerForkOptions;
return [
...(debugPort ? [`${inspectFlag}=${debugPort}`] : []),
...(configuration.maxTsServerMemory ? [`--max-old-space-size=${configuration.maxTsServerMemory}`] : [])
];
}
private static getDebugPort(kind: TsServerProcessKind): number | undefined {
@ -181,7 +197,7 @@ export class ChildServerProcess extends Disposable implements TsServerProcess {
}
private constructor(
private readonly _process: ChildProcess,
private readonly _process: child_process.ChildProcess,
) {
super();
this._reader = this._register(new Reader<Proto.Response>(this._process.stdout!));

View file

@ -6,7 +6,6 @@
import * as temp from './temp';
import path = require('path');
import fs = require('fs');
import cp = require('child_process');
import process = require('process');
@ -39,34 +38,3 @@ export const getInstanceDir = (() => {
export function getTempFile(prefix: string): string {
return path.join(getInstanceDir(), `${prefix}-${temp.makeRandomHexString(20)}.tmp`);
}
function generatePatchedEnv(env: any, modulePath: string): any {
const newEnv = Object.assign({}, env);
newEnv['ELECTRON_RUN_AS_NODE'] = '1';
newEnv['NODE_PATH'] = path.join(modulePath, '..', '..', '..');
// Ensure we always have a PATH set
newEnv['PATH'] = newEnv['PATH'] || process.env.PATH;
return newEnv;
}
export interface ForkOptions {
readonly cwd?: string;
readonly execArgv?: string[];
}
export function fork(
modulePath: string,
args: readonly string[],
options: ForkOptions,
): cp.ChildProcess {
const newEnv = generatePatchedEnv(process.env, modulePath);
return cp.fork(modulePath, args, {
silent: true,
cwd: options.cwd,
env: newEnv,
execArgv: options.execArgv
});
}