/
reader-stream.ts
67 lines (55 loc) · 1.8 KB
/
reader-stream.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as stream from 'stream';
import * as readdir from '@mrmlnc/readdir-enhanced';
import FileSystemStream from '../adapters/fs-stream';
import { Task } from '../managers/tasks';
import { Entry } from '../types/entries';
import Reader from './reader';
class TransformStream extends stream.Transform {
constructor(private readonly reader: ReaderStream) {
super({ objectMode: true });
}
public _transform(entry: Entry, _encoding: string, callback: Function): void {
callback(null, this.reader.transform(entry));
}
}
export default class ReaderStream extends Reader<NodeJS.ReadableStream> {
/**
* Returns FileSystem adapter.
*/
public get fsAdapter(): FileSystemStream {
return new FileSystemStream(this.settings);
}
/**
* Use stream API to read entries for Task.
*/
public read(task: Task): NodeJS.ReadableStream {
const root = this.getRootDirectory(task);
const options = this.getReaderOptions(task);
const transform = new TransformStream(this);
const readable: NodeJS.ReadableStream = this.api(root, task, options);
return readable
.on('error', (err: NodeJS.ErrnoException) => this.isEnoentCodeError(err) ? null : transform.emit('error', err))
.pipe(transform);
}
/**
* Returns founded paths.
*/
public api(root: string, task: Task, options: readdir.Options): NodeJS.ReadableStream {
if (task.dynamic) {
return this.dynamicApi(root, options);
}
return this.staticApi(task, options);
}
/**
* Api for dynamic tasks.
*/
public dynamicApi(root: string, options: readdir.Options): NodeJS.ReadableStream {
return readdir.readdirStreamStat(root, options);
}
/**
* Api for static tasks.
*/
public staticApi(task: Task, options: readdir.Options): NodeJS.ReadableStream {
return this.fsAdapter.read(task.patterns, options.filter as readdir.FilterFunction);
}
}