smartgulp/ts/smartgulp.classes.gulpstream.ts

29 lines
806 B
TypeScript
Raw Permalink Normal View History

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