main.js 5.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.
 *--------------------------------------------------------------------------------------------*/

6 7
'use strict';

J
Joao Moreno 已提交
8
// Perf measurements
9
global.perfStartTime = Date.now();
J
Joao Moreno 已提交
10 11

var app = require('electron').app;
J
Joao Moreno 已提交
12
var fs = require('fs');
J
Joao Moreno 已提交
13
var path = require('path');
14
var minimist = require('minimist');
J
Joao Moreno 已提交
15
var paths = require('./paths');
J
Joao Moreno 已提交
16

J
Joao Moreno 已提交
17 18 19 20
var args = minimist(process.argv, {
	string: ['user-data-dir', 'locale']
});

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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
lint  
Joao Moreno 已提交
45
}
46

J
Joao Moreno 已提交
47
function getNLSConfiguration() {
J
Joao Moreno 已提交
48
	var locale = args['locale'];
J
Joao Moreno 已提交
49

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

66 67
	var appLocale = app.getLocale();
	locale = locale || appLocale;
68 69 70 71 72
	// 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 已提交
73
	if (locale === 'pseudo') {
74
		return { locale: locale, availableLanguages: {}, pseudo: true };
J
Joao Moreno 已提交
75
	}
76
	var initialLocale = locale;
J
Joao Moreno 已提交
77
	if (process.env['VSCODE_DEV']) {
78
		return { locale: locale, availableLanguages: {} };
J
Joao Moreno 已提交
79
	}
80

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

	// 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 已提交
95
			} else {
96 97 98 99 100 101
				var index = locale.lastIndexOf('-');
				if (index > 0) {
					locale = locale.substring(0, index);
				} else {
					locale = null;
				}
J
Joao Moreno 已提交
102 103
			}
		}
104
		return null;
J
Joao Moreno 已提交
105 106
	}

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

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
function getNodeCachedDataDir() {

	// IEnvironmentService.isBuilt
	if (process.env['VSCODE_DEV']) {
		return Promise.resolve();
	}

	function ensureDir(dir) {
		return new Promise(function (resolve, reject) {
			fs.mkdir(dir, function (err) {
				if (err && err.code !== 'EEXIST') {
					reject(err);
				} else {
					resolve(dir);
				}
			});
		});
	}

	var basedir = path.join(app.getPath('userData'), 'CachedData');
	return ensureDir(basedir).then(function () {
		var version = require(path.join(__dirname, '../package.json')).version;
		var cachedDataDir = path.join(basedir, version);
		return ensureDir(cachedDataDir);
	});
}

141 142 143 144
// Set userData path before app 'ready' event and call to process.chdir
var userData = path.resolve(args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform));
app.setPath('userData', userData);

B
polish  
Benjamin Pasero 已提交
145
// Update cwd based on environment and platform
J
Joao Moreno 已提交
146
try {
B
polish  
Benjamin Pasero 已提交
147
	if (process.platform === 'win32') {
J
Joao Moreno 已提交
148
		process.env['VSCODE_CWD'] = process.cwd(); // remember as environment variable
B
polish  
Benjamin Pasero 已提交
149
		process.chdir(path.dirname(app.getPath('exe'))); // always set application folder as cwd
J
Joao Moreno 已提交
150 151
	} else if (process.env['VSCODE_CWD']) {
		process.chdir(process.env['VSCODE_CWD']);
J
Joao Moreno 已提交
152 153
	}
} catch (err) {
B
polish  
Benjamin Pasero 已提交
154
	console.error(err);
J
Joao Moreno 已提交
155 156 157 158 159
}

// 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 已提交
160
app.on('open-file', function (event, path) {
J
Joao Moreno 已提交
161 162 163
	global.macOpenFiles.push(path);
});

164 165
var openUrls = [];
var onOpenUrl = function (event, url) {
J
Joao Moreno 已提交
166 167 168 169
	event.preventDefault();
	openUrls.push(url);
};

170 171 172
app.on('will-finish-launching', function () {
	app.on('open-url', onOpenUrl);
});
J
Joao Moreno 已提交
173

174
global.getOpenUrls = function () {
J
Joao Moreno 已提交
175 176 177 178
	app.removeListener('open-url', onOpenUrl);
	return openUrls;
};

179 180 181 182 183

var nodeCachedDataDir = getNodeCachedDataDir().then(function (value) {
	process.env['VSCODE_NODE_CACHED_DATA_DIR_' + process.pid] = value;
});

J
Joao Moreno 已提交
184
// Load our code once ready
B
polish  
Benjamin Pasero 已提交
185
app.once('ready', function () {
186 187
	var nlsConfig = getNLSConfiguration();
	process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
188 189 190 191 192

	nodeCachedDataDir.then(function () {
		require('./bootstrap-amd').bootstrap('vs/code/electron-main/main');
	});
});