-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathResultSymlinkPlugin.js
49 lines (47 loc) · 1.54 KB
/
ResultSymlinkPlugin.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var popPathSeqment = require("./popPathSeqment");
function ResultSymlinkPlugin(appendings) {
}
module.exports = ResultSymlinkPlugin;
ResultSymlinkPlugin.prototype.apply = function(resolver) {
resolver.plugin("result", function pluginMethod(request, callback) {
var fs = this.fileSystem;
var paths = [request.path];
var pathSeqments = [];
var addr = [request.path];
var pathSeqment = popPathSeqment(addr);
while(pathSeqment) {
pathSeqments.push(pathSeqment);
paths.push(addr[0]);
pathSeqment = popPathSeqment(addr);
}
pathSeqments.push(paths[paths.length-1]);
var log = callback.log;
var missing = callback.missing;
var containsSymlink = false;
this.forEachBail(paths.map(function(_, i) { return i; }), function(idx, callback) {
fs.readlink(paths[idx], function(err, result) {
if(!err && result) {
pathSeqments[idx] = result;
containsSymlink = true;
// Shortcut when absolute symlink found
if(/^(\/|[a-zA-z]:($|\\))/.test(result))
return callback(null, idx);
}
callback();
});
}, function(err, idx) {
if(!containsSymlink) return callback();
var resultSeqments = typeof idx === "number" ? pathSeqments.slice(0, idx+1) : pathSeqments.slice();
var result = resultSeqments.reverse().reduce(function(a, b) {
return this.join(a, b);
}.bind(this));
log("resolved symlink to " + result);
request.path = result;
pluginMethod.call(this, request, callback);
}.bind(this));
});
};