-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
44 lines (36 loc) · 1022 Bytes
/
index.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
// @flow
'use strict';
const fs = require('fs');
const stream = require('stream');
const readline = require('readline');
const toml = require('toml');
module.exports = function createReadableTomlStream(fileStream /*: stream$Readable */) {
let tomlStream = new stream.Readable({ objectMode: true });
let lineReader = readline.createInterface({
input: fileStream,
});
let current = '';
function process(line) {
if (line === null || line.startsWith('[')) {
try {
tomlStream.push(toml.parse(current));
} catch (err) {
err.chunk = current;
tomlStream.emit('error', err);
}
current = '';
}
if (line === null) {
tomlStream.push(null);
} else {
current += line + '\n';
}
};
lineReader.on('line', line => process(line));
lineReader.on('close', () => process(null));
(tomlStream /*: any */)._read = () => {};
(tomlStream /*: any */)._destroy = () => {
(fileStream /*: any */).destroy();
};
return tomlStream;
};