-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (60 loc) · 1.53 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var through = require('through2').obj;
var fs = require('graceful-fs');
var lazystream = require('lazystream');
var stripBomFromBuffer = require('strip-bom-buf');
var stripBomFromStream = require('strip-bom-stream');
var assign = require('object-assign');
function readSymlink(file, cb) {
fs.readlink(file.path, function(err, target) {
if (err) {
return cb(err);
}
file.symlink = target;
return cb(null, file);
});
}
function readBuffer(file, options, cb) {
fs.readFile(file.path, function(err, data) {
if (err) {
return cb(err);
}
if (options.stripBOM) {
file.contents = stripBomFromBuffer(data);
} else {
file.contents = data;
}
cb(null, file);
});
}
function readStream(file, options, cb) {
var filePath = file.path;
file.contents = new lazystream.Readable(function() {
return fs.createReadStream(filePath);
});
if (options.stripBOM) {
file.contents = file.contents.pipe(stripBomFromStream());
}
cb(null, file);
}
module.exports = function(opts) {
var options = assign({
force:false,
buffer:true,
stripBOM:true
}, opts);
return through(function(file, enc, cb) {
if (!options.force && !file.isNull()) {
return cb(null, file);
}
if (file.isDirectory()) {
return cb(null, file);
}
if (file.stat && file.stat.isSymbolicLink()) {
return readSymlink(file, cb);
}
if (options.buffer !== false) {
return readBuffer(file, options, cb);
}
return readStream(file, options, cb);
});
};