vscode/src/main.js

129 lines
4.1 KiB
JavaScript
Raw Normal View History

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.
*--------------------------------------------------------------------------------------------*/
// Perf measurements
global.vscodeStart = Date.now();
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-05-04 10:43:55 +02:00
var paths = require('./paths');
var pkg = require('../package.json');
2016-02-09 14:59:11 +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-02-09 15:23:19 +01:00
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;
}
}
if (!locale) {
var userData = app.getPath('userData');
2016-05-04 10:12:49 +02:00
var localeConfig = path.join(userData, 'User', 'locale.json');
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;
if (value && typeof value === 'string') {
locale = value;
}
} catch (e) {
}
}
}
locale = locale || app.getLocale();
// 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-02-12 16:06:34 +01: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
}
// We have a built version so we have extracted nls file. Try to find
// the right file to use.
while (locale) {
var candidate = path.join(__dirname, 'vs', 'code', 'electron-main', 'main.nls.') + locale + '.js';
2016-02-09 15:23:19 +01:00
if (fs.existsSync(candidate)) {
2016-02-12 16:06:34 +01:00
return { locale: initialLocale, availableLanguages: { '*': locale } };
2016-02-09 15:23:19 +01:00
} else {
var index = locale.lastIndexOf('-');
if (index > 0) {
locale = locale.substring(0, index);
} else {
locale = null;
}
}
}
2016-02-12 16:06:34 +01:00
return { locale: initialLocale, availableLanguages: {} };
2016-02-09 15:23:19 +01:00
}
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
}
2016-05-04 10:43:55 +02:00
// Set userData path before app 'ready' event
var userData = paths.getUserDataPath(process.platform, pkg.name, process.argv);
app.setPath('userData', userData);
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);
});
// Load our code once ready
2016-04-07 09:23:27 +02:00
app.once('ready', function () {
var nlsConfig = getNLSConfiguration();
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
require('./bootstrap-amd').bootstrap('vs/code/electron-main/main');
2016-02-09 14:59:11 +01:00
});