/
PullStream.js
52 lines (44 loc) · 1.53 KB
/
PullStream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import readline from 'readline';
import { dest } from 'gulp';
import Logger from 'gulplog';
import ProjectConfig from '../../config/ProjectConfig';
import Transformer, { TransformDirection } from '../transform/Transformer';
import MappingTransformer from '../../transform/Mapping';
/**
* A stream that transforms read {@link ReadStream.ReadResult}s and stores the on the filesystem.
*/
export default class PullStream {
/**
* Creates a new PullStream based on a stream that writes {@link ReadStream.ReadResult} which may
* be an instance of {@link ReadStream}.
* @param {Stream} readStream The stream to read from.
*/
constructor(readStream) {
let pulled = 0;
readStream
.on('data', () => pulled++);
const mappingStream = new MappingTransformer({ direction: TransformDirection.FromDB });
const printProgress = setInterval(() => {
Logger.info(`Pulled: ${pulled}`);
if (Logger.listenerCount('info') > 0) {
readline.cursorTo(process.stdout, 0);
readline.moveCursor(process.stdout, 0, -1);
}
}, 1000);
return Transformer.applyTransformers(
readStream
.pipe(mappingStream),
ProjectConfig.useTransformers,
TransformDirection.FromDB
)
.pipe(dest('./src'))
.on('data', () => {}) // Unpipe readable stream
.on('end', () => {
if (Logger.listenerCount('info') > 0) {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
}
clearInterval(printProgress);
});
}
}