From d9c14b6dc5f043499e4f8669a754e6877ebd27df Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Thu, 6 Jul 2023 11:09:21 +0200 Subject: [PATCH] fix: subclass stream Transform since node 14 simplified construction does not offer "construct" --- index.js | 60 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index d9049d2..0a63d22 100644 --- a/index.js +++ b/index.js @@ -105,41 +105,43 @@ async function mergeCoverageReportFiles(filePaths, options) { /** * + */ +class WrappingTransform extends Transform { + constructor(filePathsOrMergeOptions, mergeOptions) { + super(); + + this.filePaths = Array.isArray(filePathsOrMergeOptions) + ? filePathsOrMergeOptions + : []; + + this.mergeOptions = mergeOptions + ? mergeOptions + : Array.isArray(filePathsOrMergeOptions) + ? {} + : filePathsOrMergeOptions || {}; + } + + _transform(chunk, encoding, callback) { + this.filePaths.push(chunk.toString()); + callback(null); + } + + _flush(callback) { + mergeCoverageReportFiles(this.filePaths, this.mergeOptions).then( + (tempFile) => callback(null, tempFile), + (error) => callback(error) + ); + } +} + +/** * @param {string[] | import("./lib/Configuration").ConfigurationPojo} [filePathsOrOptions] * @param {import("./lib/Configuration").ConfigurationPojo} [options] * * @return {module:stream.internal.Transform} */ function mergeCoverageReportFilesStream(filePathsOrOptions, options) { - return new Transform({ - writableObjectMode: true, - - construct(callback) { - this.filePaths = Array.isArray(filePathsOrOptions) - ? filePathsOrOptions - : []; - - callback(null); - }, - - transform(chunk, encoding, callback) { - this.filePaths.push(chunk.toString()); - callback(null); - }, - - flush(callback) { - const opts = options - ? options - : Array.isArray(filePathsOrOptions) - ? {} - : filePathsOrOptions || {}; - - mergeCoverageReportFiles(this.filePaths, opts).then( - (tempFile) => callback(null, tempFile), - (error) => callback(error) - ); - }, - }); + return new WrappingTransform(filePathsOrOptions, options); } module.exports = {