Merge pull request #4364 from atom/launch-url-or-file-directly-from-cli

Launch URL or HTML file directly
This commit is contained in:
Cheng Zhao 2016-02-16 16:13:10 +08:00
commit 44260634af
2 changed files with 44 additions and 19 deletions

View file

@ -9,13 +9,15 @@ app.on('window-all-closed', function() {
app.quit(); app.quit();
}); });
app.on('ready', function() { exports.load = function(appUrl) {
mainWindow = new BrowserWindow({ app.on('ready', function() {
width: 800, mainWindow = new BrowserWindow({
height: 600, width: 800,
autoHideMenuBar: true, height: 600,
useContentSize: true, autoHideMenuBar: true,
useContentSize: true,
});
mainWindow.loadURL(appUrl);
mainWindow.focus();
}); });
mainWindow.loadURL('file://' + __dirname + '/index.html'); };
mainWindow.focus();
});

View file

@ -4,8 +4,9 @@ const dialog = electron.dialog;
const shell = electron.shell; const shell = electron.shell;
const Menu = electron.Menu; const Menu = electron.Menu;
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
const url = require('url');
// Quit when all windows are closed and no other one is listening to this. // Quit when all windows are closed and no other one is listening to this.
app.on('window-all-closed', function() { app.on('window-all-closed', function() {
@ -234,12 +235,10 @@ if (option.modules.length > 0) {
require('module')._preloadModules(option.modules); require('module')._preloadModules(option.modules);
} }
// Start the specified app if there is one specified in command line, otherwise function loadApplicationPackage(packagePath) {
// start the default app.
if (option.file && !option.webdriver) {
try { try {
// Override app name and version. // Override app name and version.
var packagePath = path.resolve(option.file); packagePath = path.resolve(packagePath);
var packageJsonPath = path.join(packagePath, 'package.json'); var packageJsonPath = path.join(packagePath, 'package.json');
if (fs.existsSync(packageJsonPath)) { if (fs.existsSync(packageJsonPath)) {
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
@ -270,20 +269,44 @@ if (option.file && !option.webdriver) {
throw e; throw e;
} }
} }
}
function loadApplicationByUrl(appUrl) {
require('./default_app').load(appUrl);
}
// Start the specified app if there is one specified in command line, otherwise
// start the default app.
if (option.file && !option.webdriver) {
var file = option.file;
var protocol = url.parse(file).protocol;
var extension = path.extname(file);
if (protocol === 'http:' || protocol === 'https:' || protocol === 'file:') {
loadApplicationByUrl(file);
} else if (extension === '.html' || extension === '.htm') {
loadApplicationByUrl('file://' + path.resolve(file));
} else {
loadApplicationPackage(file);
}
} else if (option.version) { } else if (option.version) {
console.log('v' + process.versions.electron); console.log('v' + process.versions.electron);
process.exit(0); process.exit(0);
} else if (option.help) { } else if (option.help) {
var helpMessage = "Electron v" + process.versions.electron + " - Cross Platform Desktop Application Shell\n\n"; var helpMessage = "Electron v" + process.versions.electron + " - Cross Platform Desktop Application Shell\n\n";
helpMessage += "Usage: electron [options] [path]\n\n"; helpMessage += "Usage: electron [options] [path]\n\n";
helpMessage += "A path to an Electron application may be specified. The path must be to \n"; helpMessage += "A path to an Electron application may be specified.\n";
helpMessage += "an index.js file or to a folder containing a package.json or index.js file.\n\n"; helpMessage += "The path must be one of the following:\n\n";
helpMessage += "Options:\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";
helpMessage += " -r, --require Module to preload (option can be repeated)\n"; helpMessage += " -r, --require Module to preload (option can be repeated)\n";
helpMessage += " -h, --help Print this usage message.\n"; helpMessage += " -h, --help Print this usage message.\n";
helpMessage += " -v, --version Print the version."; helpMessage += " -v, --version Print the version.";
console.log(helpMessage); console.log(helpMessage);
process.exit(0); process.exit(0);
} else { } else {
require('./default_app'); loadApplicationByUrl('file://' + __dirname + '/index.html');
} }