Files
smartgulp/ts/smartgulp.classes.gulpstream.ts

29 lines
806 B
TypeScript
Raw Permalink Normal View History

2017-04-30 00:25:31 +02:00
// this file contains the code to generate and handle the stream
2022-06-09 19:59:21 +02:00
import * as plugins from './smartgulp.plugins.js';
2023-11-25 18:11:31 +01:00
import { SmartFile } from '@push.rocks/smartfile';
2017-04-30 00:25:31 +02:00
2018-03-03 13:57:55 +01:00
import { Transform } from 'stream';
2017-04-30 00:25:31 +02:00
export class GulpStream {
2018-03-03 13:57:55 +01:00
stream = new Transform({ objectMode: true });
2017-04-30 00:25:31 +02:00
/**
* allows you to pipe a SmartfileArray into the stream
*/
2023-11-25 18:11:31 +01:00
async pipeSmartfileArray(smartfileArray: SmartFile[]) {
2017-04-30 00:25:31 +02:00
// ensure what we get is an array
if (!Array.isArray(smartfileArray)) {
2018-03-03 13:57:55 +01:00
throw new Error('fileArg has unknown format');
2017-04-30 00:25:31 +02:00
}
for (let smartfile of smartfileArray) {
2018-03-03 13:57:55 +01:00
let hasWritten = this.stream.push(smartfile);
2017-04-30 00:25:31 +02:00
if (!hasWritten) {
2018-03-03 13:57:55 +01:00
await plugins.smartevent.once(this.stream, 'drain');
2017-04-30 00:25:31 +02:00
}
2018-03-03 13:57:55 +01:00
}
2017-04-30 00:25:31 +02:00
// signal end of stream;
2018-03-03 13:57:55 +01:00
this.stream.push(null);
2017-04-30 00:25:31 +02:00
}
}