electron/default_app/main.js

357 lines
7.8 KiB
JavaScript
Raw Normal View History

const {app, dialog, shell, Menu} = require('electron')
const fs = require('fs')
2016-05-24 16:33:55 +00:00
const Module = require('module')
const path = require('path')
const url = require('url')
// Parse command line options.
const argv = process.argv.slice(1)
2017-09-28 03:51:53 +00:00
const option = {
file: null,
help: null,
default: null,
version: null,
webdriver: null,
modules: []
}
for (let i = 0; i < argv.length; i++) {
2016-03-27 17:38:32 +00:00
if (argv[i] === '--version' || argv[i] === '-v') {
option.version = true
break
} else if (argv[i].match(/^--app=/)) {
option.file = argv[i].split('=')[1]
break
} else if (argv[i] === '--default' || argv[i] === '-d') {
option.default = true
break
2017-09-28 03:51:53 +00:00
} else if (argv[i] === '--interactive' || argv[i] === '-i' ||
argv[i] === '-repl' || argv[i] === '-r') {
option.interactive = true
2016-03-27 17:38:32 +00:00
} else if (argv[i] === '--test-type=webdriver') {
option.webdriver = true
2016-03-27 17:38:32 +00:00
} else if (argv[i] === '--require' || argv[i] === '-r') {
option.modules.push(argv[++i])
continue
2016-03-27 17:38:32 +00:00
} else if (argv[i][0] === '-') {
continue
} else {
option.file = argv[i]
break
}
}
// Quit when all windows are closed and no other one is listening to this.
app.on('window-all-closed', () => {
2016-03-27 17:38:32 +00:00
if (app.listeners('window-all-closed').length === 1 && !option.interactive) {
app.quit()
2016-03-27 17:38:32 +00:00
}
})
// Create default menu.
app.once('ready', () => {
2016-03-27 17:38:32 +00:00
if (Menu.getApplicationMenu()) return
2017-09-28 04:10:52 +00:00
const template = [
{
2015-09-01 15:34:56 +00:00
label: 'Edit',
2017-09-28 04:10:52 +00:00
submenu: [
{
2015-09-01 15:34:56 +00:00
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
2015-09-01 15:34:56 +00:00
{
role: 'selectall'
2016-03-27 17:38:32 +00:00
}
2015-09-01 15:34:56 +00:00
]
},
{
label: 'View',
2017-09-28 04:10:52 +00:00
submenu: [
{
2016-11-15 18:49:51 +00:00
role: 'reload'
2015-09-01 15:34:56 +00:00
},
2017-02-02 19:18:35 +00:00
{
role: 'forcereload'
},
2015-09-01 15:34:56 +00:00
{
2016-11-15 18:52:03 +00:00
role: 'toggledevtools'
},
{
type: 'separator'
},
{
2016-08-08 17:09:45 +00:00
role: 'resetzoom'
},
{
2016-08-08 17:09:45 +00:00
role: 'zoomin'
},
{
2016-08-08 17:09:45 +00:00
role: 'zoomout'
},
{
type: 'separator'
},
{
role: 'togglefullscreen'
2016-03-28 20:19:41 +00:00
}
2015-09-01 15:34:56 +00:00
]
},
{
role: 'window',
2017-09-28 04:10:52 +00:00
submenu: [
{
2015-09-01 15:34:56 +00:00
role: 'minimize'
},
{
role: 'close'
2016-03-28 20:19:41 +00:00
}
2015-09-01 15:34:56 +00:00
]
},
{
role: 'help',
2017-09-28 04:10:52 +00:00
submenu: [
{
2015-09-01 15:34:56 +00:00
label: 'Learn More',
2017-09-28 04:10:52 +00:00
click () {
shell.openExternal('https://electron.atom.io')
2016-01-19 22:53:59 +00:00
}
2015-09-01 15:34:56 +00:00
},
{
label: 'Documentation',
2017-09-28 04:10:52 +00:00
click () {
shell.openExternal(
2016-03-31 23:50:30 +00:00
`https://github.com/electron/electron/tree/v${process.versions.electron}/docs#readme`
)
}
2015-09-01 15:34:56 +00:00
},
{
label: 'Community Discussions',
2017-09-28 04:10:52 +00:00
click () {
shell.openExternal('https://discuss.atom.io/c/electron')
2016-01-19 22:53:59 +00:00
}
2015-09-01 15:34:56 +00:00
},
{
label: 'Search Issues',
2017-09-28 04:10:52 +00:00
click () {
2016-03-31 23:50:30 +00:00
shell.openExternal('https://github.com/electron/electron/issues')
2016-01-19 22:53:59 +00:00
}
2015-09-01 15:34:56 +00:00
}
]
2016-03-28 20:19:41 +00:00
}
]
2015-09-01 15:34:56 +00:00
2016-03-27 17:38:32 +00:00
if (process.platform === 'darwin') {
2015-09-01 15:34:56 +00:00
template.unshift({
label: 'Electron',
2017-09-28 04:10:52 +00:00
submenu: [
{
2015-09-01 15:34:56 +00:00
role: 'about'
},
{
type: 'separator'
},
{
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
role: 'hide'
},
{
role: 'hideothers'
2015-09-01 15:34:56 +00:00
},
{
role: 'unhide'
2015-09-01 15:34:56 +00:00
},
{
type: 'separator'
},
{
2016-06-21 00:04:03 +00:00
role: 'quit'
2016-03-28 20:19:41 +00:00
}
2015-09-01 15:34:56 +00:00
]
})
2017-09-28 03:51:53 +00:00
template[1].submenu.push({
type: 'separator'
}, {
label: 'Speech',
2017-09-28 04:10:52 +00:00
submenu: [
{
2017-09-28 03:51:53 +00:00
role: 'startspeaking'
},
{
role: 'stopspeaking'
}
]
})
2017-09-28 04:10:52 +00:00
template[3].submenu = [
{
role: 'close'
},
{
role: 'minimize'
},
{
role: 'zoom'
},
{
2015-09-01 15:34:56 +00:00
type: 'separator'
},
{
2015-09-01 15:34:56 +00:00
role: 'front'
}
]
2016-07-07 16:15:36 +00:00
} else {
2016-06-21 00:08:45 +00:00
template.unshift({
label: 'File',
2017-09-28 03:51:53 +00:00
submenu: [{
role: 'quit'
}]
2016-06-21 00:08:45 +00:00
})
}
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
})
if (option.modules.length > 0) {
2016-05-24 16:33:55 +00:00
Module._preloadModules(option.modules)
}
2017-09-28 04:10:52 +00:00
function loadApplicationPackage (packagePath) {
// Add a flag indicating app is started from default app.
process.defaultApp = true
try {
// Override app name and version.
packagePath = path.resolve(packagePath)
const packageJsonPath = path.join(packagePath, 'package.json')
if (fs.existsSync(packageJsonPath)) {
let packageJson
try {
2016-07-06 21:19:47 +00:00
packageJson = require(packageJsonPath)
} catch (e) {
2016-05-24 16:38:04 +00:00
showErrorMessage(`Unable to parse ${packageJsonPath}\n\n${e.message}`)
return
}
2016-03-28 20:19:41 +00:00
2016-05-24 16:42:31 +00:00
if (packageJson.version) {
app.setVersion(packageJson.version)
}
2016-03-28 20:19:41 +00:00
if (packageJson.productName) {
app.setName(packageJson.productName)
2016-03-28 20:19:41 +00:00
} else if (packageJson.name) {
app.setName(packageJson.name)
2016-03-28 20:19:41 +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-05-24 16:33:55 +00:00
try {
Module._resolveFilename(packagePath, module, true)
} catch (e) {
showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
2016-05-24 16:41:31 +00:00
return
}
2016-05-24 16:41:31 +00:00
// Run the app.
Module._load(packagePath, module, true)
2016-03-29 01:00:30 +00:00
} catch (e) {
console.error('App threw an error during load')
2016-05-24 16:51:09 +00:00
console.error(e.stack || e)
throw e
}
}
2017-09-28 04:10:52 +00:00
function showErrorMessage (message) {
app.focus()
2016-05-24 16:37:52 +00:00
dialog.showErrorBox('Error launching app', message)
process.exit(1)
}
2017-09-28 04:10:52 +00:00
function loadApplicationByUrl (appUrl) {
require('./default_app').load(appUrl)
}
2017-09-28 04:10:52 +00:00
function startRepl () {
2016-06-13 16:46:33 +00:00
if (process.platform === 'win32') {
console.error('Electron REPL not currently supported on Windows')
process.exit(1)
return
}
const repl = require('repl')
repl.start('> ').on('exit', () => {
process.exit(0)
})
2016-03-22 20:34:46 +00:00
}
// Start the specified app if there is one specified in command line, otherwise
// start the default app.
if (option.file && !option.webdriver) {
const file = option.file
const protocol = url.parse(file).protocol
const extension = path.extname(file)
2016-02-04 18:34:36 +00:00
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) {
console.log('v' + process.versions.electron)
process.exit(0)
} else if (option.default) {
const indexPath = path.join(__dirname, '/index.html')
2017-09-27 18:50:46 +00:00
loadApplicationByUrl(`file://${indexPath}`)
} else if (option.interactive) {
startRepl()
} else {
const welcomeMessage = `Electron ${process.versions.electron} - Build cross platform desktop apps with JavaScript, HTML, and CSS
Usage: electron [options] [path]
A path to an Electron app may be specified. It must be one of the following:
- index.js file.
- Folder containing a package.json file.
- Folder containing an index.js file.
- .html/.htm file.
- http://, https://, or file:// URL.
Options:
-d, --default Run the default bundled Electron app.
-i, --interactive Open a REPL to the main process.
-r, --require Module to preload (option can be repeated)
-v, --version Print the version.`
console.log(welcomeMessage)
process.exit(0)
2017-09-28 04:10:52 +00:00
}