2016-05-06 23:53:50 +00:00
|
|
|
const {app, dialog, shell, Menu} = require('electron')
|
2015-11-12 10:28:04 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const fs = require('fs')
|
2016-05-24 16:33:55 +00:00
|
|
|
const Module = require('module')
|
2016-03-24 20:15:04 +00:00
|
|
|
const path = require('path')
|
|
|
|
const url = require('url')
|
2013-04-17 12:05:43 +00:00
|
|
|
|
2014-08-25 11:11:26 +00:00
|
|
|
// Parse command line options.
|
2016-05-06 23:53:50 +00:00
|
|
|
const argv = process.argv.slice(1)
|
2016-06-07 12:45:38 +00:00
|
|
|
const option = { file: null, help: null, version: null, abi: null, webdriver: null, modules: [] }
|
2016-05-06 23:53:50 +00:00
|
|
|
for (let i = 0; i < argv.length; i++) {
|
2016-03-27 17:38:32 +00:00
|
|
|
if (argv[i] === '--version' || argv[i] === '-v') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.version = true
|
|
|
|
break
|
2016-06-07 12:45:38 +00:00
|
|
|
} else if (argv[i] === '--abi') {
|
|
|
|
option.abi = true
|
|
|
|
break
|
2015-10-07 21:14:49 +00:00
|
|
|
} else if (argv[i].match(/^--app=/)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.file = argv[i].split('=')[1]
|
|
|
|
break
|
2016-03-27 17:38:32 +00:00
|
|
|
} else if (argv[i] === '--help' || argv[i] === '-h') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.help = true
|
|
|
|
break
|
2016-03-27 17:38:32 +00:00
|
|
|
} else if (argv[i] === '--interactive' || argv[i] === '-i') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.interactive = true
|
2016-03-27 17:38:32 +00:00
|
|
|
} else if (argv[i] === '--test-type=webdriver') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.webdriver = true
|
2016-03-27 17:38:32 +00:00
|
|
|
} else if (argv[i] === '--require' || argv[i] === '-r') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.modules.push(argv[++i])
|
|
|
|
continue
|
2016-03-27 17:38:32 +00:00
|
|
|
} else if (argv[i][0] === '-') {
|
2016-03-24 20:15:04 +00:00
|
|
|
continue
|
2014-08-25 11:11:26 +00:00
|
|
|
} else {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.file = argv[i]
|
|
|
|
break
|
2014-08-25 11:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
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.
|
2016-05-06 23:53:50 +00:00
|
|
|
app.on('window-all-closed', () => {
|
2016-03-27 17:38:32 +00:00
|
|
|
if (app.listeners('window-all-closed').length === 1 && !option.interactive) {
|
2016-03-24 20:15:04 +00:00
|
|
|
app.quit()
|
2016-03-27 17:38:32 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-03-22 20:37:57 +00:00
|
|
|
|
2015-06-04 06:54:53 +00:00
|
|
|
// Create default menu.
|
2016-05-06 23:53:50 +00:00
|
|
|
app.once('ready', () => {
|
2016-03-27 17:38:32 +00:00
|
|
|
if (Menu.getApplicationMenu()) return
|
2015-06-29 05:11:53 +00:00
|
|
|
|
2016-05-06 23:53:50 +00:00
|
|
|
const template = [
|
2015-09-01 15:34:56 +00:00
|
|
|
{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
role: 'undo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'redo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'cut'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'copy'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'paste'
|
|
|
|
},
|
2016-06-07 08:42:46 +00:00
|
|
|
{
|
|
|
|
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',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Reload',
|
|
|
|
accelerator: 'CmdOrCtrl+R',
|
2016-05-06 23:53:50 +00:00
|
|
|
click (item, focusedWindow) {
|
2016-03-27 17:38:32 +00:00
|
|
|
if (focusedWindow) focusedWindow.reload()
|
2015-06-04 06:54:53 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
2016-06-21 16:34:41 +00:00
|
|
|
role: 'togglefullscreen',
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Toggle Developer Tools',
|
2016-06-21 16:38:35 +00:00
|
|
|
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
|
2016-05-06 23:53:50 +00:00
|
|
|
click (item, focusedWindow) {
|
2016-03-28 20:19:41 +00:00
|
|
|
if (focusedWindow) focusedWindow.toggleDevTools()
|
2015-09-01 15:34:56 +00:00
|
|
|
}
|
2016-03-28 20:19:41 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'window',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
role: 'minimize'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'close'
|
2016-03-28 20:19:41 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Learn More',
|
2016-05-06 23:53:50 +00:00
|
|
|
click () {
|
2016-03-24 20:15:04 +00:00
|
|
|
shell.openExternal('http://electron.atom.io')
|
2016-01-19 22:53:59 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Documentation',
|
2016-05-06 23:53:50 +00:00
|
|
|
click () {
|
2015-11-21 00:26:59 +00:00
|
|
|
shell.openExternal(
|
2016-03-31 23:50:30 +00:00
|
|
|
`https://github.com/electron/electron/tree/v${process.versions.electron}/docs#readme`
|
2016-03-24 20:15:04 +00:00
|
|
|
)
|
2015-11-21 00:26:59 +00:00
|
|
|
}
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Community Discussions',
|
2016-05-06 23:53:50 +00:00
|
|
|
click () {
|
2016-03-24 20:15:04 +00:00
|
|
|
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',
|
2016-05-06 23:53:50 +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
|
|
|
}
|
2016-03-24 20:15:04 +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',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
role: 'about'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'services',
|
|
|
|
submenu: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'hide'
|
|
|
|
},
|
|
|
|
{
|
2015-11-22 17:08:35 +00:00
|
|
|
role: 'hideothers'
|
2015-09-01 15:34:56 +00:00
|
|
|
},
|
|
|
|
{
|
2015-11-22 17:08:35 +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
|
|
|
]
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-06-07 08:42:46 +00:00
|
|
|
template[3].submenu = [
|
|
|
|
{
|
|
|
|
role: 'close'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'minimize'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'zoom'
|
|
|
|
},
|
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
|
|
|
role: 'front'
|
2015-06-04 06:54:53 +00:00
|
|
|
}
|
2016-06-07 08:42:46 +00:00
|
|
|
]
|
2015-06-04 06:54:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:08:45 +00:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
template.unshift({
|
|
|
|
label: 'File',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
role: 'quit'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-06 23:53:50 +00:00
|
|
|
const menu = Menu.buildFromTemplate(template)
|
2016-03-24 20:15:04 +00:00
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
})
|
2015-06-04 06:54:53 +00:00
|
|
|
|
2015-09-26 15:23:34 +00:00
|
|
|
if (option.modules.length > 0) {
|
2016-05-24 16:33:55 +00:00
|
|
|
Module._preloadModules(option.modules)
|
2015-09-26 15:23:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
function loadApplicationPackage (packagePath) {
|
2016-05-06 12:48:51 +00:00
|
|
|
// Add a flag indicating app is started from default app.
|
|
|
|
process.defaultApp = true
|
|
|
|
|
2013-07-24 07:20:59 +00:00
|
|
|
try {
|
2014-05-24 03:18:06 +00:00
|
|
|
// Override app name and version.
|
2016-03-24 20:15:04 +00:00
|
|
|
packagePath = path.resolve(packagePath)
|
2016-05-06 23:53:50 +00:00
|
|
|
const packageJsonPath = path.join(packagePath, 'package.json')
|
2014-05-24 03:18:06 +00:00
|
|
|
if (fs.existsSync(packageJsonPath)) {
|
2016-05-23 19:00:55 +00:00
|
|
|
let packageJson
|
|
|
|
try {
|
|
|
|
packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
|
|
|
|
} catch (e) {
|
2016-05-24 16:38:04 +00:00
|
|
|
showErrorMessage(`Unable to parse ${packageJsonPath}\n\n${e.message}`)
|
|
|
|
return
|
2016-05-23 19:00:55 +00:00
|
|
|
}
|
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) {
|
2016-03-24 20:15:04 +00:00
|
|
|
app.setName(packageJson.productName)
|
2016-03-28 20:19:41 +00:00
|
|
|
} else if (packageJson.name) {
|
2016-03-24 20:15:04 +00:00
|
|
|
app.setName(packageJson.name)
|
2016-03-28 20:19:41 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +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)
|
2014-05-24 03:18:06 +00:00
|
|
|
}
|
2016-05-24 16:33:55 +00:00
|
|
|
|
2016-05-23 19:00:55 +00:00
|
|
|
try {
|
|
|
|
Module._resolveFilename(packagePath, module, true)
|
|
|
|
} catch (e) {
|
2016-05-24 16:56:36 +00:00
|
|
|
showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
|
2016-05-24 16:41:31 +00:00
|
|
|
return
|
2016-05-23 19:00:55 +00:00
|
|
|
}
|
2016-05-24 16:41:31 +00:00
|
|
|
|
2014-05-24 03:18:06 +00:00
|
|
|
// Run the app.
|
2016-05-23 19:00:55 +00:00
|
|
|
Module._load(packagePath, module, true)
|
2016-03-29 01:00:30 +00:00
|
|
|
} catch (e) {
|
2016-05-24 16:45:32 +00:00
|
|
|
console.error('App threw an error during load')
|
2016-05-24 16:51:09 +00:00
|
|
|
console.error(e.stack || e)
|
2016-05-24 16:45:32 +00:00
|
|
|
throw e
|
2013-07-24 07:20:59 +00:00
|
|
|
}
|
2016-02-04 18:07:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:00:55 +00:00
|
|
|
function showErrorMessage (message) {
|
|
|
|
app.focus()
|
2016-05-24 16:37:52 +00:00
|
|
|
dialog.showErrorBox('Error launching app', message)
|
2016-05-23 19:00:55 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
function loadApplicationByUrl (appUrl) {
|
|
|
|
require('./default_app').load(appUrl)
|
2016-02-04 18:26:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +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
|
|
|
|
}
|
|
|
|
|
2016-06-19 07:34:48 +00:00
|
|
|
const repl = require('repl')
|
2016-05-06 23:53:50 +00:00
|
|
|
repl.start('> ').on('exit', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
process.exit(0)
|
|
|
|
})
|
2016-03-22 20:34:46 +00:00
|
|
|
}
|
|
|
|
|
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-05-06 23:53:50 +00:00
|
|
|
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:') {
|
2016-03-24 20:15:04 +00:00
|
|
|
loadApplicationByUrl(file)
|
2016-02-04 18:33:22 +00:00
|
|
|
} else if (extension === '.html' || extension === '.htm') {
|
2016-03-24 20:15:04 +00:00
|
|
|
loadApplicationByUrl('file://' + path.resolve(file))
|
2016-02-04 18:26:11 +00:00
|
|
|
} else {
|
2016-03-24 20:15:04 +00:00
|
|
|
loadApplicationPackage(file)
|
2016-02-04 18:26:11 +00:00
|
|
|
}
|
2014-08-25 11:11:26 +00:00
|
|
|
} else if (option.version) {
|
2016-03-24 20:15:04 +00:00
|
|
|
console.log('v' + process.versions.electron)
|
|
|
|
process.exit(0)
|
2016-06-07 12:45:38 +00:00
|
|
|
} else if (option.abi) {
|
|
|
|
console.log(process.versions.modules)
|
|
|
|
process.exit(0)
|
2015-04-24 21:02:49 +00:00
|
|
|
} else if (option.help) {
|
2016-05-24 16:43:25 +00:00
|
|
|
const helpMessage = `Electron ${process.versions.electron} - Build cross platform desktop apps with JavaScript, HTML, and CSS
|
2016-03-24 20:15:04 +00:00
|
|
|
|
|
|
|
Usage: electron [options] [path]
|
|
|
|
|
2016-05-24 16:53:23 +00:00
|
|
|
A path to an Electron app may be specified. The path must be one of the following:
|
2016-03-24 20:15:04 +00:00
|
|
|
|
|
|
|
- index.js file.
|
|
|
|
- Folder containing a package.json file.
|
|
|
|
- Folder containing an index.js file.
|
|
|
|
- .html/.htm file.
|
|
|
|
- http://, https://, or file:// URL.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help Print this usage message.
|
|
|
|
-i, --interactive Open a REPL to the main process.
|
|
|
|
-r, --require Module to preload (option can be repeated)
|
2016-06-07 12:45:38 +00:00
|
|
|
-v, --version Print the version.
|
|
|
|
--abi Print the application binary interface.`
|
2016-03-24 20:15:04 +00:00
|
|
|
console.log(helpMessage)
|
|
|
|
process.exit(0)
|
2016-03-22 20:22:39 +00:00
|
|
|
} else if (option.interactive) {
|
2016-03-24 20:15:04 +00:00
|
|
|
startRepl()
|
2013-07-17 08:21:33 +00:00
|
|
|
} else {
|
2016-05-06 23:53:50 +00:00
|
|
|
const indexPath = path.join(__dirname, '/index.html')
|
2016-03-29 01:00:30 +00:00
|
|
|
loadApplicationByUrl(`file://${indexPath}`)
|
2013-04-15 07:39:54 +00:00
|
|
|
}
|