-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (32 loc) · 939 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
var path = require('path');
var flowRemoveTypes = require('flow-remove-types');
var through = require('through');
var defaultOptions = {
extensions: [ '.js', '.jsx', '.flow' ]
};
module.exports = function unflowify(filename, options) {
options = Object.assign({}, defaultOptions, options);
if (options.extensions &&
options.extensions.indexOf(path.extname(filename)) === -1) {
return through();
}
var file = [];
function write(chunk) {
if (!Buffer.isBuffer(chunk)) {
chunk = Buffer.from(chunk)
}
return file.push(chunk)
}
function transform() {
var source = Buffer.concat(file).toString('utf8');
try {
this.queue(flowRemoveTypes(source, options).toString());
this.queue(null);
} catch (error) {
error.message = filename + ': ' + error.message;
error.fileName = filename;
this.emit('error', error);
}
}
return through(write, transform);
}