bootstrap.js 5.9 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6 7 8
//@ts-check
'use strict';

9 10 11 12 13 14 15
//#region global bootstrapping

// increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
Error.stackTraceLimit = 100;

// Workaround for Electron not installing a handler to ignore SIGPIPE
// (https://github.com/electron/electron/issues/13254)
16
// @ts-ignore
17 18 19 20 21 22
process.on('SIGPIPE', () => {
	console.error(new Error('Unexpected SIGPIPE'));
});

//#endregion

A
Alex Dima 已提交
23
//#region Add support for using node_modules.asar
B
Benjamin Pasero 已提交
24 25 26 27
/**
 * @param {string=} nodeModulesPath
 */
exports.enableASARSupport = function (nodeModulesPath) {
28

29
	// @ts-ignore
A
Alex Dima 已提交
30
	const Module = require('module');
31
	const path = require('path');
32

B
Benjamin Pasero 已提交
33 34 35 36 37
	let NODE_MODULES_PATH = nodeModulesPath;
	if (!NODE_MODULES_PATH) {
		NODE_MODULES_PATH = path.join(__dirname, '../node_modules');
	}

A
Alex Dima 已提交
38 39
	const NODE_MODULES_ASAR_PATH = NODE_MODULES_PATH + '.asar';

40
	// @ts-ignore
A
Alex Dima 已提交
41
	const originalResolveLookupPaths = Module._resolveLookupPaths;
42
	// @ts-ignore
A
Alex Dima 已提交
43 44
	Module._resolveLookupPaths = function (request, parent, newReturn) {
		const result = originalResolveLookupPaths(request, parent, newReturn);
A
Alex Dima 已提交
45

A
Alex Dima 已提交
46
		const paths = newReturn ? result : result[1];
A
Alex Dima 已提交
47 48 49 50 51 52 53 54 55
		for (let i = 0, len = paths.length; i < len; i++) {
			if (paths[i] === NODE_MODULES_PATH) {
				paths.splice(i, 0, NODE_MODULES_ASAR_PATH);
				break;
			}
		}

		return result;
	};
56
};
A
Alex Dima 已提交
57
//#endregion
58

59
//#region URI helpers
60 61 62 63
/**
 * @param {string} _path
 * @returns {string}
 */
64 65
exports.uriFromPath = function (_path) {
	const path = require('path');
66

67 68 69 70
	let pathName = path.resolve(_path).replace(/\\/g, '/');
	if (pathName.length > 0 && pathName.charAt(0) !== '/') {
		pathName = '/' + pathName;
	}
71

72 73 74
	return encodeURI('file://' + pathName).replace(/#/g, '%23');
};
//#endregion
E
Erich Gamma 已提交
75

76
//#region FS helpers
77 78
/**
 * @param {string} file
79
 * @returns {Promise<string>}
80
 */
81 82
exports.readFile = function (file) {
	const fs = require('fs');
E
Erich Gamma 已提交
83

84 85 86 87 88 89 90 91 92 93
	return new Promise(function (resolve, reject) {
		fs.readFile(file, 'utf8', function (err, data) {
			if (err) {
				reject(err);
				return;
			}
			resolve(data);
		});
	});
};
E
Erich Gamma 已提交
94

95 96 97
/**
 * @param {string} file
 * @param {string} content
98
 * @returns {Promise<void>}
99
 */
100 101
exports.writeFile = function (file, content) {
	const fs = require('fs');
E
Erich Gamma 已提交
102

103 104 105 106 107 108 109 110 111 112 113
	return new Promise(function (resolve, reject) {
		fs.writeFile(file, content, 'utf8', function (err) {
			if (err) {
				reject(err);
				return;
			}
			resolve();
		});
	});
};
//#endregion
E
Erich Gamma 已提交
114

115
//#region NLS helpers
116 117 118
/**
 * @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean }}
 */
119 120
exports.setupNLS = function () {
	const path = require('path');
E
Erich Gamma 已提交
121

122 123 124
	// Get the nls configuration into the process.env as early as possible.
	let nlsConfig = { availableLanguages: {} };
	if (process.env['VSCODE_NLS_CONFIG']) {
125
		try {
126 127 128
			nlsConfig = JSON.parse(process.env['VSCODE_NLS_CONFIG']);
		} catch (e) {
			// Ignore
129 130 131
		}
	}

132 133
	if (nlsConfig._resolvedLanguagePackCoreLocation) {
		const bundles = Object.create(null);
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		nlsConfig.loadBundle = function (bundle, language, cb) {
			let result = bundles[bundle];
			if (result) {
				cb(undefined, result);

				return;
			}

			const bundleFile = path.join(nlsConfig._resolvedLanguagePackCoreLocation, bundle.replace(/\//g, '!') + '.nls.json');
			exports.readFile(bundleFile).then(function (content) {
				let json = JSON.parse(content);
				bundles[bundle] = json;

				cb(undefined, json);
			}).catch((error) => {
				try {
					if (nlsConfig._corruptedFile) {
						exports.writeFile(nlsConfig._corruptedFile, 'corrupted').catch(function (error) { console.error(error); });
					}
				} finally {
					cb(error, undefined);
				}
			});
		};
E
Erich Gamma 已提交
159
	}
160

161 162 163
	return nlsConfig;
};
//#endregion
E
Erich Gamma 已提交
164

165
//#region Portable helpers
166 167 168
/**
 * @returns {{ portableDataPath: string, isPortable: boolean }}
 */
169
exports.configurePortable = function () {
170 171
	// @ts-ignore
	const product = require('../product.json');
172 173
	const path = require('path');
	const fs = require('fs');
174

175
	const appRoot = path.dirname(__dirname);
J
Joao Moreno 已提交
176

177 178 179 180
	function getApplicationPath() {
		if (process.env['VSCODE_DEV']) {
			return appRoot;
		}
181

182 183 184
		if (process.platform === 'darwin') {
			return path.dirname(path.dirname(path.dirname(appRoot)));
		}
185

186
		return path.dirname(path.dirname(appRoot));
J
Joao Moreno 已提交
187
	}
188 189 190 191 192 193 194 195

	function getPortableDataPath() {
		if (process.env['VSCODE_PORTABLE']) {
			return process.env['VSCODE_PORTABLE'];
		}

		if (process.platform === 'win32' || process.platform === 'linux') {
			return path.join(getApplicationPath(), 'data');
196
		}
197 198 199

		const portableDataName = product.portable || `${product.applicationName}-portable-data`;
		return path.join(path.dirname(getApplicationPath()), portableDataName);
200 201
	}

202
	const portableDataPath = getPortableDataPath();
J
Joao Moreno 已提交
203
	const isPortable = !('target' in product) && fs.existsSync(portableDataPath);
204 205 206 207 208 209 210 211 212 213 214 215
	const portableTempPath = path.join(portableDataPath, 'tmp');
	const isTempPortable = isPortable && fs.existsSync(portableTempPath);

	if (isPortable) {
		process.env['VSCODE_PORTABLE'] = portableDataPath;
	} else {
		delete process.env['VSCODE_PORTABLE'];
	}

	if (isTempPortable) {
		process.env[process.platform === 'win32' ? 'TEMP' : 'TMPDIR'] = portableTempPath;
	}
216

217 218 219 220 221
	return {
		portableDataPath,
		isPortable
	};
};
R
Ramya Rao 已提交
222 223 224 225 226 227 228 229
//#endregion

//#region ApplicationInsights
/**
 * Prevents appinsights from monkey patching modules.
 * This should be called before importing the applicationinsights module
 */
exports.avoidMonkeyPatchFromAppInsights = function () {
230
	// @ts-ignore
R
Ramya Rao 已提交
231 232 233
	process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] = true; // Skip monkey patching of 3rd party modules by appinsights
	global['diagnosticsSource'] = {}; // Prevents diagnostic channel (which patches "require") from initializing entirely
};
234
//#endregion