Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow command line arguments #52

Merged
merged 3 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ require('electron-reload')(__dirname, {
# API
`electron_reload(paths, options)`
* `paths`: a file, directory or glob pattern to watch
* `options` (optional): [`chokidar`](https://github.com/paulmillr/chokidar) options plus `electron` property pointing to electron executables. (default: `{ignored: /node_modules|[\/\\]\./}`)
* `options` (optional) containing:

[`chokidar`](https://github.com/paulmillr/chokidar) options

`electron` property pointing to electron executables.

`argv` string array with command line options passed to the executed Electron app. Only used when hard resetting.

`options` will default to `{ignored: /node_modules|[\/\\]\./, argv: []}`.


# Why this module?
Expand Down
13 changes: 7 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {app} = require('electron')
const { app } = require('electron')
const chokidar = require('chokidar')
const fs = require('fs')
const {spawn} = require('child_process')
const { spawn } = require('child_process')
const path = require('path')

// Main file poses a special case, as its changes are
Expand All @@ -18,11 +18,12 @@ const ignoredPaths = [mainFile, /node_modules|[/\\]\./]
* @param {String} hardResetMethod method to restart electron
* @returns {Function} handler to pass to chokidar
*/
const createHardresetHandler = (eXecutable, hardResetMethod) =>
const createHardresetHandler = (eXecutable, hardResetMethod, argv) =>
() => {
// Detaching child is useful when in Windows to let child
// live after the parent is killed
let child = spawn(eXecutable, [appPath], {
let args = (argv || []).concat([appPath])
let child = spawn(eXecutable, args, {
detached: true,
stdio: 'inherit'
})
Expand All @@ -48,7 +49,7 @@ const createHardresetHandler = (eXecutable, hardResetMethod) =>
const createWatcher = (glob, options = {}) => {
// Watch everything but the node_modules folder and main file
// main file changes are only effective if hard reset is possible
let opts = Object.assign({ignored: ignoredPaths}, options)
let opts = Object.assign({ ignored: ignoredPaths }, options)
return chokidar.watch(glob, opts)
}

Expand All @@ -75,7 +76,7 @@ module.exports = (glob, options = {}) => {
// A hard reset is only done when the main file has changed
let eXecutable = options.electron
if (eXecutable && fs.existsSync(eXecutable)) {
chokidar.watch(mainFile).once('change', createHardresetHandler(eXecutable, options.hardResetMethod))
chokidar.watch(mainFile).once('change', createHardresetHandler(eXecutable, options.hardResetMethod, options.argv))
} else {
console.log('Electron could not be found. No hard resets for you!')
}
Expand Down