Skip to content

Commit

Permalink
feat(transform): add support to async transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 11, 2023
1 parent f236657 commit 4b6790d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions workspaces/transform/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Minimatch, type MinimatchOptions } from 'minimatch';

type File = { path: string; contents: any };

type TransformFile<F extends File = File> = (file: F) => F | undefined;
type TransformFile<F extends File = File> = (file: F) => Promise<F> | F | undefined;

export function createTransform<F extends File = File>(transform: TransformFile<F>) {
return new Transform({
objectMode: true,
transform(chunk: any, _encoding: BufferEncoding, callback: TransformCallback) {
async transform(chunk: any, _encoding: BufferEncoding, callback: TransformCallback) {
try {
callback(undefined, transform.apply(this, chunk));
callback(undefined, await transform.apply(this, chunk));
} catch (error: unknown) {
callback(error as Error);
}
Expand Down

0 comments on commit 4b6790d

Please sign in to comment.