index.js 6.4 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 11
if (window.location.search.indexOf('performance-startup-profile') >= 0) {
	var profiler = require('v8-profiler');
12
	profiler.startProfiling('renderer', true);
13 14
}

J
Joao Moreno 已提交
15 16 17 18 19 20 21
/*global window,document,define*/

const path = require('path');
const electron = require('electron');
const remote = electron.remote;
const ipc = electron.ipcRenderer;

22

J
Johannes Rieken 已提交
23
process.lazyEnv = new Promise(function (resolve) {
24 25 26 27 28 29 30
	ipc.once('vscode:acceptShellEnv', function (event, shellEnv) {
		assign(process.env, shellEnv);
		resolve(process.env);
	});
	ipc.send('vscode:fetchShellEnv', remote.getCurrentWindow().id);
});

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

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

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

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

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

	return search.split(/[?&]/)
J
Joao Moreno 已提交
52 53 54 55
		.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 已提交
56 57 58 59 60 61 62 63 64 65 66 67
}

function createScript(src, onload) {
	const script = document.createElement('script');
	script.src = src;
	script.addEventListener('load', onload);

	const head = document.getElementsByTagName('head')[0];
	head.insertBefore(script, head.lastChild);
}

function uriFromPath(_path) {
J
Joao Moreno 已提交
68
	var pathName = path.resolve(_path).replace(/\\/g, '/');
J
Joao Moreno 已提交
69 70 71 72 73 74 75 76 77 78
	if (pathName.length > 0 && pathName.charAt(0) !== '/') {
		pathName = '/' + pathName;
	}

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

function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
B
Benjamin Pasero 已提交
79
	var listener;
J
Joao Moreno 已提交
80
	if (enableDeveloperTools) {
81
		const extractKey = function (e) {
J
Joao Moreno 已提交
82 83 84 85 86 87 88 89 90 91 92 93
			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 已提交
94
		listener = function (e) {
J
Joao Moreno 已提交
95 96
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
J
Joao Moreno 已提交
97
				remote.getCurrentWebContents().toggleDevTools();
J
Joao Moreno 已提交
98
			} else if (key === RELOAD_KB) {
99
				remote.getCurrentWindow().reload();
J
Joao Moreno 已提交
100
			}
B
Benjamin Pasero 已提交
101 102
		};
		window.addEventListener('keydown', listener);
J
Joao Moreno 已提交
103 104
	}

105
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools) });
B
Benjamin Pasero 已提交
106 107 108 109 110 111 112

	return function () {
		if (listener) {
			window.removeEventListener('keydown', listener);
			listener = void 0;
		}
	}
J
Joao Moreno 已提交
113 114 115 116 117 118 119 120 121 122 123
}

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

	// Get the nls configuration into the process.env as early as possible.
J
Joao Moreno 已提交
124
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
125 126 127 128 129 130 131 132
	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 已提交
133
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
134 135 136 137 138 139 140 141
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}

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

142
	const enableDeveloperTools = process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath;
B
Benjamin Pasero 已提交
143
	const unbind = registerListeners(enableDeveloperTools);
J
Joao Moreno 已提交
144 145

	// disable pinch zoom & apply zoom level early to avoid glitches
146
	const zoomLevel = configuration.zoomLevel;
J
Joao Moreno 已提交
147
	webFrame.setZoomLevelLimits(1, 1);
148 149
	if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
		webFrame.setZoomLevel(zoomLevel);
J
Joao Moreno 已提交
150 151 152 153
	}

	// Load the loader and start loading the workbench
	const rootUrl = uriFromPath(configuration.appRoot) + '/out';
B
Benjamin Pasero 已提交
154

J
Joao Moreno 已提交
155 156
	// In the bundled version the nls plugin is packaged with the loader so the NLS Plugins
	// loads as soon as the loader loads. To be able to have pseudo translation
157 158
	createScript(rootUrl + '/vs/loader.js', function () {
		define('fs', ['original-fs'], function (originalFS) { return originalFS; }); // replace the patched electron fs with the original node fs for all AMD code
B
Benjamin Pasero 已提交
159

160 161 162
		window.MonacoEnvironment = {};

		const nodeCachedDataErrors = window.MonacoEnvironment.nodeCachedDataErrors = [];
J
Joao Moreno 已提交
163 164 165
		require.config({
			baseUrl: rootUrl,
			'vs/nls': nlsConfig,
166 167
			recordStats: !!configuration.performance,
			nodeCachedDataDir: configuration.nodeCachedDataDir,
J
Johannes Rieken 已提交
168
			onNodeCachedDataError: function (err) { nodeCachedDataErrors.push(err) },
169
			nodeModules: [/*BUILD->INSERT_NODE_MODULES*/]
J
Joao Moreno 已提交
170
		});
B
Benjamin Pasero 已提交
171

J
Joao Moreno 已提交
172
		if (nlsConfig.pseudo) {
173
			require(['vs/nls'], function (nlsPlugin) {
J
Joao Moreno 已提交
174 175 176 177
				nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
			});
		}

178
		// Perf Counters
J
Joao Moreno 已提交
179
		const timers = window.MonacoEnvironment.timers = {
180 181
			isInitialStartup: !!configuration.isInitialStartup,
			hasAccessibilitySupport: !!configuration.accessibilitySupport,
B
Benjamin Pasero 已提交
182
			start: new Date(configuration.perfStartTime),
183
			appReady: new Date(configuration.perfAppReady),
B
Benjamin Pasero 已提交
184 185
			windowLoad: new Date(configuration.perfWindowLoadTime),
			beforeLoadWorkbenchMain: new Date()
J
Joao Moreno 已提交
186 187 188
		};

		require([
189 190 191
			'vs/workbench/electron-browser/workbench.main',
			'vs/nls!vs/workbench/electron-browser/workbench.main',
			'vs/css!vs/workbench/electron-browser/workbench.main'
J
Joao Moreno 已提交
192
		], function () {
B
Benjamin Pasero 已提交
193
			timers.afterLoadWorkbenchMain = new Date();
J
Joao Moreno 已提交
194

195 196 197 198 199 200 201 202 203 204 205
			process.lazyEnv.then(function () {

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

J
Joao Moreno 已提交
206 207 208 209
		});
	});
}

210
main();