2015-11-12 10:28:04 +00:00
|
|
|
const electron = require('electron');
|
|
|
|
const app = electron.app;
|
|
|
|
const dialog = electron.dialog;
|
|
|
|
const shell = electron.shell;
|
|
|
|
const Menu = electron.Menu;
|
|
|
|
|
2016-02-04 17:16:40 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2016-03-22 20:22:39 +00:00
|
|
|
const repl = require('repl');
|
2016-02-04 18:26:11 +00:00
|
|
|
const url = require('url');
|
2013-04-17 12:05:43 +00:00
|
|
|
|
2014-08-25 11:11:26 +00:00
|
|
|
// Parse command line options.
|
|
|
|
var argv = process.argv.slice(1);
|
2015-09-26 15:23:34 +00:00
|
|
|
var option = { file: null, help: null, version: null, webdriver: null, modules: [] };
|
|
|
|
for (var i = 0; i < argv.length; i++) {
|
2014-08-25 11:11:26 +00:00
|
|
|
if (argv[i] == '--version' || argv[i] == '-v') {
|
|
|
|
option.version = true;
|
|
|
|
break;
|
2015-10-07 21:14:49 +00:00
|
|
|
} else if (argv[i].match(/^--app=/)) {
|
|
|
|
option.file = argv[i].split('=')[1];
|
|
|
|
break;
|
2015-04-24 21:02:49 +00:00
|
|
|
} else if (argv[i] == '--help' || argv[i] == '-h') {
|
|
|
|
option.help = true;
|
|
|
|
break;
|
2016-03-22 20:22:39 +00:00
|
|
|
} else if (argv[i] == '--interactive' || argv[i] == '-i') {
|
|
|
|
option.interactive = true;
|
2014-09-12 14:54:00 +00:00
|
|
|
} else if (argv[i] == '--test-type=webdriver') {
|
|
|
|
option.webdriver = true;
|
2015-09-26 15:23:34 +00:00
|
|
|
} else if (argv[i] == '--require' || argv[i] == '-r') {
|
|
|
|
option.modules.push(argv[++i]);
|
|
|
|
continue;
|
2014-08-25 11:11:26 +00:00
|
|
|
} else if (argv[i][0] == '-') {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
option.file = argv[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-03 03:36:17 +00:00
|
|
|
|
2016-03-22 20:37:57 +00:00
|
|
|
// Quit when all windows are closed and no other one is listening to this.
|
|
|
|
app.on('window-all-closed', function() {
|
|
|
|
if (app.listeners('window-all-closed').length == 1 && !option.interactive)
|
|
|
|
app.quit();
|
|
|
|
});
|
|
|
|
|
2015-06-04 06:54:53 +00:00
|
|
|
// Create default menu.
|
|
|
|
app.once('ready', function() {
|
2015-06-29 05:11:53 +00:00
|
|
|
if (Menu.getApplicationMenu())
|
|
|
|
return;
|
|
|
|
|
2015-09-01 15:34:56 +00:00
|
|
|
var template = [
|
|
|
|
{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Undo',
|
|
|
|
accelerator: 'CmdOrCtrl+Z',
|
|
|
|
role: 'undo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Redo',
|
|
|
|
accelerator: 'Shift+CmdOrCtrl+Z',
|
|
|
|
role: 'redo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Cut',
|
|
|
|
accelerator: 'CmdOrCtrl+X',
|
|
|
|
role: 'cut'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Copy',
|
|
|
|
accelerator: 'CmdOrCtrl+C',
|
|
|
|
role: 'copy'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Paste',
|
|
|
|
accelerator: 'CmdOrCtrl+V',
|
|
|
|
role: 'paste'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Select All',
|
|
|
|
accelerator: 'CmdOrCtrl+A',
|
|
|
|
role: 'selectall'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Reload',
|
|
|
|
accelerator: 'CmdOrCtrl+R',
|
|
|
|
click: function(item, focusedWindow) {
|
|
|
|
if (focusedWindow)
|
|
|
|
focusedWindow.reload();
|
2015-06-04 06:54:53 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Toggle Full Screen',
|
|
|
|
accelerator: (function() {
|
|
|
|
if (process.platform == 'darwin')
|
|
|
|
return 'Ctrl+Command+F';
|
|
|
|
else
|
|
|
|
return 'F11';
|
|
|
|
})(),
|
|
|
|
click: function(item, focusedWindow) {
|
|
|
|
if (focusedWindow)
|
|
|
|
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Toggle Developer Tools',
|
|
|
|
accelerator: (function() {
|
|
|
|
if (process.platform == 'darwin')
|
|
|
|
return 'Alt+Command+I';
|
|
|
|
else
|
|
|
|
return 'Ctrl+Shift+I';
|
|
|
|
})(),
|
|
|
|
click: function(item, focusedWindow) {
|
|
|
|
if (focusedWindow)
|
|
|
|
focusedWindow.toggleDevTools();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Window',
|
|
|
|
role: 'window',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Minimize',
|
|
|
|
accelerator: 'CmdOrCtrl+M',
|
|
|
|
role: 'minimize'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Close',
|
|
|
|
accelerator: 'CmdOrCtrl+W',
|
|
|
|
role: 'close'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Help',
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Learn More',
|
2016-01-19 22:53:59 +00:00
|
|
|
click: function() {
|
|
|
|
shell.openExternal('http://electron.atom.io');
|
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Documentation',
|
2015-11-21 00:26:59 +00:00
|
|
|
click: function() {
|
|
|
|
shell.openExternal(
|
|
|
|
`https://github.com/atom/electron/tree/v${process.versions.electron}/docs#readme`
|
2016-01-19 22:53:59 +00:00
|
|
|
);
|
2015-11-21 00:26:59 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Community Discussions',
|
2016-01-19 22:53:59 +00:00
|
|
|
click: function() {
|
|
|
|
shell.openExternal('https://discuss.atom.io/c/electron');
|
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Search Issues',
|
2016-01-19 22:53:59 +00:00
|
|
|
click: function() {
|
|
|
|
shell.openExternal('https://github.com/atom/electron/issues');
|
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
if (process.platform == 'darwin') {
|
|
|
|
template.unshift({
|
|
|
|
label: 'Electron',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'About Electron',
|
|
|
|
role: 'about'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Services',
|
|
|
|
role: 'services',
|
|
|
|
submenu: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Hide Electron',
|
|
|
|
accelerator: 'Command+H',
|
|
|
|
role: 'hide'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Hide Others',
|
2016-01-21 22:48:06 +00:00
|
|
|
accelerator: 'Command+Alt+H',
|
2015-11-22 17:08:35 +00:00
|
|
|
role: 'hideothers'
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Show All',
|
2015-11-22 17:08:35 +00:00
|
|
|
role: 'unhide'
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Quit',
|
|
|
|
accelerator: 'Command+Q',
|
|
|
|
click: function() { app.quit(); }
|
|
|
|
},
|
|
|
|
]
|
|
|
|
});
|
|
|
|
template[3].submenu.push(
|
2015-06-04 06:54:53 +00:00
|
|
|
{
|
2015-09-01 15:34:56 +00:00
|
|
|
type: 'separator'
|
2015-06-04 06:54:53 +00:00
|
|
|
},
|
|
|
|
{
|
2015-09-01 15:34:56 +00:00
|
|
|
label: 'Bring All to Front',
|
|
|
|
role: 'front'
|
2015-06-04 06:54:53 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
);
|
2015-06-04 06:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var menu = Menu.buildFromTemplate(template);
|
|
|
|
Menu.setApplicationMenu(menu);
|
|
|
|
});
|
|
|
|
|
2015-09-26 15:23:34 +00:00
|
|
|
if (option.modules.length > 0) {
|
|
|
|
require('module')._preloadModules(option.modules);
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:35:17 +00:00
|
|
|
function loadApplicationPackage(packagePath) {
|
2013-07-24 07:20:59 +00:00
|
|
|
try {
|
2014-05-24 03:18:06 +00:00
|
|
|
// Override app name and version.
|
2016-02-04 18:07:19 +00:00
|
|
|
packagePath = path.resolve(packagePath);
|
2014-05-24 03:18:06 +00:00
|
|
|
var packageJsonPath = path.join(packagePath, 'package.json');
|
|
|
|
if (fs.existsSync(packageJsonPath)) {
|
|
|
|
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
|
|
|
|
if (packageJson.version)
|
|
|
|
app.setVersion(packageJson.version);
|
|
|
|
if (packageJson.productName)
|
|
|
|
app.setName(packageJson.productName);
|
|
|
|
else if (packageJson.name)
|
|
|
|
app.setName(packageJson.name);
|
2015-01-19 01:58:17 +00:00
|
|
|
app.setPath('userData', path.join(app.getPath('appData'), app.getName()));
|
2015-01-19 05:09:42 +00:00
|
|
|
app.setPath('userCache', path.join(app.getPath('cache'), app.getName()));
|
2015-07-06 09:35:35 +00:00
|
|
|
app.setAppPath(packagePath);
|
2014-05-24 03:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the app.
|
2014-06-22 06:57:11 +00:00
|
|
|
require('module')._load(packagePath, module, true);
|
2013-07-24 07:20:59 +00:00
|
|
|
} catch(e) {
|
2014-05-14 08:27:40 +00:00
|
|
|
if (e.code == 'MODULE_NOT_FOUND') {
|
|
|
|
app.focus();
|
2015-11-21 00:26:59 +00:00
|
|
|
dialog.showErrorBox(
|
|
|
|
'Error opening app',
|
|
|
|
'The app provided is not a valid Electron app, please read the docs on how to write one:\n' +
|
|
|
|
`https://github.com/atom/electron/tree/v${process.versions.electron}/docs\n\n${e.toString()}`
|
|
|
|
);
|
2014-05-14 08:27:40 +00:00
|
|
|
process.exit(1);
|
|
|
|
} else {
|
2015-03-02 20:08:12 +00:00
|
|
|
console.error('App threw an error when running', e);
|
2014-05-14 08:27:40 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2013-07-24 07:20:59 +00:00
|
|
|
}
|
2016-02-04 18:07:19 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 18:26:11 +00:00
|
|
|
function loadApplicationByUrl(appUrl) {
|
|
|
|
require('./default_app').load(appUrl);
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:34:46 +00:00
|
|
|
function startRepl() {
|
|
|
|
repl.start('> ').on('exit', function() {
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:07:19 +00:00
|
|
|
// Start the specified app if there is one specified in command line, otherwise
|
|
|
|
// start the default app.
|
|
|
|
if (option.file && !option.webdriver) {
|
2016-02-04 18:33:22 +00:00
|
|
|
var file = option.file;
|
|
|
|
var protocol = url.parse(file).protocol;
|
|
|
|
var extension = path.extname(file);
|
2016-02-04 18:34:36 +00:00
|
|
|
if (protocol === 'http:' || protocol === 'https:' || protocol === 'file:') {
|
2016-02-04 18:33:22 +00:00
|
|
|
loadApplicationByUrl(file);
|
|
|
|
} else if (extension === '.html' || extension === '.htm') {
|
|
|
|
loadApplicationByUrl('file://' + path.resolve(file));
|
2016-02-04 18:26:11 +00:00
|
|
|
} else {
|
2016-02-04 18:35:17 +00:00
|
|
|
loadApplicationPackage(file);
|
2016-02-04 18:26:11 +00:00
|
|
|
}
|
2014-08-25 11:11:26 +00:00
|
|
|
} else if (option.version) {
|
2015-04-24 21:02:49 +00:00
|
|
|
console.log('v' + process.versions.electron);
|
|
|
|
process.exit(0);
|
|
|
|
} else if (option.help) {
|
|
|
|
var helpMessage = "Electron v" + process.versions.electron + " - Cross Platform Desktop Application Shell\n\n";
|
|
|
|
helpMessage += "Usage: electron [options] [path]\n\n";
|
2016-02-04 18:45:52 +00:00
|
|
|
helpMessage += "A path to an Electron application may be specified.\n";
|
|
|
|
helpMessage += "The path must be one of the following:\n\n";
|
|
|
|
helpMessage += " - index.js file.\n";
|
|
|
|
helpMessage += " - Folder containing a package.json file.\n";
|
|
|
|
helpMessage += " - Folder containing an index.js file.\n";
|
|
|
|
helpMessage += " - .html/.htm file.\n";
|
|
|
|
helpMessage += " - http://, https://, or file:// URL.\n";
|
|
|
|
helpMessage += "\nOptions:\n";
|
2015-04-24 21:02:49 +00:00
|
|
|
helpMessage += " -h, --help Print this usage message.\n";
|
2016-03-22 20:24:41 +00:00
|
|
|
helpMessage += " -i, --interactive Open a REPL to the main process.\n";
|
|
|
|
helpMessage += " -r, --require Module to preload (option can be repeated)\n";
|
2015-04-24 21:02:49 +00:00
|
|
|
helpMessage += " -v, --version Print the version.";
|
|
|
|
console.log(helpMessage);
|
2013-10-03 03:36:17 +00:00
|
|
|
process.exit(0);
|
2016-03-22 20:22:39 +00:00
|
|
|
} else if (option.interactive) {
|
2016-03-22 20:34:46 +00:00
|
|
|
startRepl();
|
2013-07-17 08:21:33 +00:00
|
|
|
} else {
|
2016-02-04 18:26:11 +00:00
|
|
|
loadApplicationByUrl('file://' + __dirname + '/index.html');
|
2013-04-15 07:39:54 +00:00
|
|
|
}
|