forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedProcess.ts
More file actions
77 lines (62 loc) · 2.04 KB
/
sharedProcess.ts
File metadata and controls
77 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as cp from 'child_process';
import URI from 'vs/base/common/uri';
import { IDisposable } from 'vs/base/common/lifecycle';
import { assign } from 'vs/base/common/objects';
import { ParsedArgs } from 'vs/platform/environment/common/environment';
import { TPromise } from 'vs/base/common/winjs.base';
export interface ISharedProcessInitData {
args: ParsedArgs;
}
export interface ISharedProcessOptions {
allowOutput?: boolean;
debugPort?: number;
}
const boostrapPath = URI.parse(require.toUrl('bootstrap')).fsPath;
function _spawnSharedProcess(initData: ISharedProcessInitData, options: ISharedProcessOptions): cp.ChildProcess {
const execArgv: string[] = [];
const env = assign({}, process.env, {
AMD_ENTRYPOINT: 'vs/code/node/sharedProcessMain',
ELECTRON_NO_ASAR: '1'
});
if (options.allowOutput) {
env['VSCODE_ALLOW_IO'] = 'true';
}
if (options.debugPort) {
execArgv.push(`--debug=${options.debugPort}`);
}
const result = cp.fork(boostrapPath, ['--type=SharedProcess'], { env, execArgv });
return result;
}
export function spawnSharedProcess(initData: ISharedProcessInitData, options: ISharedProcessOptions = {}): TPromise<IDisposable> {
let spawnCount = 0;
let child: cp.ChildProcess;
let promise: TPromise<IDisposable>;
const spawn = () => {
if (++spawnCount > 10) {
return;
}
child = _spawnSharedProcess(initData, options);
promise = new TPromise<IDisposable>((c, e) => {
// handshake
child.once('message', () => {
child.send(initData);
c({
dispose: () => {
if (child) {
child.removeListener('exit', spawn);
child.kill();
child = null;
}
}
});
});
});
child.on('exit', spawn);
};
spawn();
return promise;
}