Skip to content

Commit

Permalink
👌 works if we run within the sandbox folder (gulp stream issue)
Browse files Browse the repository at this point in the history
(apparently resolved in gulpjs/glob-stream#131)
  • Loading branch information
fluffynuts committed Apr 8, 2024
1 parent 7f3c53d commit c99d306
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 193 deletions.
51 changes: 0 additions & 51 deletions src/streamify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,54 +45,3 @@ export function streamify_original<T>(
);
}

export function streamify_perdocs<T>(
fn: AsyncTVoid<T>,
optionsFactory: OptionsFactory<T>,
pluginName: string,
operation: string
): Transform {
// Monkey patch Transform or create your own subclass,
// implementing `_transform()` and optionally `_flush()`
var transformStream = new Transform({ objectMode: true });
/**
* @param {Buffer|string} file
* @param {string=} encoding - ignored if file contains a Buffer
* @param {function(Error, object)} callback - Call this function (optionally with an
* error argument and data) when you are done processing the supplied chunk.
*/
transformStream._transform = async function (file, encoding, callback) {
const
error = null,
options = await optionsFactory(file),
output = await fn(options);
callback(error, output);
};

return sink(transformStream);
}

export function streamify_perdocs_sync<T>(
fn: (arg: T) => void,
optionsFactory: SyncOptionsFactory<T>,
pluginName: string,
operation: string
): Transform {
// Monkey patch Transform or create your own subclass,
// implementing `_transform()` and optionally `_flush()`
var transformStream = new Transform({ objectMode: true });
/**
* @param {Buffer|string} file
* @param {string=} encoding - ignored if file contains a Buffer
* @param {function(Error, object)} callback - Call this function (optionally with an
* error argument and data) when you are done processing the supplied chunk.
*/
transformStream._transform = function (file, encoding, callback) {
const
error = null,
options = optionsFactory(file),
output = fn(options);
callback(error, output);
};

return sink(transformStream);
}
178 changes: 36 additions & 142 deletions tests/streamify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const sink = require("lead");
const { Sandbox } = require("filesystem-sandbox");
import * as vinyl from "vinyl";
import * as gulp from "gulp";
import { streamify_original, streamify_perdocs, streamify_perdocs_sync } from "../src/streamify";
import { streamify_original } from "../src/streamify";

describe(`streamify-async-function`, () => {
interface FooOpts {
Expand All @@ -24,68 +24,24 @@ describe(`streamify-async-function`, () => {
captured.opts = opts;
}

describe(`lead`, () => {
it(`should work as per the readme example`, async () => {
// Arrange
const { Readable, Transform } = require("streamx");
const sink = require("lead");
let calls = 0;
// Act
const maybeThrough = new Transform({
transform(chunk: any, cb: any) {
calls++;
cb(null, chunk);
}
});

// Assert
// this is the example as at https://github.com/gulpjs/lead
Readable.from(['hello', 'world'])
// Sink it to behave like a Writeable
.pipe(sink(maybeThrough));
await sleep(100); // stream should be done by then
expect(calls)
.toEqual(2);

// now, what if we do the same thing, but with gulp.src
// as the source?
const
sandbox = await Sandbox.create();

await sandbox.writeFile("first.txt", "first");
await sandbox.writeFile("second.txt", "second");
await sandbox.writeFile("third.txt", "third");
gulp.src(`${sandbox.path}/*`)
.pipe(sink(maybeThrough));
await new Promise(resolve => setTimeout(resolve, 100));
// this _will_ fail, because the transform _wasn't_ called.
expect(calls)
.toEqual(5);
});
});

async function sleep(ms: number): Promise<void> {
await new Promise(resolve => setTimeout(resolve, ms));
}

describe(`discovery`, () => {
describe(`discovery: example plugin code`, () => {
it(`should complete`, async () => {
// Arrange
const sandbox = await Sandbox.create();
await sandbox.writeFile("foo.txt", "moo-cow");
// Act
await new Promise<void>(resolve => {

gulp.src(`${ sandbox.path }/*`)
.pipe(
through.obj(function (chunk, enc, cb) {
console.log("-- going through the pipe ---");
cb();
resolve();
}
)
);

await sandbox.run(async () => {
await new Promise<void>(resolve => {
gulp.src(`*`)
.pipe(
through.obj(function (chunk, enc, cb) {
console.log("-- going through the pipe ---");
cb();
resolve();
}
)
);
});
});
});
});
Expand All @@ -96,93 +52,31 @@ describe(`streamify-async-function`, () => {
const sandbox = await Sandbox.create();
const txtFile = await sandbox.writeFile("foo.txt", "moo-cow");
// Act
await new Promise<void>(resolve => {
gulp.src(`${ sandbox.path }/**/*.txt`)
.pipe(
sink(
streamify_original(
foo,
(f: vinyl.BufferFile) => {
return {
target: f.path,
flag: true
}
},
"test plugin",
"foo"
await sandbox.run(async () => {
await new Promise<void>(resolve => {
gulp.src(`**/*.txt`)
.pipe(
sink(
streamify_original(
foo,
(f: vinyl.BufferFile) => {
return {
target: f.path,
flag: true
}
},
"test plugin",
"foo"
)
)
)
)
.pipe(
through.obj(function () {
resolve();
}
)
).pipe(gulp.dest(sandbox.path));
});
// Assert
expect(captured.opts.target)
.toEqual(txtFile)
});
});

describe(`modified example function`, () => {
it(`should provide a new function taking the same arguments, which can be streamed`, async () => {
// Arrange
const sandbox = await Sandbox.create();
const txtFile = await sandbox.writeFile("foo.txt", "moo-cow");
// Act
await new Promise<void>(resolve => {
gulp.src(`${ sandbox.path }/**/*.txt`)
.pipe(streamify_perdocs(
foo,
(f: vinyl.BufferFile) => {
return {
target: f.path,
flag: true
}
},
"test plugin",
"foo"
)
).pipe(
through.obj(function () {
resolve();
}
)
);
});
// Assert
expect(captured.opts.target)
.toEqual(txtFile)
});
});

describe(`modified example function (sync)`, () => {
it(`should provide a new function taking the same arguments, which can be streamed`, async () => {
// Arrange
const sandbox = await Sandbox.create();
const txtFile = await sandbox.writeFile("foo.txt", "moo-cow");
// Act
await new Promise<void>(resolve => {
gulp.src(`${ sandbox.path }/**/*.txt`)
.pipe(streamify_perdocs_sync(
foo_sync,
(f: vinyl.BufferFile) => {
return {
target: f.path,
flag: true
.pipe(
through.obj(function () {
resolve();
}
},
"test plugin",
"foo"
)
).pipe(
through.obj(function () {
resolve();
}
)
);
)
).pipe(gulp.dest("_build"));
});
});
// Assert
expect(captured.opts.target)
Expand Down

0 comments on commit c99d306

Please sign in to comment.