2019-02-06 18:27:20 +00:00
|
|
|
import { app, dialog } from 'electron'
|
2019-01-10 19:54:34 +00:00
|
|
|
|
2019-02-06 18:27:20 +00:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as path from 'path'
|
|
|
|
import * as url from 'url'
|
|
|
|
|
|
|
|
type DefaultAppOptions = {
|
|
|
|
file: null | string;
|
|
|
|
noHelp: boolean;
|
|
|
|
version: boolean;
|
|
|
|
webdriver: boolean;
|
|
|
|
interactive: boolean;
|
|
|
|
abi: boolean;
|
|
|
|
modules: string[];
|
|
|
|
}
|
2015-11-12 10:28:04 +00:00
|
|
|
|
2016-05-24 16:33:55 +00:00
|
|
|
const Module = require('module')
|
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)
|
2017-09-28 03:51:53 +00:00
|
|
|
|
2019-02-06 18:27:20 +00:00
|
|
|
const option: DefaultAppOptions = {
|
2017-09-28 03:51:53 +00:00
|
|
|
file: null,
|
2018-09-21 05:24:42 +00:00
|
|
|
noHelp: Boolean(process.env.ELECTRON_NO_HELP),
|
2019-02-06 18:27:20 +00:00
|
|
|
version: false,
|
|
|
|
webdriver: false,
|
|
|
|
interactive: false,
|
|
|
|
abi: false,
|
2017-09-28 03:51:53 +00:00
|
|
|
modules: []
|
|
|
|
}
|
|
|
|
|
2018-09-21 05:24:42 +00:00
|
|
|
let nextArgIsRequire = false
|
|
|
|
|
|
|
|
for (const arg of argv) {
|
|
|
|
if (nextArgIsRequire) {
|
|
|
|
option.modules.push(arg)
|
|
|
|
nextArgIsRequire = false
|
|
|
|
continue
|
|
|
|
} else if (arg === '--version' || arg === '-v') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.version = true
|
|
|
|
break
|
2018-09-21 05:24:42 +00:00
|
|
|
} else if (arg.match(/^--app=/)) {
|
|
|
|
option.file = arg.split('=')[1]
|
2016-03-24 20:15:04 +00:00
|
|
|
break
|
2018-09-21 05:24:42 +00:00
|
|
|
} else if (arg === '--interactive' || arg === '-i' || arg === '-repl') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.interactive = true
|
2018-09-21 05:24:42 +00:00
|
|
|
} else if (arg === '--test-type=webdriver') {
|
2016-03-24 20:15:04 +00:00
|
|
|
option.webdriver = true
|
2018-09-21 05:24:42 +00:00
|
|
|
} else if (arg === '--require' || arg === '-r') {
|
|
|
|
nextArgIsRequire = true
|
2016-03-24 20:15:04 +00:00
|
|
|
continue
|
2018-09-21 05:24:42 +00:00
|
|
|
} else if (arg === '--abi' || arg === '-a') {
|
2017-11-10 22:58:21 +00:00
|
|
|
option.abi = true
|
|
|
|
continue
|
2018-09-21 05:24:42 +00:00
|
|
|
} else if (arg === '--no-help') {
|
|
|
|
option.noHelp = true
|
|
|
|
continue
|
|
|
|
} else if (arg[0] === '-') {
|
2016-03-24 20:15:04 +00:00
|
|
|
continue
|
2014-08-25 11:11:26 +00:00
|
|
|
} else {
|
2018-09-21 05:24:42 +00:00
|
|
|
option.file = arg
|
2016-03-24 20:15:04 +00:00
|
|
|
break
|
2014-08-25 11:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-03 03:36:17 +00:00
|
|
|
|
2018-09-21 05:24:42 +00:00
|
|
|
if (nextArgIsRequire) {
|
|
|
|
console.error('Invalid Usage: --require [file]\n\n"file" is required')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up preload modules
|
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
|
|
|
}
|
|
|
|
|
2019-02-06 18:27:20 +00:00
|
|
|
function loadApplicationPackage (packagePath: string) {
|
2016-05-06 12:48:51 +00:00
|
|
|
// Add a flag indicating app is started from default app.
|
2018-09-21 05:24:42 +00:00
|
|
|
Object.defineProperty(process, 'defaultApp', {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
value: true
|
|
|
|
})
|
2016-05-06 12:48:51 +00:00
|
|
|
|
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 {
|
2016-07-06 21:19:47 +00:00
|
|
|
packageJson = require(packageJsonPath)
|
2016-05-23 19:00:55 +00:00
|
|
|
} 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) {
|
2019-04-30 20:55:33 +00:00
|
|
|
app.name = packageJson.productName
|
2016-03-28 20:19:41 +00:00
|
|
|
} else if (packageJson.name) {
|
2019-04-30 20:55:33 +00:00
|
|
|
app.name = packageJson.name
|
2016-03-28 20:19:41 +00:00
|
|
|
}
|
2019-04-16 18:22:51 +00:00
|
|
|
app._setDefaultAppPaths(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
|
|
|
}
|
|
|
|
|
2019-02-06 18:27:20 +00:00
|
|
|
function showErrorMessage (message: string) {
|
2016-05-23 19:00:55 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-03-11 23:13:46 +00:00
|
|
|
async function loadApplicationByURL (appUrl: string) {
|
|
|
|
const { loadURL } = await import('./default_app')
|
|
|
|
loadURL(appUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadApplicationByFile (appPath: string) {
|
|
|
|
const { loadFile } = await import('./default_app')
|
|
|
|
loadFile(appPath)
|
2016-02-04 18:26:11 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:35:53 +00:00
|
|
|
// prevent quitting
|
|
|
|
app.on('window-all-closed', () => {})
|
|
|
|
|
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)
|
2018-07-17 07:23:44 +00:00
|
|
|
if (protocol === 'http:' || protocol === 'https:' || protocol === 'file:' || protocol === 'chrome:') {
|
2019-03-11 23:13:46 +00:00
|
|
|
loadApplicationByURL(file)
|
2016-02-04 18:33:22 +00:00
|
|
|
} else if (extension === '.html' || extension === '.htm') {
|
2019-03-11 23:13:46 +00:00
|
|
|
loadApplicationByFile(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)
|
2017-11-10 22:58:21 +00:00
|
|
|
} else if (option.abi) {
|
|
|
|
console.log(process.versions.modules)
|
|
|
|
process.exit(0)
|
2017-09-27 18:23:42 +00:00
|
|
|
} else if (option.interactive) {
|
|
|
|
startRepl()
|
|
|
|
} else {
|
2018-09-21 05:24:42 +00:00
|
|
|
if (!option.noHelp) {
|
|
|
|
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:
|
|
|
|
-i, --interactive Open a REPL to the main process.
|
|
|
|
-r, --require Module to preload (option can be repeated).
|
|
|
|
-v, --version Print the version.
|
|
|
|
-a, --abi Print the Node ABI version.`
|
|
|
|
|
|
|
|
console.log(welcomeMessage)
|
|
|
|
}
|
2017-10-07 03:29:54 +00:00
|
|
|
|
2019-03-11 23:13:46 +00:00
|
|
|
loadApplicationByFile('index.html')
|
2017-09-28 04:10:52 +00:00
|
|
|
}
|