index.js 9.3 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 85 86
function showPartsSplash(configuration) {

	let key;
87
	let keep = false;
88
	// this is the logic of StorageService#getWorkspaceKey and StorageService#toStorageKey
J
Johannes Rieken 已提交
89 90
	if (configuration.folderUri) {
		let workspaceKey = require('vscode-uri').default.revive(configuration.folderUri).toString().replace('file:///', '').replace(/^\//, '');
91
		key = `storage://workspace/${workspaceKey}/parts-splash`;
92 93 94 95
	} else if (configuration.workspace) {
		key = `storage://workspace/root:${configuration.workspace.id}/parts-splash`;
	} else {
		key = `storage://global/parts-splash`;
96
		keep = true;
97 98
	}

99 100 101 102 103 104
	// TODO@Ben remove me after a while
	perf.mark('willAccessLocalStorage');
	let storage = window.localStorage;
	perf.mark('didAccessLocalStorage');

	let structure = storage.getItem(key);
105 106 107 108
	if (structure) {
		let splash = document.createElement('div');
		splash.innerHTML = structure;
		document.body.appendChild(splash);
109 110
	}
	if (!keep) {
111
		storage.removeItem(key);
112 113 114
	}
}

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

J
Joao Moreno 已提交
117 118 119
function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
B
Benjamin Pasero 已提交
120
	var listener;
J
Joao Moreno 已提交
121
	if (enableDeveloperTools) {
122
		const extractKey = function (e) {
J
Joao Moreno 已提交
123 124 125 126 127 128 129 130 131 132 133 134
			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 已提交
135
		listener = function (e) {
J
Joao Moreno 已提交
136 137
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
J
Joao Moreno 已提交
138
				remote.getCurrentWebContents().toggleDevTools();
J
Joao Moreno 已提交
139
			} else if (key === RELOAD_KB) {
140
				remote.getCurrentWindow().reload();
J
Joao Moreno 已提交
141
			}
B
Benjamin Pasero 已提交
142 143
		};
		window.addEventListener('keydown', listener);
J
Joao Moreno 已提交
144 145
	}

J
Joao Moreno 已提交
146
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools); });
B
Benjamin Pasero 已提交
147 148 149 150 151 152

	return function () {
		if (listener) {
			window.removeEventListener('keydown', listener);
			listener = void 0;
		}
J
Joao Moreno 已提交
153
	};
J
Joao Moreno 已提交
154 155 156 157 158 159 160
}

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

A
Alex Dima 已提交
161 162 163 164 165 166 167 168 169 170 171 172
	//#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 已提交
173 174
		Module._resolveLookupPaths = function (request, parent, newReturn) {
			const result = originalResolveLookupPaths(request, parent, newReturn);
A
Alex Dima 已提交
175

A
Alex Dima 已提交
176
			const paths = newReturn ? result : result[1];
A
Alex Dima 已提交
177 178 179 180 181 182 183 184 185 186 187 188
			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 已提交
189 190
	// Correctly inherit the parent's environment
	assign(process.env, configuration.userEnv);
191
	perf.importEntries(configuration.perfEntries);
J
Joao Moreno 已提交
192

193
	showPartsSplash(configuration);
194

J
Joao Moreno 已提交
195
	// Get the nls configuration into the process.env as early as possible.
J
Joao Moreno 已提交
196
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
197 198 199 200 201 202 203 204
	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 已提交
205 206
	if (nlsConfig._resolvedLanguagePackCoreLocation) {
		let bundles = Object.create(null);
207
		nlsConfig.loadBundle = function (bundle, language, cb) {
D
Dirk Baeumer 已提交
208 209 210 211 212 213 214 215 216 217
			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);
218 219 220 221 222 223 224 225 226
			}).catch((error) => {
				try {
					if (nlsConfig._corruptedFile) {
						writeFile(nlsConfig._corruptedFile, 'corrupted').catch(function (error) { console.error(error); });
					}
				} finally {
					cb(error, undefined);
				}
			});
D
Dirk Baeumer 已提交
227 228 229
		};
	}

J
Joao Moreno 已提交
230
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
231 232 233 234 235 236 237
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}
	window.document.documentElement.setAttribute('lang', locale);

238
	const enableDeveloperTools = (process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath) && !configuration.extensionTestsPath;
B
Benjamin Pasero 已提交
239
	const unbind = registerListeners(enableDeveloperTools);
J
Joao Moreno 已提交
240 241

	// disable pinch zoom & apply zoom level early to avoid glitches
242
	const zoomLevel = configuration.zoomLevel;
243
	webFrame.setVisualZoomLevelLimits(1, 1);
244 245
	if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
		webFrame.setZoomLevel(zoomLevel);
J
Joao Moreno 已提交
246 247 248
	}

	// Load the loader and start loading the workbench
249 250 251
	const loaderFilename = configuration.appRoot + '/out/vs/loader.js';
	const loaderSource = require('fs').readFileSync(loaderFilename);
	require('vm').runInThisContext(loaderSource, { filename: loaderFilename });
252 253
	var define = global.define;
	global.define = undefined;
J
Joao 已提交
254

255
	window.nodeRequire = require.__$__nodeRequire;
B
Benjamin Pasero 已提交
256

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

259
	window.MonacoEnvironment = {};
J
Joao Moreno 已提交
260

261 262 263 264 265 266 267 268 269
	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 已提交
270

271 272 273
	if (nlsConfig.pseudo) {
		require(['vs/nls'], function (nlsPlugin) {
			nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
J
Joao Moreno 已提交
274
		});
275 276
	}

277
	// Perf Counters
278
	window.MonacoEnvironment.timers = {
279 280 281
		isInitialStartup: !!configuration.isInitialStartup,
		hasAccessibilitySupport: !!configuration.accessibilitySupport,
		start: configuration.perfStartTime,
282
		windowLoad: configuration.perfWindowLoadTime
283 284
	};

J
Johannes Rieken 已提交
285
	perf.mark('willLoadWorkbenchMain');
286 287 288 289 290
	require([
		'vs/workbench/workbench.main',
		'vs/nls!vs/workbench/workbench.main',
		'vs/css!vs/workbench/workbench.main'
	], function () {
J
Johannes Rieken 已提交
291
		perf.mark('didLoadWorkbenchMain');
292 293 294 295 296 297 298 299 300 301 302 303

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

J
Joao Moreno 已提交
305 306
}

307
main();