bootstrap.js 6.0 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.
 *--------------------------------------------------------------------------------------------*/

A
Alex Dima 已提交
6 7 8 9 10 11 12 13
//#region Add support for using node_modules.asar
(function () {
	const path = require('path');
	const Module = require('module');
	const NODE_MODULES_PATH = path.join(__dirname, '../node_modules');
	const NODE_MODULES_ASAR_PATH = NODE_MODULES_PATH + '.asar';

	const originalResolveLookupPaths = Module._resolveLookupPaths;
A
Alex Dima 已提交
14 15
	Module._resolveLookupPaths = function (request, parent, newReturn) {
		const result = originalResolveLookupPaths(request, parent, newReturn);
A
Alex Dima 已提交
16

A
Alex Dima 已提交
17
		const paths = newReturn ? result : result[1];
A
Alex Dima 已提交
18 19 20 21 22 23 24 25 26 27 28
		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;
	};
})();
//#endregion
29

E
Erich Gamma 已提交
30 31 32 33 34 35
// Will be defined if we got forked from another node process
// In that case we override console.log/warn/error to be able
// to send loading issues to the main side for logging.
if (!!process.send && process.env.PIPE_LOGGING === 'true') {
	var MAX_LENGTH = 100000;

36 37
	// Prevent circular stringify and convert arguments to real array
	function safeToArray(args) {
E
Erich Gamma 已提交
38 39
		var seen = [];
		var res;
40
		var argsArray = [];
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

		// Massage some arguments with special treatment
		if (args.length) {
			for (var i = 0; i < args.length; i++) {

				// Any argument of type 'undefined' needs to be specially treated because
				// JSON.stringify will simply ignore those. We replace them with the string
				// 'undefined' which is not 100% right, but good enough to be logged to console
				if (typeof args[i] === 'undefined') {
					args[i] = 'undefined';
				}

				// Any argument that is an Error will be changed to be just the error stack/message
				// itself because currently cannot serialize the error over entirely.
				else if (args[i] instanceof Error) {
					var errorObj = args[i];
					if (errorObj.stack) {
						args[i] = errorObj.stack;
					} else {
						args[i] = errorObj.toString();
					}
				}
63 64

				argsArray.push(args[i]);
65 66 67
			}
		}

68 69 70 71 72 73 74
		// Add the stack trace as payload if we are told so. We remove the message and the 2 top frames
		// to start the stacktrace where the console message was being written
		if (process.env.VSCODE_LOG_STACK === 'true') {
			const stack = new Error().stack;
			argsArray.push({ __$stack: stack.split('\n').slice(3).join('\n') });
		}

E
Erich Gamma 已提交
75
		try {
76
			res = JSON.stringify(argsArray, function (key, value) {
E
Erich Gamma 已提交
77 78

				// Objects get special treatment to prevent circles
79
				if (isObject(value) || Array.isArray(value)) {
E
Erich Gamma 已提交
80
					if (seen.indexOf(value) !== -1) {
81
						return '[Circular]';
E
Erich Gamma 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
					}

					seen.push(value);
				}

				return value;
			});
		} catch (error) {
			return 'Output omitted for an object that cannot be inspected (' + error.toString() + ')';
		}

		if (res && res.length > MAX_LENGTH) {
			return 'Output omitted for a large object that exceeds the limits';
		}

		return res;
	}

100 101 102 103 104 105 106 107
	function safeSend(arg) {
		try {
			process.send(arg);
		} catch (error) {
			// Can happen if the parent channel is closed meanwhile
		}
	}

108 109 110 111 112 113 114 115
	function isObject(obj) {
		return typeof obj === 'object'
			&& obj !== null
			&& !Array.isArray(obj)
			&& !(obj instanceof RegExp)
			&& !(obj instanceof Date);
	}

E
Erich Gamma 已提交
116 117
	// Pass console logging to the outside so that we have it in the main side if told so
	if (process.env.VERBOSE_LOGGING === 'true') {
118 119 120
		console.log = function () { safeSend({ type: '__$console', severity: 'log', arguments: safeToArray(arguments) }); };
		console.info = function () { safeSend({ type: '__$console', severity: 'log', arguments: safeToArray(arguments) }); };
		console.warn = function () { safeSend({ type: '__$console', severity: 'warn', arguments: safeToArray(arguments) }); };
E
Erich Gamma 已提交
121 122 123
	} else {
		console.log = function () { /* ignore */ };
		console.warn = function () { /* ignore */ };
124
		console.info = function () { /* ignore */ };
E
Erich Gamma 已提交
125
	}
126

127
	console.error = function () { safeSend({ type: '__$console', severity: 'error', arguments: safeToArray(arguments) }); };
E
Erich Gamma 已提交
128 129
}

130 131 132 133 134 135 136 137
if (!process.env['VSCODE_ALLOW_IO']) {
	// Let stdout, stderr and stdin be no-op streams. This prevents an issue where we would get an EBADF
	// error when we are inside a forked process and this process tries to access those channels.
	var stream = require('stream');
	var writable = new stream.Writable({
		write: function () { /* No OP */ }
	});

J
Joao Moreno 已提交
138 139 140
	process.__defineGetter__('stdout', function () { return writable; });
	process.__defineGetter__('stderr', function () { return writable; });
	process.__defineGetter__('stdin', function () { return writable; });
141
}
J
Joao Moreno 已提交
142

J
Johannes Rieken 已提交
143 144 145 146 147 148 149 150 151
if (!process.env['VSCODE_HANDLES_UNCAUGHT_ERRORS']) {
	// Handle uncaught exceptions
	process.on('uncaughtException', function (err) {
		console.error('Uncaught Exception: ', err.toString());
		if (err.stack) {
			console.error(err.stack);
		}
	});
}
E
Erich Gamma 已提交
152

J
Joao Moreno 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
// Kill oneself if one's parent dies. Much drama.
if (process.env['VSCODE_PARENT_PID']) {
	const parentPid = Number(process.env['VSCODE_PARENT_PID']);

	if (typeof parentPid === 'number' && !isNaN(parentPid)) {
		setInterval(function () {
			try {
				process.kill(parentPid, 0); // throws an exception if the main process doesn't exist anymore.
			} catch (e) {
				process.exit();
			}
		}, 5000);
	}
}

168 169 170 171 172 173 174 175 176 177 178 179
const crashReporterOptionsRaw = process.env['CRASH_REPORTER_START_OPTIONS'];
if (typeof crashReporterOptionsRaw === 'string') {
	try {
		const crashReporterOptions = JSON.parse(crashReporterOptionsRaw);
		if (crashReporterOptions) {
			process.crashReporter.start(crashReporterOptions);
		}
	} catch (error) {
		console.error(error);
	}
}

180 181 182 183 184 185
// Workaround for Electron not installing a handler to ignore SIGPIPE
// (https://github.com/electron/electron/issues/13254)
process.on('SIGPIPE', () => {
	console.error(new Error('Unexpected SIGPIPE'));
});

J
Johannes Rieken 已提交
186
require('./bootstrap-amd').bootstrap(process.env['AMD_ENTRYPOINT']);