Move fork impl into serverProcess
This commit is contained in:
parent
2f10b23958
commit
c6ce8f26cc
2 changed files with 30 additions and 46 deletions
|
@ -3,17 +3,17 @@
|
||||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
* 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 fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
import type { Readable } from 'stream';
|
import type { Readable } from 'stream';
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as nls from 'vscode-nls';
|
import * as nls from 'vscode-nls';
|
||||||
import type * as Proto from '../protocol';
|
import type * as Proto from '../protocol';
|
||||||
import { TsServerProcess, TsServerProcessKind } from './server';
|
|
||||||
import { TypeScriptServiceConfiguration } from '../utils/configuration';
|
import { TypeScriptServiceConfiguration } from '../utils/configuration';
|
||||||
import { fork } from '../utils/electron';
|
|
||||||
import { TypeScriptVersionManager } from './versionManager';
|
|
||||||
import { Disposable } from '../utils/dispose';
|
import { Disposable } from '../utils/dispose';
|
||||||
|
import { TsServerProcess, TsServerProcessKind } from './server';
|
||||||
|
import { TypeScriptVersionManager } from './versionManager';
|
||||||
|
|
||||||
const localize = nls.loadMessageBundle();
|
const localize = nls.loadMessageBundle();
|
||||||
|
|
||||||
|
@ -149,20 +149,36 @@ export class ChildServerProcess extends Disposable implements TsServerProcess {
|
||||||
versionManager.reset();
|
versionManager.reset();
|
||||||
tsServerPath = versionManager.currentVersion.tsServerPath;
|
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);
|
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 debugPort = this.getDebugPort(kind);
|
||||||
const inspectFlag = process.env['TSS_DEBUG_BRK'] ? '--inspect-brk' : '--inspect';
|
const inspectFlag = process.env['TSS_DEBUG_BRK'] ? '--inspect-brk' : '--inspect';
|
||||||
const tsServerForkOptions: any = {
|
return [
|
||||||
execArgv: [
|
|
||||||
...(debugPort ? [`${inspectFlag}=${debugPort}`] : []),
|
...(debugPort ? [`${inspectFlag}=${debugPort}`] : []),
|
||||||
...(configuration.maxTsServerMemory ? [`--max-old-space-size=${configuration.maxTsServerMemory}`] : [])
|
...(configuration.maxTsServerMemory ? [`--max-old-space-size=${configuration.maxTsServerMemory}`] : [])
|
||||||
]
|
];
|
||||||
};
|
|
||||||
return tsServerForkOptions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getDebugPort(kind: TsServerProcessKind): number | undefined {
|
private static getDebugPort(kind: TsServerProcessKind): number | undefined {
|
||||||
|
@ -181,7 +197,7 @@ export class ChildServerProcess extends Disposable implements TsServerProcess {
|
||||||
}
|
}
|
||||||
|
|
||||||
private constructor(
|
private constructor(
|
||||||
private readonly _process: ChildProcess,
|
private readonly _process: child_process.ChildProcess,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
this._reader = this._register(new Reader<Proto.Response>(this._process.stdout!));
|
this._reader = this._register(new Reader<Proto.Response>(this._process.stdout!));
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
import * as temp from './temp';
|
import * as temp from './temp';
|
||||||
import path = require('path');
|
import path = require('path');
|
||||||
import fs = require('fs');
|
import fs = require('fs');
|
||||||
import cp = require('child_process');
|
|
||||||
import process = require('process');
|
import process = require('process');
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,34 +38,3 @@ export const getInstanceDir = (() => {
|
||||||
export function getTempFile(prefix: string): string {
|
export function getTempFile(prefix: string): string {
|
||||||
return path.join(getInstanceDir(), `${prefix}-${temp.makeRandomHexString(20)}.tmp`);
|
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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue