env.ts 1.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*---------------------------------------------------------------------------------------------
 *  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) => {
22
		let child = cp.spawn(process.env.SHELL, ['-ilc', 'env'], {
E
Erich Gamma 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
			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);

40
			buffer.split('\n').forEach(line => {
E
Erich Gamma 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
				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);
		});
	});
}