electron/atom/browser/lib/init.js

153 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-01-14 21:21:11 +00:00
const fs = require('fs');
const path = require('path');
const util = require('util');
const Module = require('module');
2016-01-12 02:40:23 +00:00
2016-01-14 21:21:11 +00:00
var slice = [].slice;
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// We modified the original process.argv to let node.js load the atom.js,
// we need to restore it here.
2016-01-12 02:40:23 +00:00
process.argv.splice(1, 1);
2016-01-14 18:35:29 +00:00
// Clear search paths.
2016-01-12 02:40:23 +00:00
require(path.resolve(__dirname, '..', '..', 'common', 'lib', 'reset-search-paths'));
2016-01-14 18:35:29 +00:00
// Import common settings.
2016-01-12 02:40:23 +00:00
require(path.resolve(__dirname, '..', '..', 'common', 'lib', 'init'));
2016-01-14 21:21:11 +00:00
var globalPaths = Module.globalPaths;
2016-01-12 02:40:23 +00:00
if (!process.env.ELECTRON_HIDE_INTERNAL_MODULES) {
globalPaths.push(path.resolve(__dirname, '..', 'api', 'lib'));
}
2016-01-14 18:35:29 +00:00
// Expose public APIs.
2016-01-12 02:40:23 +00:00
globalPaths.push(path.resolve(__dirname, '..', 'api', 'lib', 'exports'));
if (process.platform === 'win32') {
2016-01-14 18:44:21 +00:00
// Redirect node's console to use our own implementations, since node can not
// handle console output when running as GUI program.
2016-01-14 21:21:11 +00:00
var consoleLog = function() {
2016-01-12 02:40:23 +00:00
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return process.log(util.format.apply(util, args) + "\n");
};
2016-01-14 21:21:11 +00:00
var streamWrite = function(chunk, encoding, callback) {
2016-01-12 02:40:23 +00:00
if (Buffer.isBuffer(chunk)) {
chunk = chunk.toString(encoding);
}
process.log(chunk);
if (callback) {
callback();
}
return true;
};
console.log = console.error = console.warn = consoleLog;
process.stdout.write = process.stderr.write = streamWrite;
2016-01-14 18:35:29 +00:00
// Always returns EOF for stdin stream.
2016-01-14 21:21:11 +00:00
var Readable = require('stream').Readable;
var stdin = new Readable;
2016-01-12 02:40:23 +00:00
stdin.push(null);
process.__defineGetter__('stdin', function() {
return stdin;
});
}
2016-01-14 18:35:29 +00:00
// Don't quit on fatal error.
2016-01-12 02:40:23 +00:00
process.on('uncaughtException', function(error) {
2016-01-14 18:35:29 +00:00
// Do nothing if the user has a custom uncaught exception handler.
2016-01-12 02:40:23 +00:00
var dialog, message, ref, stack;
if (process.listeners('uncaughtException').length > 1) {
return;
}
2016-01-14 18:35:29 +00:00
// Show error in GUI.
2016-01-12 02:40:23 +00:00
dialog = require('electron').dialog;
stack = (ref = error.stack) != null ? ref : error.name + ": " + error.message;
message = "Uncaught Exception:\n" + stack;
return dialog.showErrorBox('A JavaScript error occurred in the main process', message);
});
2016-01-14 18:35:29 +00:00
// Emit 'exit' event on quit.
2016-01-14 21:21:11 +00:00
var app = require('electron').app;
2016-01-12 02:40:23 +00:00
app.on('quit', function(event, exitCode) {
return process.emit('exit', exitCode);
});
2016-01-14 18:35:29 +00:00
// Map process.exit to app.exit, which quits gracefully.
2016-01-12 02:40:23 +00:00
process.exit = app.exit;
2016-01-14 18:35:29 +00:00
// Load the RPC server.
2016-01-12 02:40:23 +00:00
require('./rpc-server');
2016-01-14 18:35:29 +00:00
// Load the guest view manager.
2016-01-12 02:40:23 +00:00
require('./guest-view-manager');
require('./guest-window-manager');
2016-01-14 18:35:29 +00:00
// Now we try to load app's package.json.
2016-01-14 21:21:11 +00:00
var packageJson = null;
var searchPaths = ['app', 'app.asar', 'default_app'];
2016-01-15 02:07:29 +00:00
var i, len, packagePath;
2016-01-12 02:40:23 +00:00
for (i = 0, len = searchPaths.length; i < len; i++) {
2016-01-15 02:07:29 +00:00
packagePath = searchPaths[i];
2016-01-12 02:40:23 +00:00
try {
packagePath = path.join(process.resourcesPath, packagePath);
packageJson = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json')));
break;
2016-01-14 21:21:11 +00:00
} catch (error) {
2016-01-12 02:40:23 +00:00
continue;
}
}
if (packageJson == null) {
process.nextTick(function() {
return process.exit(1);
});
throw new Error("Unable to find a valid app");
}
2016-01-14 18:35:29 +00:00
// Set application's version.
2016-01-12 02:40:23 +00:00
if (packageJson.version != null) {
app.setVersion(packageJson.version);
}
2016-01-14 18:35:29 +00:00
// Set application's name.
2016-01-12 02:40:23 +00:00
if (packageJson.productName != null) {
app.setName(packageJson.productName);
} else if (packageJson.name != null) {
app.setName(packageJson.name);
}
2016-01-14 18:35:29 +00:00
// Set application's desktop name.
2016-01-12 02:40:23 +00:00
if (packageJson.desktopName != null) {
app.setDesktopName(packageJson.desktopName);
} else {
app.setDesktopName((app.getName()) + ".desktop");
}
2016-01-14 18:35:29 +00:00
// Chrome 42 disables NPAPI plugins by default, reenable them here
2016-01-12 02:40:23 +00:00
app.commandLine.appendSwitch('enable-npapi');
2016-01-14 18:35:29 +00:00
// Set the user path according to application's name.
2016-01-12 02:40:23 +00:00
app.setPath('userData', path.join(app.getPath('appData'), app.getName()));
app.setPath('userCache', path.join(app.getPath('cache'), app.getName()));
app.setAppPath(packagePath);
2016-01-14 18:35:29 +00:00
// Load the chrome extension support.
2016-01-12 02:40:23 +00:00
require('./chrome-extension');
2016-01-14 18:35:29 +00:00
// Load internal desktop-capturer module.
2016-01-12 02:40:23 +00:00
require('./desktop-capturer');
2016-01-14 18:35:29 +00:00
// Set main startup script of the app.
2016-01-14 21:21:11 +00:00
var mainStartupScript = packageJson.main || 'index.js';
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Finally load app's main.js and transfer control to C++.
2016-01-12 02:40:23 +00:00
Module._load(path.join(packagePath, mainStartupScript), Module, true);