From f7f7b435f7d8be99481593597f49fd719a270ecb Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Sat, 26 Aug 2017 21:40:29 +0900 Subject: [PATCH] Fix: MaxListenersExceededWarning (fixes #105) --- bin/common/bootstrap.js | 7 +++++++ docs/node-api.md | 6 ++++++ lib/index.js | 2 +- test/common.js | 23 +++++++++++++++++++++++ test/fail.js | 2 ++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/bin/common/bootstrap.js b/bin/common/bootstrap.js index 2717da2..e73b093 100644 --- a/bin/common/bootstrap.js +++ b/bin/common/bootstrap.js @@ -24,6 +24,13 @@ module.exports = function bootstrap(name) { return require("./version")(process.stdout) default: + // https://github.com/mysticatea/npm-run-all/issues/105 + // Avoid MaxListenersExceededWarnings. + process.stdout.setMaxListeners(0) + process.stderr.setMaxListeners(0) + process.stdin.setMaxListeners(0) + + // Main return require(`../${name}/main`)( argv, process.stdout, diff --git a/docs/node-api.md b/docs/node-api.md index 44f9bf6..cbea37f 100644 --- a/docs/node-api.md +++ b/docs/node-api.md @@ -104,3 +104,9 @@ runAll(["clean", "lint", "build"]) console.log(`${results[2].name}: ${results[2].code}`); // build: 0 }); ``` + +## About MaxListenersExceededWarning + +- 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. +- 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.
+ On the other hand, if `process.stdXXX.isTTY` is `true`, `npm-run-all` uses `inherit` option, so the configuration is unnecessary. diff --git a/lib/index.js b/lib/index.js index 4ee18cf..cc0c2e0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -208,7 +208,7 @@ function maxLength(length, name) { * @returns {Promise} * A promise object which becomes fullfilled when all npm-scripts are completed. */ -module.exports = function npmRunAll(patternOrPatterns, options) { +module.exports = function npmRunAll(patternOrPatterns, options) { //eslint-disable-line complexity const stdin = (options && options.stdin) || null const stdout = (options && options.stdout) || null const stderr = (options && options.stderr) || null diff --git a/test/common.js b/test/common.js index ca118b1..ce2d53e 100644 --- a/test/common.js +++ b/test/common.js @@ -330,4 +330,27 @@ describe("[common]", () => { assert(false, "Should fail.") }) }) + + // https://github.com/mysticatea/npm-run-all/issues/105 + describe("should not print MaxListenersExceededWarning when it runs 10 tasks:", () => { + const tasks = Array.from({ length: 10 }, () => "test-task:append:a") + + it("npm-run-all command", async () => { + const buf = new BufferStream() + await runAll(tasks, null, buf) + assert(buf.value.indexOf("MaxListenersExceededWarning") === -1) + }) + + it("run-s command", async () => { + const buf = new BufferStream() + await runSeq(tasks, null, buf) + assert(buf.value.indexOf("MaxListenersExceededWarning") === -1) + }) + + it("run-p command", async () => { + const buf = new BufferStream() + await runPar(tasks, null, buf) + assert(buf.value.indexOf("MaxListenersExceededWarning") === -1) + }) + }) }) diff --git a/test/fail.js b/test/fail.js index 1b5cf16..21ed7a8 100644 --- a/test/fail.js +++ b/test/fail.js @@ -12,6 +12,7 @@ const assert = require("power-assert") const nodeApi = require("../lib") const util = require("./lib/util") +const delay = util.delay const removeResult = util.removeResult const runAll = util.runAll const runPar = util.runPar @@ -43,6 +44,7 @@ describe("[fail] it should fail", () => { after(() => process.chdir("..")) beforeEach(removeResult) + afterEach(() => delay(1000)) describe("if an invalid option exists.", () => { it("npm-run-all command", () => shouldFail(runAll(["--invalid"])))