-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-copy.js
56 lines (48 loc) · 1.54 KB
/
smart-copy.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
var pathJoin = require('pathjoin');
var modes = require('js-git/lib/modes');
var sha1 = require('git-sha1');
module.exports = smartCopy;
function smartCopy(servePath, req, callback) {
var rules = req.rules.map(function (pair) {
return {
pattern: new RegExp(pair[0]),
include: pair[1]
};
});
if (req.paths.local && !shouldCopy(req.paths.local)) return callback();
var path = req.input[0] === "." ?
pathJoin(req.paths.rule, "..", req.input, req.paths.local) :
pathJoin(req.paths.root, req.input, req.paths.local);
servePath(path, function (err, result) {
if (!result || !result.hash) return callback(err);
callback(null, {
mode: result.mode,
hash: sha1(result.hash + "-" + req.ruleHash + "-" + req.codeHash),
root: result.root,
fetch : modes.isFile(result.mode) ? result.fetch : fetchTree
});
function fetchTree(callback) {
result.fetch(function (err, tree) {
if (!tree) return callback(err);
var newTree = {};
Object.keys(tree).forEach(function (name) {
var isTree = result.mode === modes.tree;
var fullPath = pathJoin(path, name);
if (shouldCopy(fullPath, isTree)) {
newTree[name] = tree[name];
}
});
callback(null, newTree);
});
}
});
function shouldCopy(path, start) {
var should = start;
for (var i = 0, l = rules.length; i < l; i++) {
var rule = rules[i];
if (!rule.pattern.test(path)) continue;
should = rule.include;
}
return should;
}
}