-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (39 loc) · 1.06 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
'use strict';
var fs = require('fs');
var path = require('path');
var expand = require('expand-tilde');
var isBinary = require('file-is-binary');
var through = require('through2');
var get = require('get-value');
module.exports = function(options) {
var opts = Object.assign({prop: 'data.contents'}, options);
return through.obj(function(file, enc, next) {
if (file.isDirectory() || isBinary(file)) {
next(null, file);
return;
}
if (typeof get(file, opts.prop) !== 'string') {
next(null, file);
return;
}
if (typeof opts.resolve === 'function') {
opts.resolve(file, opts, next);
return;
}
resolve(file, opts, function(err, filepath) {
if (err) {
next(err);
return;
}
if (fs.existsSync(filepath)) {
file.contents = fs.readFileSync(filepath);
}
next(null, file);
});
});
};
function resolve(file, options, next) {
var filename = get(file, options.prop);
var cwd = expand(options.cwd || file.base);
next(null, path.resolve(cwd, filename));
}