index.js 10.6 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
const path = require('path');
D
Dirk Baeumer 已提交
16
const fs = require('fs');
J
Joao Moreno 已提交
17 18 19 20
const electron = require('electron');
const remote = electron.remote;
const ipc = electron.ipcRenderer;

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

B
Benjamin Pasero 已提交
34 35
Error.stackTraceLimit = 100; // increase number of stack frames (from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)

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

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

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

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

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

	return search.split(/[?&]/)
J
Joao Moreno 已提交
57 58 59 60
		.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 已提交
61 62 63
}

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

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

D
Dirk Baeumer 已提交
72
function readFile(file) {
73 74
	return new Promise(function (resolve, reject) {
		fs.readFile(file, 'utf8', function (err, data) {
D
Dirk Baeumer 已提交
75 76 77 78 79 80 81 82 83
			if (err) {
				reject(err);
				return;
			}
			resolve(data);
		});
	});
}

84
function showPartsSplash(configuration) {
J
Johannes Rieken 已提交
85
	perf.mark('willShowPartsSplash');
86

87 88 89 90 91
	// TODO@Ben remove me after a while
	perf.mark('willAccessLocalStorage');
	let storage = window.localStorage;
	perf.mark('didAccessLocalStorage');

J
Johannes Rieken 已提交
92 93 94 95 96 97
	let data;
	try {
		let raw = storage.getItem('storage://global/parts-splash-data');
		data = JSON.parse(raw);
	} catch (e) {
		// ignore
98
	}
J
Johannes Rieken 已提交
99 100 101

	if (data) {
		const splash = document.createElement('div');
102
		splash.id = data.id;
J
Johannes Rieken 已提交
103 104 105
		const { layoutInfo, colorInfo } = data;
		if (configuration.folderUri || configuration.workspace) {
			// folder or workspace -> status bar color, sidebar
106
			splash.innerHTML = `
J
Johannes Rieken 已提交
107 108 109 110
			<div style="position: absolute; width: 100%; left: 0; top: 0; height: ${layoutInfo.titleBarHeight}px; background-color: ${colorInfo.titleBarBackground};"></div>
			<div style="position: absolute; height: calc(100% - ${layoutInfo.titleBarHeight}px); top: ${layoutInfo.titleBarHeight}px; ${layoutInfo.sideBarSide}: 0; width: ${layoutInfo.activityBarWidth}px; background-color: ${colorInfo.activityBarBackground};"></div>
			<div style="position: absolute; height: calc(100% - ${layoutInfo.titleBarHeight}px); top: ${layoutInfo.titleBarHeight}px; ${layoutInfo.sideBarSide}: ${layoutInfo.activityBarWidth}px; width: ${layoutInfo.sideBarWidth}px; background-color: ${colorInfo.sideBarBackground};"></div>
			<div style="position: absolute; width: 100%; bottom: 0; left: 0; height: ${layoutInfo.statusBarHeight}px; background-color: ${colorInfo.statusBarBackground};"></div>
111
			`;
J
Johannes Rieken 已提交
112 113
		} else {
			// empty -> speical status bar color, no sidebar
114
			splash.innerHTML = `
J
Johannes Rieken 已提交
115 116 117
			<div style="position: absolute; width: 100%; left: 0; top: 0; height: ${layoutInfo.titleBarHeight}px; background-color: ${colorInfo.titleBarBackground};"></div>
			<div style="position: absolute; height: calc(100% - ${layoutInfo.titleBarHeight}px); top: ${layoutInfo.titleBarHeight}px; ${layoutInfo.sideBarSide}: 0; width: ${layoutInfo.activityBarWidth}px; background-color: ${colorInfo.activityBarBackground};"></div>
			<div style="position: absolute; width: 100%; bottom: 0; left: 0; height: ${layoutInfo.statusBarHeight}px; background-color: ${colorInfo.statusBarNoFolderBackground};"></div>
118
			`;
J
Johannes Rieken 已提交
119 120
		}
		document.body.appendChild(splash);
121
	}
J
Johannes Rieken 已提交
122
	perf.mark('didShowPartsSplash');
123 124
}

125 126
const writeFile = (file, content) => new Promise((c, e) => fs.writeFile(file, content, 'utf8', err => err ? e(err) : c()));

J
Joao Moreno 已提交
127 128 129
function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
B
Benjamin Pasero 已提交
130
	var listener;
J
Joao Moreno 已提交
131
	if (enableDeveloperTools) {
132
		const extractKey = function (e) {
J
Joao Moreno 已提交
133 134 135 136 137 138 139 140 141 142 143 144
			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 已提交
145
		listener = function (e) {
J
Joao Moreno 已提交
146 147
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
J
Joao Moreno 已提交
148
				remote.getCurrentWebContents().toggleDevTools();
J
Joao Moreno 已提交
149
			} else if (key === RELOAD_KB) {
150
				remote.getCurrentWindow().reload();
J
Joao Moreno 已提交
151
			}
B
Benjamin Pasero 已提交
152 153
		};
		window.addEventListener('keydown', listener);
J
Joao Moreno 已提交
154 155
	}

J
Joao Moreno 已提交
156
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools); });
B
Benjamin Pasero 已提交
157 158 159 160 161 162

	return function () {
		if (listener) {
			window.removeEventListener('keydown', listener);
			listener = void 0;
		}
J
Joao Moreno 已提交
163
	};
J
Joao Moreno 已提交
164 165 166 167 168 169 170
}

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

A
Alex Dima 已提交
171 172 173 174 175 176 177 178 179 180 181 182
	//#region Add support for using node_modules.asar
	(function () {
		const path = require('path');
		const Module = require('module');
		let NODE_MODULES_PATH = path.join(configuration.appRoot, 'node_modules');
		if (/[a-z]\:/.test(NODE_MODULES_PATH)) {
			// Make drive letter uppercase
			NODE_MODULES_PATH = NODE_MODULES_PATH.charAt(0).toUpperCase() + NODE_MODULES_PATH.substr(1);
		}
		const NODE_MODULES_ASAR_PATH = NODE_MODULES_PATH + '.asar';

		const originalResolveLookupPaths = Module._resolveLookupPaths;
A
Alex Dima 已提交
183 184
		Module._resolveLookupPaths = function (request, parent, newReturn) {
			const result = originalResolveLookupPaths(request, parent, newReturn);
A
Alex Dima 已提交
185

A
Alex Dima 已提交
186
			const paths = newReturn ? result : result[1];
A
Alex Dima 已提交
187 188 189 190 191 192 193 194 195 196 197 198
			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

J
Joao Moreno 已提交
199 200
	// Correctly inherit the parent's environment
	assign(process.env, configuration.userEnv);
201
	perf.importEntries(configuration.perfEntries);
J
Joao Moreno 已提交
202

203
	showPartsSplash(configuration);
204

J
Joao Moreno 已提交
205
	// Get the nls configuration into the process.env as early as possible.
J
Joao Moreno 已提交
206
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
207 208 209 210 211 212 213 214
	const config = process.env['VSCODE_NLS_CONFIG'];
	if (config) {
		process.env['VSCODE_NLS_CONFIG'] = config;
		try {
			nlsConfig = JSON.parse(config);
		} catch (e) { /*noop*/ }
	}

D
Dirk Baeumer 已提交
215 216
	if (nlsConfig._resolvedLanguagePackCoreLocation) {
		let bundles = Object.create(null);
217
		nlsConfig.loadBundle = function (bundle, language, cb) {
D
Dirk Baeumer 已提交
218 219 220 221 222 223 224 225 226 227
			let result = bundles[bundle];
			if (result) {
				cb(undefined, result);
				return;
			}
			let bundleFile = path.join(nlsConfig._resolvedLanguagePackCoreLocation, bundle.replace(/\//g, '!') + '.nls.json');
			readFile(bundleFile).then(function (content) {
				let json = JSON.parse(content);
				bundles[bundle] = json;
				cb(undefined, json);
228 229 230 231 232 233 234 235 236
			}).catch((error) => {
				try {
					if (nlsConfig._corruptedFile) {
						writeFile(nlsConfig._corruptedFile, 'corrupted').catch(function (error) { console.error(error); });
					}
				} finally {
					cb(error, undefined);
				}
			});
D
Dirk Baeumer 已提交
237 238 239
		};
	}

J
Joao Moreno 已提交
240
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
241 242 243 244 245 246 247
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}
	window.document.documentElement.setAttribute('lang', locale);

248
	const enableDeveloperTools = (process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath) && !configuration.extensionTestsPath;
B
Benjamin Pasero 已提交
249
	const unbind = registerListeners(enableDeveloperTools);
J
Joao Moreno 已提交
250 251

	// disable pinch zoom & apply zoom level early to avoid glitches
252
	const zoomLevel = configuration.zoomLevel;
253
	webFrame.setVisualZoomLevelLimits(1, 1);
254 255
	if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
		webFrame.setZoomLevel(zoomLevel);
J
Joao Moreno 已提交
256 257 258
	}

	// Load the loader and start loading the workbench
259 260 261
	const loaderFilename = configuration.appRoot + '/out/vs/loader.js';
	const loaderSource = require('fs').readFileSync(loaderFilename);
	require('vm').runInThisContext(loaderSource, { filename: loaderFilename });
262 263
	var define = global.define;
	global.define = undefined;
J
Joao 已提交
264

265
	window.nodeRequire = require.__$__nodeRequire;
B
Benjamin Pasero 已提交
266

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

269
	window.MonacoEnvironment = {};
J
Joao Moreno 已提交
270

271 272 273 274 275 276 277 278 279
	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 已提交
280

281 282 283
	if (nlsConfig.pseudo) {
		require(['vs/nls'], function (nlsPlugin) {
			nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
J
Joao Moreno 已提交
284
		});
285 286
	}

287
	// Perf Counters
288
	window.MonacoEnvironment.timers = {
289 290 291
		isInitialStartup: !!configuration.isInitialStartup,
		hasAccessibilitySupport: !!configuration.accessibilitySupport,
		start: configuration.perfStartTime,
292
		windowLoad: configuration.perfWindowLoadTime
293 294
	};

J
Johannes Rieken 已提交
295
	perf.mark('willLoadWorkbenchMain');
296 297 298 299 300
	require([
		'vs/workbench/workbench.main',
		'vs/nls!vs/workbench/workbench.main',
		'vs/css!vs/workbench/workbench.main'
	], function () {
J
Johannes Rieken 已提交
301
		perf.mark('didLoadWorkbenchMain');
302 303 304 305 306 307 308 309 310 311 312 313

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

J
Joao Moreno 已提交
315 316
}

317
main();