bootstrap.js 7.3 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
// Simple module style to support node.js and browser environments
(function (globalThis, factory) {
11

12 13 14 15
	// Node.js
	if (typeof exports === 'object') {
		module.exports = factory();
	}
16

17 18
	// Browser
	else {
19
		globalThis.MonacoBootstrap = factory();
20 21
	}
}(this, function () {
22 23 24
	const Module = typeof require === 'function' ? require('module') : undefined;
	const path = typeof require === 'function' ? require('path') : undefined;
	const fs = typeof require === 'function' ? require('fs') : undefined;
25

26
	//#region global bootstrapping
B
Benjamin Pasero 已提交
27

28 29
	// increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
	Error.stackTraceLimit = 100;
A
Alex Dima 已提交
30

31 32
	// Workaround for Electron not installing a handler to ignore SIGPIPE
	// (https://github.com/electron/electron/issues/13254)
33 34 35 36 37
	if (typeof process !== 'undefined') {
		process.on('SIGPIPE', () => {
			console.error(new Error('Unexpected SIGPIPE'));
		});
	}
38

39
	//#endregion
A
Alex Dima 已提交
40

41

42 43 44
	//#region Add support for using node_modules.asar

	/**
45
	 * @param {string | undefined} appRoot
46
	 */
47
	function enableASARSupport(appRoot) {
48 49 50 51 52
		if (!path || !Module) {
			console.warn('enableASARSupport() is only available in node.js environments');
			return;
		}

53
		let NODE_MODULES_PATH = appRoot ? path.join(appRoot, 'node_modules') : undefined;
54 55
		if (!NODE_MODULES_PATH) {
			NODE_MODULES_PATH = path.join(__dirname, '../node_modules');
A
Alex Dima 已提交
56 57 58 59 60
		} else {
			// use the drive letter casing of __dirname
			if (process.platform === 'win32') {
				NODE_MODULES_PATH = __dirname.substr(0, 1) + NODE_MODULES_PATH.substr(1);
			}
61
		}
62

63
		const NODE_MODULES_ASAR_PATH = `${NODE_MODULES_PATH}.asar`;
64

65 66
		// @ts-ignore
		const originalResolveLookupPaths = Module._resolveLookupPaths;
67

68 69 70 71 72 73 74 75 76 77 78
		// @ts-ignore
		Module._resolveLookupPaths = function (request, parent) {
			const paths = originalResolveLookupPaths(request, parent);
			if (Array.isArray(paths)) {
				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;
					}
				}
			}
79

80 81
			return paths;
		};
82 83
	}

84
	//#endregion
85

E
Erich Gamma 已提交
86

87
	//#region URI helpers
88

89
	/**
90
	 * @param {string} path
91
	 * @param {{ isWindows?: boolean, scheme?: string, fallbackAuthority?: string }} config
92 93
	 * @returns {string}
	 */
94 95 96 97
	function fileUriFromPath(path, config) {

		// Since we are building a URI, we normalize any backlsash
		// to slashes and we ensure that the path begins with a '/'.
98
		let pathName = path.replace(/\\/g, '/');
99 100 101
		if (pathName.length > 0 && pathName.charAt(0) !== '/') {
			pathName = `/${pathName}`;
		}
E
Erich Gamma 已提交
102

103 104
		/** @type {string} */
		let uri;
105 106 107 108 109 110 111 112 113 114 115

		// Windows: in order to support UNC paths (which start with '//')
		// that have their own authority, we do not use the provided authority
		// but rather preserve it.
		if (config.isWindows && pathName.startsWith('//')) {
			uri = encodeURI(`${config.scheme || 'file'}:${pathName}`);
		}

		// Otherwise we optionally add the provided authority if specified
		else {
			uri = encodeURI(`${config.scheme || 'file'}://${config.fallbackAuthority || ''}${pathName}`);
116
		}
117 118

		return uri.replace(/#/g, '%23');
119 120
	}

121 122
	//#endregion

123

124
	//#region NLS helpers
125

126
	/**
127
	 * @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean } | undefined}
128 129
	 */
	function setupNLS() {
130 131 132 133
		if (!path || !fs) {
			console.warn('setupNLS() is only available in node.js environments');
			return;
		}
134 135 136 137 138 139 140 141

		// Get the nls configuration into the process.env as early as possible.
		let nlsConfig = { availableLanguages: {} };
		if (process.env['VSCODE_NLS_CONFIG']) {
			try {
				nlsConfig = JSON.parse(process.env['VSCODE_NLS_CONFIG']);
			} catch (e) {
				// Ignore
142
			}
143
		}
144

145 146
		if (nlsConfig._resolvedLanguagePackCoreLocation) {
			const bundles = Object.create(null);
147

148 149 150 151 152 153
			nlsConfig.loadBundle = function (bundle, language, cb) {
				const result = bundles[bundle];
				if (result) {
					cb(undefined, result);

					return;
154
				}
155

156
				const bundleFile = path.join(nlsConfig._resolvedLanguagePackCoreLocation, `${bundle.replace(/\//g, '!')}.nls.json`);
157
				fs.promises.readFile(bundleFile, 'utf8').then(function (content) {
158 159 160 161 162 163 164
					const json = JSON.parse(content);
					bundles[bundle] = json;

					cb(undefined, json);
				}).catch((error) => {
					try {
						if (nlsConfig._corruptedFile) {
165
							fs.promises.writeFile(nlsConfig._corruptedFile, 'corrupted', 'utf8').catch(function (error) { console.error(error); });
166 167 168 169 170 171 172
						}
					} finally {
						cb(error, undefined);
					}
				});
			};
		}
173

174 175
		return nlsConfig;
	}
176

177
	//#endregion
178

E
Erich Gamma 已提交
179

180
	//#region Portable helpers
181

182 183
	/**
	 * @param {{ portable: string; applicationName: string; }} product
184
	 * @returns {{ portableDataPath: string; isPortable: boolean; } | undefined}
185 186
	 */
	function configurePortable(product) {
187 188 189 190 191
		if (!path || !fs) {
			console.warn('configurePortable() is only available in node.js environments');
			return;
		}

192 193 194 195 196 197
		const appRoot = path.dirname(__dirname);

		function getApplicationPath() {
			if (process.env['VSCODE_DEV']) {
				return appRoot;
			}
198

199 200 201
			if (process.platform === 'darwin') {
				return path.dirname(path.dirname(path.dirname(appRoot)));
			}
J
Joao Moreno 已提交
202

203
			return path.dirname(path.dirname(appRoot));
204
		}
205

206 207 208 209 210 211 212 213 214 215 216 217
		function getPortableDataPath() {
			if (process.env['VSCODE_PORTABLE']) {
				return process.env['VSCODE_PORTABLE'];
			}

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

			// @ts-ignore
			const portableDataName = product.portable || `${product.applicationName}-portable-data`;
			return path.join(path.dirname(getApplicationPath()), portableDataName);
218
		}
219

220 221 222 223
		const portableDataPath = getPortableDataPath();
		const isPortable = !('target' in product) && fs.existsSync(portableDataPath);
		const portableTempPath = path.join(portableDataPath, 'tmp');
		const isTempPortable = isPortable && fs.existsSync(portableTempPath);
224

225 226 227 228
		if (isPortable) {
			process.env['VSCODE_PORTABLE'] = portableDataPath;
		} else {
			delete process.env['VSCODE_PORTABLE'];
229 230
		}

231 232 233 234 235 236 237
		if (isTempPortable) {
			if (process.platform === 'win32') {
				process.env['TMP'] = portableTempPath;
				process.env['TEMP'] = portableTempPath;
			} else {
				process.env['TMPDIR'] = portableTempPath;
			}
238
		}
239

240 241 242 243
		return {
			portableDataPath,
			isPortable
		};
244 245
	}

246
	//#endregion
247

248

249
	//#region ApplicationInsights
250

251 252 253
	// Prevents appinsights from monkey patching modules.
	// This should be called before importing the applicationinsights module
	function avoidMonkeyPatchFromAppInsights() {
254 255 256 257 258
		if (typeof process === 'undefined') {
			console.warn('avoidMonkeyPatchFromAppInsights() is only available in node.js environments');
			return;
		}

259 260 261 262
		// @ts-ignore
		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
	}
R
Ramya Rao 已提交
263

264
	//#endregion
265 266


267 268 269 270 271
	return {
		enableASARSupport,
		avoidMonkeyPatchFromAppInsights,
		configurePortable,
		setupNLS,
272
		fileUriFromPath
273 274
	};
}));