smartgulp/ts/smartgulp.classes.gulpstream.ts

31 lines
839 B
TypeScript
Raw 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';
2018-09-02 12:35:49 +00:00
import { Smartfile } from '@pushrocks/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
*/
async pipeSmartfileArray(smartfileArray: Smartfile[]) {
// 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
} else {
// iterate
}
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
}
}