-
Notifications
You must be signed in to change notification settings - Fork 29.7k
/
env.ts
57 lines (44 loc) · 1.42 KB
/
env.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import platform = require('vs/base/common/platform');
import { TPromise } from 'vs/base/common/winjs.base';
import cp = require('child_process');
export interface IEnv {
[key: string]: string;
}
export function getUserEnvironment(): TPromise<IEnv> {
if (platform.isWindows) {
return TPromise.as({});
}
return new TPromise((c, e) => {
let child = cp.spawn(process.env.SHELL, ['-ilc', 'env'], {
detached: true,
stdio: ['ignore', 'pipe', process.stderr],
});
child.stdout.setEncoding('utf8');
child.on('error', () => c({}));
let buffer = '';
child.stdout.on('data', (d: string) => { buffer += d; });
child.on('close', (code: number, signal: any) => {
if (code !== 0) {
return c({});
}
let result: IEnv = Object.create(null);
buffer.split('\n').forEach(line => {
let pos = line.indexOf('=');
if (pos > 0) {
let key = line.substring(0, pos);
let value = line.substring(pos + 1);
if (!key || typeof result[key] === 'string') {
return;
}
result[key] = value;
}
});
c(result);
});
});
}