Skip to content

Commit

Permalink
fix: subclass stream Transform since node 14 simplified construction …
Browse files Browse the repository at this point in the history
…does not offer "construct"
  • Loading branch information
mdeanjones committed Jul 6, 2023
1 parent 4b674b6 commit d9c14b6
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit d9c14b6

Please sign in to comment.