index.js 6.2 KB
Newer Older
J
Joao Moreno 已提交
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.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6 7
// Warning: Do not use the `let` declarator in this file, it breaks our minification

J
Joao Moreno 已提交
8 9
'use strict';

10
/*global window,document,define*/
J
Joao Moreno 已提交
11

12 13 14
const perf = require('../../../base/common/performance');
perf.mark('renderer/started');

J
Joao Moreno 已提交
15 16 17 18 19
const path = require('path');
const electron = require('electron');
const remote = electron.remote;
const ipc = electron.ipcRenderer;

J
Johannes Rieken 已提交
20
process.lazyEnv = new Promise(function (resolve) {
J
Joao Moreno 已提交
21 22
	const handle = setTimeout(function () {
		resolve();
J
Joao Moreno 已提交
23
		console.warn('renderer did not receive lazyEnv in time');
J
Joao Moreno 已提交
24
	}, 10000);
25
	ipc.once('vscode:acceptShellEnv', function (event, shellEnv) {
J
Joao Moreno 已提交
26
		clearTimeout(handle);
27 28 29 30 31 32
		assign(process.env, shellEnv);
		resolve(process.env);
	});
	ipc.send('vscode:fetchShellEnv', remote.getCurrentWindow().id);
});

J
Joao Moreno 已提交
33 34
function onError(error, enableDeveloperTools) {
	if (enableDeveloperTools) {
J
Joao Moreno 已提交
35
		remote.getCurrentWebContents().openDevTools();
J
Joao Moreno 已提交
36 37 38 39 40 41 42 43 44 45 46
	}

	console.error('[uncaught exception]: ' + error);

	if (error.stack) {
		console.error(error.stack);
	}
}

function assign(destination, source) {
	return Object.keys(source)
J
Joao Moreno 已提交
47
		.reduce(function (r, key) { r[key] = source[key]; return r; }, destination);
J
Joao Moreno 已提交
48 49 50 51 52 53
}

function parseURLQueryArgs() {
	const search = window.location.search || '';

	return search.split(/[?&]/)
J
Joao Moreno 已提交
54 55 56 57
		.filter(function (param) { return !!param; })
		.map(function (param) { return param.split('='); })
		.filter(function (param) { return param.length === 2; })
		.reduce(function (r, param) { r[param[0]] = decodeURIComponent(param[1]); return r; }, {});
J
Joao Moreno 已提交
58 59 60
}

function uriFromPath(_path) {
J
Joao Moreno 已提交
61
	var pathName = path.resolve(_path).replace(/\\/g, '/');
J
Joao Moreno 已提交
62 63 64 65 66 67 68 69 70 71
	if (pathName.length > 0 && pathName.charAt(0) !== '/') {
		pathName = '/' + pathName;
	}

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

function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
B
Benjamin Pasero 已提交
72
	var listener;
J
Joao Moreno 已提交
73
	if (enableDeveloperTools) {
74
		const extractKey = function (e) {
J
Joao Moreno 已提交
75 76 77 78 79 80 81 82 83 84 85 86
			return [
				e.ctrlKey ? 'ctrl-' : '',
				e.metaKey ? 'meta-' : '',
				e.altKey ? 'alt-' : '',
				e.shiftKey ? 'shift-' : '',
				e.keyCode
			].join('');
		};

		const TOGGLE_DEV_TOOLS_KB = (process.platform === 'darwin' ? 'meta-alt-73' : 'ctrl-shift-73'); // mac: Cmd-Alt-I, rest: Ctrl-Shift-I
		const RELOAD_KB = (process.platform === 'darwin' ? 'meta-82' : 'ctrl-82'); // mac: Cmd-R, rest: Ctrl-R

B
Benjamin Pasero 已提交
87
		listener = function (e) {
J
Joao Moreno 已提交
88 89
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
J
Joao Moreno 已提交
90
				remote.getCurrentWebContents().toggleDevTools();
J
Joao Moreno 已提交
91
			} else if (key === RELOAD_KB) {
92
				remote.getCurrentWindow().reload();
J
Joao Moreno 已提交
93
			}
B
Benjamin Pasero 已提交
94 95
		};
		window.addEventListener('keydown', listener);
J
Joao Moreno 已提交
96 97
	}

J
Joao Moreno 已提交
98
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools); });
B
Benjamin Pasero 已提交
99 100 101 102 103 104

	return function () {
		if (listener) {
			window.removeEventListener('keydown', listener);
			listener = void 0;
		}
J
Joao Moreno 已提交
105
	};
J
Joao Moreno 已提交
106 107 108 109 110 111 112 113 114
}

function main() {
	const webFrame = require('electron').webFrame;
	const args = parseURLQueryArgs();
	const configuration = JSON.parse(args['config'] || '{}') || {};

	// Correctly inherit the parent's environment
	assign(process.env, configuration.userEnv);
115
	perf.importEntries(configuration.perfEntries);
J
Joao Moreno 已提交
116 117

	// Get the nls configuration into the process.env as early as possible.
J
Joao Moreno 已提交
118
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
119 120 121 122 123 124 125 126
	const config = process.env['VSCODE_NLS_CONFIG'];
	if (config) {
		process.env['VSCODE_NLS_CONFIG'] = config;
		try {
			nlsConfig = JSON.parse(config);
		} catch (e) { /*noop*/ }
	}

J
Joao Moreno 已提交
127
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
128 129 130 131 132 133 134 135
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}

	window.document.documentElement.setAttribute('lang', locale);

136
	const enableDeveloperTools = (process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath) && !configuration.extensionTestsPath;
B
Benjamin Pasero 已提交
137
	const unbind = registerListeners(enableDeveloperTools);
J
Joao Moreno 已提交
138 139

	// disable pinch zoom & apply zoom level early to avoid glitches
140
	const zoomLevel = configuration.zoomLevel;
141
	webFrame.setVisualZoomLevelLimits(1, 1);
142 143
	if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
		webFrame.setZoomLevel(zoomLevel);
J
Joao Moreno 已提交
144 145 146
	}

	// Load the loader and start loading the workbench
147 148 149
	const loaderFilename = configuration.appRoot + '/out/vs/loader.js';
	const loaderSource = require('fs').readFileSync(loaderFilename);
	require('vm').runInThisContext(loaderSource, { filename: loaderFilename });
J
Joao 已提交
150

151
	window.nodeRequire = require.__$__nodeRequire;
B
Benjamin Pasero 已提交
152

153
	define('fs', ['original-fs'], function (originalFS) { return originalFS; }); // replace the patched electron fs with the original node fs for all AMD code
154

155
	window.MonacoEnvironment = {};
J
Joao Moreno 已提交
156

157 158 159 160 161 162 163 164 165
	const onNodeCachedData = window.MonacoEnvironment.onNodeCachedData = [];
	require.config({
		baseUrl: uriFromPath(configuration.appRoot) + '/out',
		'vs/nls': nlsConfig,
		recordStats: !!configuration.performance,
		nodeCachedDataDir: configuration.nodeCachedDataDir,
		onNodeCachedData: function () { onNodeCachedData.push(arguments); },
		nodeModules: [/*BUILD->INSERT_NODE_MODULES*/]
	});
J
Joao Moreno 已提交
166

167 168 169
	if (nlsConfig.pseudo) {
		require(['vs/nls'], function (nlsPlugin) {
			nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
J
Joao Moreno 已提交
170
		});
171 172
	}

173
	// Perf Counters
174
	window.MonacoEnvironment.timers = {
175 176 177
		isInitialStartup: !!configuration.isInitialStartup,
		hasAccessibilitySupport: !!configuration.accessibilitySupport,
		start: configuration.perfStartTime,
178
		windowLoad: configuration.perfWindowLoadTime
179 180
	};

J
Johannes Rieken 已提交
181
	perf.mark('willLoadWorkbenchMain');
182 183 184 185 186
	require([
		'vs/workbench/workbench.main',
		'vs/nls!vs/workbench/workbench.main',
		'vs/css!vs/workbench/workbench.main'
	], function () {
J
Johannes Rieken 已提交
187
		perf.mark('didLoadWorkbenchMain');
188 189 190 191 192 193 194 195 196 197 198 199

		process.lazyEnv.then(function () {
			perf.mark('main/startup');
			require('vs/workbench/electron-browser/main')
				.startup(configuration)
				.done(function () {
					unbind(); // since the workbench is running, unbind our developer related listeners and let the workbench handle them
				}, function (error) {
					onError(error, enableDeveloperTools);
				});
		});
	});
200

J
Joao Moreno 已提交
201 202
}

203
main();