Skip to content

Commit

Permalink
Fix: MaxListenersExceededWarning (fixes #105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Aug 26, 2017
1 parent 64f6479 commit f7f7b43
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions bin/common/bootstrap.js
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions docs/node-api.md
Expand Up @@ -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.<br>
On the other hand, if `process.stdXXX.isTTY` is `true`, `npm-run-all` uses `inherit` option, so the configuration is unnecessary.
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/common.js
Expand Up @@ -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)
})
})
})
2 changes: 2 additions & 0 deletions test/fail.js
Expand Up @@ -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
Expand Down Expand Up @@ -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"])))
Expand Down

0 comments on commit f7f7b43

Please sign in to comment.