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

// 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;

	// Prevent circular stringify
13
	function safeStringify(args) {
E
Erich Gamma 已提交
14 15
		var seen = [];
		var res;
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

		// 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();
					}
				}
			}
		}

E
Erich Gamma 已提交
41
		try {
42
			res = JSON.stringify(args, function (key, value) {
E
Erich Gamma 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

				// Objects get special treatment to prevent circles
				if (value && Object.prototype.toString.call(value) === '[object Object]') {
					if (seen.indexOf(value) !== -1) {
						return Object.create(null); // prevent circular references!
					}

					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;
	}

66 67 68 69 70 71 72 73
	function safeSend(arg) {
		try {
			process.send(arg);
		} catch (error) {
			// Can happen if the parent channel is closed meanwhile
		}
	}

E
Erich Gamma 已提交
74 75
	// Pass console logging to the outside so that we have it in the main side if told so
	if (process.env.VERBOSE_LOGGING === 'true') {
76 77
		console.log = function () { safeSend({ type: '__$console', severity: 'log', arguments: safeStringify(arguments) }); };
		console.warn = function () { safeSend({ type: '__$console', severity: 'warn', arguments: safeStringify(arguments) }); };
E
Erich Gamma 已提交
78 79 80 81
	} else {
		console.log = function () { /* ignore */ };
		console.warn = function () { /* ignore */ };
	}
82

83
	console.error = function () { safeSend({ type: '__$console', severity: 'error', arguments: safeStringify(arguments) }); };
E
Erich Gamma 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

	// 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 (chunk, encoding, next) { /* No OP */ }
	});
	process.__defineGetter__('stdout', function() { return writable; });
	process.__defineGetter__('stderr', function() { return writable; });
	process.__defineGetter__('stdin', function() { return writable; });
}

// Handle uncaught exceptions
process.on('uncaughtException', function (err) {
	console.error('Uncaught Exception: ', err.toString());
	if (err.stack) {
		console.error(err.stack);
	}
});

var path = require('path');
var loader = require('./vs/loader');

// TODO: Duplicated in:
// * src\bootstrap.js
// * src\vs\workbench\electron-main\bootstrap.js
// * src\vs\platform\plugins\common\nativePluginService.ts
function uriFromPath(_path) {
	var pathName = path.resolve(_path).replace(/\\/g, '/');

	if (pathName.length > 0 && pathName.charAt(0) !== '/') {
		pathName = '/' + pathName;
	}

	return encodeURI('file://' + pathName);
}

loader.config({
J
Joao Moreno 已提交
122
	baseUrl: uriFromPath(path.join(__dirname)),
E
Erich Gamma 已提交
123 124 125 126 127 128 129 130 131
	catchError: true,
	nodeRequire: require,
	nodeMain: __filename
});

var entrypoint = process.env.AMD_ENTRYPOINT;
if (entrypoint) {
	loader([entrypoint], function () { }, function (err) { console.error(err); });
}