Skip to content

Commit f7f7b43

Browse files
committed
Fix: MaxListenersExceededWarning (fixes #105)
1 parent 64f6479 commit f7f7b43

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

bin/common/bootstrap.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ module.exports = function bootstrap(name) {
2424
return require("./version")(process.stdout)
2525

2626
default:
27+
// https://github.com/mysticatea/npm-run-all/issues/105
28+
// Avoid MaxListenersExceededWarnings.
29+
process.stdout.setMaxListeners(0)
30+
process.stderr.setMaxListeners(0)
31+
process.stdin.setMaxListeners(0)
32+
33+
// Main
2734
return require(`../${name}/main`)(
2835
argv,
2936
process.stdout,

docs/node-api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,9 @@ runAll(["clean", "lint", "build"])
104104
console.log(`${results[2].name}: ${results[2].code}`); // build: 0
105105
});
106106
```
107+
108+
## About MaxListenersExceededWarning
109+
110+
- If you use `options.stdin`, `options.stdout`, or `options.stderr` in parallel mode, please configure max listeners by [emitter.setMaxListeners(n)](https://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n) properly.
111+
- If you don't use those options and `process.stdXXX.isTTY` is `false`, please configure max listeners of the `process.stdXXX` properly. In that case, `npm-run-all` uses piping to connect to child processes.<br>
112+
On the other hand, if `process.stdXXX.isTTY` is `true`, `npm-run-all` uses `inherit` option, so the configuration is unnecessary.

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function maxLength(length, name) {
208208
* @returns {Promise}
209209
* A promise object which becomes fullfilled when all npm-scripts are completed.
210210
*/
211-
module.exports = function npmRunAll(patternOrPatterns, options) {
211+
module.exports = function npmRunAll(patternOrPatterns, options) { //eslint-disable-line complexity
212212
const stdin = (options && options.stdin) || null
213213
const stdout = (options && options.stdout) || null
214214
const stderr = (options && options.stderr) || null

test/common.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,27 @@ describe("[common]", () => {
330330
assert(false, "Should fail.")
331331
})
332332
})
333+
334+
// https://github.com/mysticatea/npm-run-all/issues/105
335+
describe("should not print MaxListenersExceededWarning when it runs 10 tasks:", () => {
336+
const tasks = Array.from({ length: 10 }, () => "test-task:append:a")
337+
338+
it("npm-run-all command", async () => {
339+
const buf = new BufferStream()
340+
await runAll(tasks, null, buf)
341+
assert(buf.value.indexOf("MaxListenersExceededWarning") === -1)
342+
})
343+
344+
it("run-s command", async () => {
345+
const buf = new BufferStream()
346+
await runSeq(tasks, null, buf)
347+
assert(buf.value.indexOf("MaxListenersExceededWarning") === -1)
348+
})
349+
350+
it("run-p command", async () => {
351+
const buf = new BufferStream()
352+
await runPar(tasks, null, buf)
353+
assert(buf.value.indexOf("MaxListenersExceededWarning") === -1)
354+
})
355+
})
333356
})

test/fail.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
const assert = require("power-assert")
1313
const nodeApi = require("../lib")
1414
const util = require("./lib/util")
15+
const delay = util.delay
1516
const removeResult = util.removeResult
1617
const runAll = util.runAll
1718
const runPar = util.runPar
@@ -43,6 +44,7 @@ describe("[fail] it should fail", () => {
4344
after(() => process.chdir(".."))
4445

4546
beforeEach(removeResult)
47+
afterEach(() => delay(1000))
4648

4749
describe("if an invalid option exists.", () => {
4850
it("npm-run-all command", () => shouldFail(runAll(["--invalid"])))

0 commit comments

Comments
 (0)