main.js 4.6 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

// Perf measurements
global.vscodeStart = Date.now();

var app = require('electron').app;
J
Joao Moreno 已提交
10
var fs = require('fs');
J
Joao Moreno 已提交
11
var path = require('path');
J
Joao Moreno 已提交
12 13
var paths = require('./paths');
var pkg = require('../package.json');
J
Joao Moreno 已提交
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
function stripComments(content) {
	var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
	var result = content.replace(regexp, function (match, m1, m2, m3, m4) {
		// Only one of m1, m2, m3, m4 matches
		if (m3) {
			// A block comment. Replace with nothing
			return '';
		}
		else if (m4) {
			// A line comment. If it ends in \r?\n then keep it.
			var length_1 = m4.length;
			if (length_1 > 2 && m4[length_1 - 1] === '\n') {
				return m4[length_1 - 2] === '\r' ? '\r\n' : '\n';
			}
			else {
				return '';
			}
		}
		else {
			// We match a string
			return match;
		}
	});
	return result;
};

J
Joao Moreno 已提交
41 42 43 44 45 46 47 48 49 50 51 52
function getNLSConfiguration() {
	var locale = undefined;
	var localeOpts = '--locale';
	for (var i = 0; i < process.argv.length; i++) {
		var arg = process.argv[i];
		if (arg.slice(0, localeOpts.length) == localeOpts) {
			var segments = arg.split('=');
			locale = segments[1];
			break;
		}
	}

53 54
	if (!locale) {
		var userData = app.getPath('userData');
J
Joao Moreno 已提交
55
		var localeConfig = path.join(userData, 'User', 'locale.json');
56 57 58
		if (fs.existsSync(localeConfig)) {
			try {
				var content = stripComments(fs.readFileSync(localeConfig, 'utf8'));
J
Joao Moreno 已提交
59
				var value = JSON.parse(content).locale;
60 61 62 63 64 65 66 67
				if (value && typeof value === 'string') {
					locale = value;
				}
			} catch (e) {
			}
		}
	}

68 69
	var appLocale = app.getLocale();
	locale = locale || appLocale;
70 71 72 73 74
	// Language tags are case insensitve however an amd loader is case sensitive
	// To make this work on case preserving & insensitive FS we do the following:
	// the language bundles have lower case language tags and we always lower case
	// the locale we receive from the user or OS.
	locale = locale ? locale.toLowerCase() : locale;
J
Joao Moreno 已提交
75
	if (locale === 'pseudo') {
76
		return { locale: locale, availableLanguages: {}, pseudo: true }
J
Joao Moreno 已提交
77
	}
78
	var initialLocale = locale;
J
Joao Moreno 已提交
79
	if (process.env['VSCODE_DEV']) {
80
		return { locale: locale, availableLanguages: {} };
J
Joao Moreno 已提交
81
	}
82

J
Joao Moreno 已提交
83 84
	// We have a built version so we have extracted nls file. Try to find
	// the right file to use.
85 86 87 88 89 90 91 92 93 94 95 96

	// Check if we have an English locale. If so fall to default since that is our
	// English translation (we don't ship *.nls.en.json files)
	if (locale && (locale == 'en' || locale.startsWith('en-'))) {
		return { locale: locale, availableLanguages: {} };
	}

	function resolveLocale(locale) {
		while (locale) {
			var candidate = path.join(__dirname, 'vs', 'code', 'electron-main', 'main.nls.') + locale + '.js';
			if (fs.existsSync(candidate)) {
				return { locale: initialLocale, availableLanguages: { '*': locale } };
J
Joao Moreno 已提交
97
			} else {
98 99 100 101 102 103
				var index = locale.lastIndexOf('-');
				if (index > 0) {
					locale = locale.substring(0, index);
				} else {
					locale = null;
				}
J
Joao Moreno 已提交
104 105
			}
		}
106
		return null;
J
Joao Moreno 已提交
107 108
	}

109 110 111 112 113
	var resolvedLocale = resolveLocale(locale);
	if (!resolvedLocale && appLocale && appLocale !== locale) {
		resolvedLocale = resolveLocale(appLocale);
	}
	return resolvedLocale ? resolvedLocale : { locale: initialLocale, availableLanguages: {} };
J
Joao Moreno 已提交
114 115
}

B
polish  
Benjamin Pasero 已提交
116
// Update cwd based on environment and platform
J
Joao Moreno 已提交
117
try {
B
polish  
Benjamin Pasero 已提交
118
	if (process.platform === 'win32') {
J
Joao Moreno 已提交
119
		process.env['VSCODE_CWD'] = process.cwd(); // remember as environment variable
B
polish  
Benjamin Pasero 已提交
120
		process.chdir(path.dirname(app.getPath('exe'))); // always set application folder as cwd
J
Joao Moreno 已提交
121 122
	} else if (process.env['VSCODE_CWD']) {
		process.chdir(process.env['VSCODE_CWD']);
J
Joao Moreno 已提交
123 124
	}
} catch (err) {
B
polish  
Benjamin Pasero 已提交
125
	console.error(err);
J
Joao Moreno 已提交
126 127
}

J
Joao Moreno 已提交
128 129 130
// Set userData path before app 'ready' event
var userData = paths.getUserDataPath(process.platform, pkg.name, process.argv);
app.setPath('userData', userData);
131

J
Joao Moreno 已提交
132 133 134
// Mac: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
// the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
global.macOpenFiles = [];
B
polish  
Benjamin Pasero 已提交
135
app.on('open-file', function (event, path) {
J
Joao Moreno 已提交
136 137 138 139
	global.macOpenFiles.push(path);
});

// Load our code once ready
B
polish  
Benjamin Pasero 已提交
140
app.once('ready', function () {
141 142
	var nlsConfig = getNLSConfiguration();
	process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
143
	require('./bootstrap-amd').bootstrap('vs/code/electron-main/main');
J
Joao Moreno 已提交
144
});