forked from syavorsky/comment-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (46 loc) · 1.17 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict'
var fs = require('fs')
var stream = require('readable-stream')
var util = require('util')
var parse = require('./parser')
module.exports = parse
/* ------- Transform stream ------- */
function Parser (opts) {
opts = opts || {}
stream.Transform.call(this, {objectMode: true})
this._extract = parse.mkextract(opts)
}
util.inherits(Parser, stream.Transform)
Parser.prototype._transform = function transform (data, encoding, done) {
var block
var lines = data.toString().split(/\n/)
while (lines.length) {
block = this._extract(lines.shift())
if (block) {
this.push(block)
}
}
done()
}
module.exports.stream = function stream (opts) {
return new Parser(opts)
}
/* ------- File parser ------- */
module.exports.file = function file (file_path, done) {
var opts = {}
var collected = []
if (arguments.length === 3) {
opts = done
done = arguments[2]
}
return fs.createReadStream(file_path, {encoding: 'utf8'})
.on('error', done)
.pipe(new Parser(opts))
.on('error', done)
.on('data', function (data) {
collected.push(data)
})
.on('finish', function () {
done(null, collected)
})
}