Skip to content

Commit

Permalink
Add concept of global config, and use that to control setMaxListeners
Browse files Browse the repository at this point in the history
fixes #81
  • Loading branch information
M-Zuber committed Jun 13, 2021
1 parent 2b85b79 commit f77bf4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ Tests run *perfectly*, ship it to the enterprise!
Once you have the watcher running, you can force restart all tasks by entering `rs`.
If you want to only force a single task, type the name of the key from the watch config (for example `rs test`).

### Global Config

#### `setMaxListeners`
If too many listeners are added to an event emitter, [`node.js` will send a warning (rightfully so) about potential memory leaks](https://nodejs.org/docs/latest/api/events.html#events_emitter_setmaxlisteners_n).
The default maximum is 10. If you need more than that, you can add a top level global config to your package.json
```json
"watchGlobalConfig": {
"setMaxListeners": true
}
```
And max listeners will be set on the relevant processes to the minimum needed to avoid the warning.
### Options

#### `patterns`
Expand Down
18 changes: 14 additions & 4 deletions watch-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ module.exports = function watchPackage(_pkgDir, exit, taskName) {
}
startScript(taskName, pkg, processes);
} else {

// We only look for the global watch config here, since it is otherwise not relevant
var setMaxListeners = null;
if (typeof pkg.watchGlobalConfig === 'object') {
setMaxListeners = pkg.watchGlobalConfig.setMaxListeners
}
var scriptsCount = Object.keys(pkg.watch).length;
Object.keys(pkg.watch).forEach(function (script) {
if (!pkg.scripts[script]) {
die('No such script "' + script + '"', 2)
}
startScript(script, pkg, processes);
startScript(script, pkg, processes, setMaxListeners, scriptsCount + 1);
})
}

Expand Down Expand Up @@ -94,7 +99,7 @@ function prefixer(prefix) {
})
}

function startScript(script, pkg, processes) {
function startScript(script, pkg, processes, setMaxListeners, scriptsCount) {
var exec = [npm, 'run', '-s', script].join(' ')
var patterns = null
var extensions = null
Expand Down Expand Up @@ -148,7 +153,12 @@ function startScript(script, pkg, processes) {
if (delay) { args = args.concat(['--delay', delay + 'ms']) }
if (verbose) { args = args.concat(['-V']) }
if (silent) { args = args.concat(['-q']) }
if (runOnChangeOnly) { args = args.concat(['--on-change-only']) }
if (runOnChangeOnly) { args = args.concat(['--on-change-only']) }
if (setMaxListeners){
process.setMaxListeners(scriptsCount)
stdin.stdout.setMaxListeners(scriptsCount)
stdin.stderr.setMaxListeners(scriptsCount)
}
args = args.concat(['--exec', exec])

var proc = processes[script] = spawn(nodemon, args, {
Expand Down

0 comments on commit f77bf4f

Please sign in to comment.