From 34c7c6291ffd2c52b36e3a40bfa49eec222c6cbb Mon Sep 17 00:00:00 2001 From: Dave Houlbrooke Date: Tue, 15 Jan 2019 22:33:09 +0000 Subject: [PATCH] fix: more robust stream rescoping (to fix bug in npm module) --- lib/{rescopeStream.js => RescopedStream.js} | 28 ++++++++++++++------- lib/multiSemanticRelease.js | 6 ++--- 2 files changed, 22 insertions(+), 12 deletions(-) rename lib/{rescopeStream.js => RescopedStream.js} (55%) diff --git a/lib/rescopeStream.js b/lib/RescopedStream.js similarity index 55% rename from lib/rescopeStream.js rename to lib/RescopedStream.js index f71ebb32..3fa29cf8 100644 --- a/lib/rescopeStream.js +++ b/lib/RescopedStream.js @@ -1,3 +1,4 @@ +const { Writable } = require("stream"); const { check } = require("./blork"); /** @@ -7,16 +8,25 @@ const { check } = require("./blork"); * @param {stream.Writable} stream The actual stream to write messages to. * @param {string} scope The string scope for the stream (instances of the text `[semantic-release]` are replaced in the stream). * @returns {stream.Writable} Object that's compatible with stream.Writable (implements a `write()` property). + * + * @internal */ -function rescopeStream(stream, scope) { - // Check args. - check(scope, "scope: string"); - return { - write(msg) { - stream.write(msg.replace("[semantic-release]", `[${scope}]`)); - } - }; +class RescopedStream extends Writable { + // Constructor. + constructor(stream, scope) { + super(); + check(scope, "scope: string"); + check(stream, "stream: stream"); + this._stream = stream; + this._scope = scope; + } + + // Custom write method. + write(msg) { + check(msg, "msg: string"); + this._stream.write(msg.replace("[semantic-release]", `[${this._scope}]`)); + } } // Exports. -module.exports = rescopeStream; +module.exports = RescopedStream; diff --git a/lib/multiSemanticRelease.js b/lib/multiSemanticRelease.js index bf6c3e1f..de97a7c8 100644 --- a/lib/multiSemanticRelease.js +++ b/lib/multiSemanticRelease.js @@ -7,7 +7,7 @@ const getConfig = require("./getConfig"); const getConfigSemantic = require("./getConfigSemantic"); const getManifest = require("./getManifest"); const cleanPath = require("./cleanPath"); -const rescopeStream = require("./rescopeStream"); +const RescopedStream = require("./RescopedStream"); const createInlinePluginCreator = require("./createInlinePluginCreator"); /** @@ -163,7 +163,7 @@ async function releasePackage(pkg, createInlinePlugin, multiContext) { pkg.result = await semanticRelease(options, { cwd: dir, env, - stdout: rescopeStream(stdout, name), - stderr: rescopeStream(stdout, name) + stdout: new RescopedStream(stdout, name), + stderr: new RescopedStream(stdout, name) }); }