index.js 8.7 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 87 88 89 90 91 92 93 94
function showPartsSplash(folder) {
	const storageKey = `storage://workspace${folder}/_splash_`;
	const structure = window.localStorage.getItem(storageKey);
	if (structure) {
		let splash = document.createElement('div');
		splash.innerHTML = structure;
		document.body.appendChild(splash);
		window.localStorage.removeItem(storageKey);
	}
}

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

J
Joao Moreno 已提交
97 98 99
function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
B
Benjamin Pasero 已提交
100
	var listener;
J
Joao Moreno 已提交
101
	if (enableDeveloperTools) {
102
		const extractKey = function (e) {
J
Joao Moreno 已提交
103 104 105 106 107 108 109 110 111 112 113 114
			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 已提交
115
		listener = function (e) {
J
Joao Moreno 已提交
116 117
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
J
Joao Moreno 已提交
118
				remote.getCurrentWebContents().toggleDevTools();
J
Joao Moreno 已提交
119
			} else if (key === RELOAD_KB) {
120
				remote.getCurrentWindow().reload();
J
Joao Moreno 已提交
121
			}
B
Benjamin Pasero 已提交
122 123
		};
		window.addEventListener('keydown', listener);
J
Joao Moreno 已提交
124 125
	}

J
Joao Moreno 已提交
126
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools); });
B
Benjamin Pasero 已提交
127 128 129 130 131 132

	return function () {
		if (listener) {
			window.removeEventListener('keydown', listener);
			listener = void 0;
		}
J
Joao Moreno 已提交
133
	};
J
Joao Moreno 已提交
134 135 136 137 138 139 140
}

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

A
Alex Dima 已提交
141 142 143 144 145 146 147 148 149 150 151 152
	//#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 已提交
153 154
		Module._resolveLookupPaths = function (request, parent, newReturn) {
			const result = originalResolveLookupPaths(request, parent, newReturn);
A
Alex Dima 已提交
155

A
Alex Dima 已提交
156
			const paths = newReturn ? result : result[1];
A
Alex Dima 已提交
157 158 159 160 161 162 163 164 165 166 167 168
			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 已提交
169 170
	// Correctly inherit the parent's environment
	assign(process.env, configuration.userEnv);
171
	perf.importEntries(configuration.perfEntries);
J
Joao Moreno 已提交
172

173 174
	showPartsSplash(configuration.folderPath);

J
Joao Moreno 已提交
175
	// Get the nls configuration into the process.env as early as possible.
J
Joao Moreno 已提交
176
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
177 178 179 180 181 182 183 184
	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 已提交
185 186
	if (nlsConfig._resolvedLanguagePackCoreLocation) {
		let bundles = Object.create(null);
187
		nlsConfig.loadBundle = function (bundle, language, cb) {
D
Dirk Baeumer 已提交
188 189 190 191 192 193 194 195 196 197
			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);
198 199 200 201 202 203 204 205 206
			}).catch((error) => {
				try {
					if (nlsConfig._corruptedFile) {
						writeFile(nlsConfig._corruptedFile, 'corrupted').catch(function (error) { console.error(error); });
					}
				} finally {
					cb(error, undefined);
				}
			});
D
Dirk Baeumer 已提交
207 208 209
		};
	}

J
Joao Moreno 已提交
210
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
211 212 213 214 215 216 217
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}
	window.document.documentElement.setAttribute('lang', locale);

218
	const enableDeveloperTools = (process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath) && !configuration.extensionTestsPath;
B
Benjamin Pasero 已提交
219
	const unbind = registerListeners(enableDeveloperTools);
J
Joao Moreno 已提交
220 221

	// disable pinch zoom & apply zoom level early to avoid glitches
222
	const zoomLevel = configuration.zoomLevel;
223
	webFrame.setVisualZoomLevelLimits(1, 1);
224 225
	if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
		webFrame.setZoomLevel(zoomLevel);
J
Joao Moreno 已提交
226 227 228
	}

	// Load the loader and start loading the workbench
229 230 231
	const loaderFilename = configuration.appRoot + '/out/vs/loader.js';
	const loaderSource = require('fs').readFileSync(loaderFilename);
	require('vm').runInThisContext(loaderSource, { filename: loaderFilename });
232 233
	var define = global.define;
	global.define = undefined;
J
Joao 已提交
234

235
	window.nodeRequire = require.__$__nodeRequire;
B
Benjamin Pasero 已提交
236

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

239
	window.MonacoEnvironment = {};
J
Joao Moreno 已提交
240

241 242 243 244 245 246 247 248 249
	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 已提交
250

251 252 253
	if (nlsConfig.pseudo) {
		require(['vs/nls'], function (nlsPlugin) {
			nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
J
Joao Moreno 已提交
254
		});
255 256
	}

257
	// Perf Counters
258
	window.MonacoEnvironment.timers = {
259 260 261
		isInitialStartup: !!configuration.isInitialStartup,
		hasAccessibilitySupport: !!configuration.accessibilitySupport,
		start: configuration.perfStartTime,
262
		windowLoad: configuration.perfWindowLoadTime
263 264
	};

J
Johannes Rieken 已提交
265
	perf.mark('willLoadWorkbenchMain');
266 267 268 269 270
	require([
		'vs/workbench/workbench.main',
		'vs/nls!vs/workbench/workbench.main',
		'vs/css!vs/workbench/workbench.main'
	], function () {
J
Johannes Rieken 已提交
271
		perf.mark('didLoadWorkbenchMain');
272 273 274 275 276 277 278 279 280 281 282 283

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

J
Joao Moreno 已提交
285 286
}

287
main();