2016-02-09 14:59:11 +01:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
2016-09-19 10:22:57 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-11-06 18:50:57 +01:00
|
|
|
var perf = require('./vs/base/common/performance');
|
|
|
|
perf.mark('main:started');
|
|
|
|
|
2016-02-09 14:59:11 +01:00
|
|
|
// Perf measurements
|
2016-11-16 12:25:30 +01:00
|
|
|
global.perfStartTime = Date.now();
|
2016-02-09 14:59:11 +01:00
|
|
|
|
|
|
|
var app = require('electron').app;
|
2016-02-09 15:23:19 +01:00
|
|
|
var fs = require('fs');
|
2016-02-09 14:59:11 +01:00
|
|
|
var path = require('path');
|
2016-08-17 11:16:02 +02:00
|
|
|
var minimist = require('minimist');
|
2016-05-04 10:43:55 +02:00
|
|
|
var paths = require('./paths');
|
2016-02-09 14:59:11 +01:00
|
|
|
|
2016-08-26 16:47:57 +02:00
|
|
|
var args = minimist(process.argv, {
|
|
|
|
string: ['user-data-dir', 'locale']
|
|
|
|
});
|
|
|
|
|
2016-03-15 16:14:59 +01:00
|
|
|
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;
|
2016-07-27 09:31:07 +02:00
|
|
|
}
|
2016-03-15 16:14:59 +01:00
|
|
|
|
2016-02-09 15:23:19 +01:00
|
|
|
function getNLSConfiguration() {
|
2016-08-26 16:47:57 +02:00
|
|
|
var locale = args['locale'];
|
2016-02-09 15:23:19 +01:00
|
|
|
|
2016-03-15 16:14:59 +01:00
|
|
|
if (!locale) {
|
|
|
|
var userData = app.getPath('userData');
|
2016-05-04 10:12:49 +02:00
|
|
|
var localeConfig = path.join(userData, 'User', 'locale.json');
|
2016-03-15 16:14:59 +01:00
|
|
|
if (fs.existsSync(localeConfig)) {
|
|
|
|
try {
|
|
|
|
var content = stripComments(fs.readFileSync(localeConfig, 'utf8'));
|
2016-05-04 10:12:49 +02:00
|
|
|
var value = JSON.parse(content).locale;
|
2016-03-15 16:14:59 +01:00
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
locale = value;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2016-07-27 09:31:07 +02:00
|
|
|
// noop
|
2016-03-15 16:14:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 15:22:59 +02:00
|
|
|
var appLocale = app.getLocale();
|
|
|
|
locale = locale || appLocale;
|
2016-03-22 12:50:39 +01:00
|
|
|
// 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;
|
2016-02-09 15:23:19 +01:00
|
|
|
if (locale === 'pseudo') {
|
2016-09-19 10:22:57 +02:00
|
|
|
return { locale: locale, availableLanguages: {}, pseudo: true };
|
2016-02-09 15:23:19 +01:00
|
|
|
}
|
2016-02-12 16:06:34 +01:00
|
|
|
var initialLocale = locale;
|
2016-05-02 18:53:46 +02:00
|
|
|
if (process.env['VSCODE_DEV']) {
|
2016-02-12 16:06:34 +01:00
|
|
|
return { locale: locale, availableLanguages: {} };
|
2016-02-09 15:23:19 +01:00
|
|
|
}
|
2016-05-20 15:22:59 +02:00
|
|
|
|
2016-02-09 15:23:19 +01:00
|
|
|
// We have a built version so we have extracted nls file. Try to find
|
|
|
|
// the right file to use.
|
2016-05-20 15:22:59 +02:00
|
|
|
|
|
|
|
// 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 } };
|
2016-02-09 15:23:19 +01:00
|
|
|
} else {
|
2016-05-20 15:22:59 +02:00
|
|
|
var index = locale.lastIndexOf('-');
|
|
|
|
if (index > 0) {
|
|
|
|
locale = locale.substring(0, index);
|
|
|
|
} else {
|
|
|
|
locale = null;
|
|
|
|
}
|
2016-02-09 15:23:19 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-20 15:22:59 +02:00
|
|
|
return null;
|
2016-02-09 15:23:19 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 15:22:59 +02:00
|
|
|
var resolvedLocale = resolveLocale(locale);
|
|
|
|
if (!resolvedLocale && appLocale && appLocale !== locale) {
|
|
|
|
resolvedLocale = resolveLocale(appLocale);
|
|
|
|
}
|
|
|
|
return resolvedLocale ? resolvedLocale : { locale: initialLocale, availableLanguages: {} };
|
2016-02-09 15:23:19 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 11:42:46 +01:00
|
|
|
function getNodeCachedDataDir() {
|
2017-11-23 10:53:25 +01:00
|
|
|
// flag to disable cached data support
|
|
|
|
if (process.argv.indexOf('--no-cached-data') > 0) {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
}
|
2016-12-21 11:42:46 +01:00
|
|
|
|
|
|
|
// IEnvironmentService.isBuilt
|
|
|
|
if (process.env['VSCODE_DEV']) {
|
2016-12-21 14:26:10 +01:00
|
|
|
return Promise.resolve(undefined);
|
2016-12-21 11:42:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-05 09:21:02 +02:00
|
|
|
// find commit id
|
|
|
|
var productJson = require(path.join(__dirname, '../product.json'));
|
|
|
|
if (!productJson.commit) {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dir = path.join(app.getPath('userData'), 'CachedData', productJson.commit);
|
2016-12-21 11:42:46 +01:00
|
|
|
|
2017-06-20 16:21:44 +02:00
|
|
|
return mkdirp(dir).then(undefined, function () { /*ignore*/ });
|
2017-01-27 20:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function mkdirp(dir) {
|
|
|
|
return mkdir(dir)
|
|
|
|
.then(null, function (err) {
|
|
|
|
if (err && err.code === 'ENOENT') {
|
|
|
|
var parent = path.dirname(dir);
|
|
|
|
if (parent !== dir) { // if not arrived at root
|
|
|
|
return mkdirp(parent)
|
|
|
|
.then(function () {
|
|
|
|
return mkdir(dir);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mkdir(dir) {
|
2016-12-21 12:46:01 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
fs.mkdir(dir, function (err) {
|
|
|
|
if (err && err.code !== 'EEXIST') {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(dir);
|
|
|
|
}
|
|
|
|
});
|
2016-12-21 11:42:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-22 12:41:35 +02:00
|
|
|
// Set userData path before app 'ready' event and call to process.chdir
|
2017-05-19 15:31:56 +02:00
|
|
|
var userData = path.resolve(args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform));
|
2016-10-22 12:41:35 +02:00
|
|
|
app.setPath('userData', userData);
|
|
|
|
|
2016-04-07 09:23:27 +02:00
|
|
|
// Update cwd based on environment and platform
|
2016-02-09 14:59:11 +01:00
|
|
|
try {
|
2016-04-07 09:23:27 +02:00
|
|
|
if (process.platform === 'win32') {
|
2016-05-02 18:53:46 +02:00
|
|
|
process.env['VSCODE_CWD'] = process.cwd(); // remember as environment variable
|
2016-04-07 09:23:27 +02:00
|
|
|
process.chdir(path.dirname(app.getPath('exe'))); // always set application folder as cwd
|
2016-05-02 18:53:46 +02:00
|
|
|
} else if (process.env['VSCODE_CWD']) {
|
|
|
|
process.chdir(process.env['VSCODE_CWD']);
|
2016-02-09 14:59:11 +01:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2016-04-07 09:23:27 +02:00
|
|
|
console.error(err);
|
2016-02-09 14:59:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = [];
|
2016-04-07 09:23:27 +02:00
|
|
|
app.on('open-file', function (event, path) {
|
2016-02-09 14:59:11 +01:00
|
|
|
global.macOpenFiles.push(path);
|
|
|
|
});
|
|
|
|
|
2016-09-19 10:22:57 +02:00
|
|
|
var openUrls = [];
|
|
|
|
var onOpenUrl = function (event, url) {
|
2016-09-16 12:21:41 +02:00
|
|
|
event.preventDefault();
|
|
|
|
openUrls.push(url);
|
|
|
|
};
|
|
|
|
|
2016-09-19 10:22:57 +02:00
|
|
|
app.on('will-finish-launching', function () {
|
|
|
|
app.on('open-url', onOpenUrl);
|
|
|
|
});
|
2016-09-16 12:21:41 +02:00
|
|
|
|
2016-09-19 10:22:57 +02:00
|
|
|
global.getOpenUrls = function () {
|
2016-09-16 12:21:41 +02:00
|
|
|
app.removeListener('open-url', onOpenUrl);
|
|
|
|
return openUrls;
|
|
|
|
};
|
|
|
|
|
2016-12-21 11:42:46 +01:00
|
|
|
|
2016-12-21 12:46:01 +01:00
|
|
|
// use '<UserData>/CachedData'-directory to store
|
|
|
|
// node/v8 cached data.
|
2016-12-21 11:42:46 +01:00
|
|
|
var nodeCachedDataDir = getNodeCachedDataDir().then(function (value) {
|
2016-12-21 14:26:10 +01:00
|
|
|
if (value) {
|
2017-04-21 10:42:20 +02:00
|
|
|
// store the data directory
|
2016-12-21 14:26:10 +01:00
|
|
|
process.env['VSCODE_NODE_CACHED_DATA_DIR_' + process.pid] = value;
|
2017-04-21 10:42:20 +02:00
|
|
|
|
|
|
|
// tell v8 to not be lazy when parsing JavaScript. Generally this makes startup slower
|
|
|
|
// but because we generate cached data it makes subsequent startups much faster
|
|
|
|
app.commandLine.appendSwitch('--js-flags', '--nolazy');
|
2016-12-21 14:26:10 +01:00
|
|
|
}
|
2016-12-21 11:42:46 +01:00
|
|
|
});
|
|
|
|
|
2017-06-09 10:34:35 +02:00
|
|
|
// Load our code once ready
|
|
|
|
app.once('ready', function () {
|
2017-11-06 18:50:57 +01:00
|
|
|
perf.mark('main:appReady');
|
2017-06-09 10:34:35 +02:00
|
|
|
var nlsConfig = getNLSConfiguration();
|
|
|
|
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
|
|
|
|
|
|
|
|
nodeCachedDataDir.then(function () {
|
|
|
|
require('./bootstrap-amd').bootstrap('vs/code/electron-main/main');
|
|
|
|
}, console.error);
|
2016-12-21 11:42:46 +01:00
|
|
|
});
|