Skip to content

Commit

Permalink
Simplify transform utilities. (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Apr 23, 2021
1 parent fad0054 commit e262506
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/util/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ const {Transform} = require('stream');

class YoResolveError extends WError {}

function passthroughTransform(file, _enc, cb) {
this.push(file);
cb();
}

function createFileTransform(transform = passthroughTransform, options = {}) {
function createFileTransform(
transform = (file, _enc, cb) => cb(null, file),
options = {}
) {
const stream = new Transform({
objectMode: true,
...options,
Expand Down Expand Up @@ -64,12 +62,10 @@ function createEachFileTransform(forEach, options = {}) {
forward();
return;
}
const mayBePromise = forEach.call(this, file, enc, autoContinue ? undefined : cb);
if (mayBePromise && mayBePromise.then) {
mayBePromise.then(() => forward()).catch(error => cb(error));
} else {
forward();
}
Promise
.resolve(forEach.call(this, file, enc, autoContinue ? undefined : cb))
.then(() => forward())
.catch(error => cb(error));
});
}

Expand Down

0 comments on commit e262506

Please sign in to comment.