-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.ts
57 lines (51 loc) · 1.62 KB
/
helpers.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
'use strict';
import {gustav} from './index';
import {appendFileSync, writeFileSync} from 'fs';
import {Observable} from '@reactivex/rxjs';
// TODO: .d.ts for tail
// import {Tail} from 'tail';
let Tail = require('tail').Tail;
export let fileSource = gustav.source('fileSource', (config) => {
if (typeof config === 'string') { config = { filename: config }; }
let tailConfig = {
filename: config.filename,
lineSeparator: config.lineSeparator || /[\r]{0,1}\n/,
watchOptions: config.watchOptions || {},
fromStart: config.fromStart || false
};
return () => {
let logTail = new Tail(
tailConfig.filename,
tailConfig.lineSeparator,
tailConfig.watchOptions,
tailConfig.fromStart
);
return new Observable(o => {
logTail.on('line', (line) => o.next(line));
logTail.on('err', (err) => o.error(err));
logTail.on('end', () => o.complete());
});
};
});
export let consoleSink = gustav.sink('consoleSink', (prefix = 'Gustav:') => {
return (iO) => {
iO.subscribe(console.log.bind(console, prefix), console.log.bind(console, prefix), console.log.bind(console, prefix));
};
});
export let fileSink = gustav.sink('FileSink', (filename) => {
return (iO) => {
// Clear the file
writeFileSync(filename, '');
iO.subscribe(
arr => arr.forEach(title => appendFileSync(filename, title + '\n')),
err => console.log('err', err),
() => appendFileSync(filename, '**done**\n')
);
};
});
let noop = () => {};
export let nullSink = gustav.sink('nullSink', () => {
return (iO) => {
iO.subscribe(noop, (err) => console.log('err', err), noop);
};
});