Skip to content

Commit

Permalink
Merge pull request #52 from Mathyn/feature/command-line-args
Browse files Browse the repository at this point in the history
Allow command line arguments (could be extended to support #50 in the future)
  • Loading branch information
yan-foto authored Nov 10, 2018
2 parents 1078a0a + 024e866 commit 2cf23ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
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

0 comments on commit 2cf23ae

Please sign in to comment.