-
Notifications
You must be signed in to change notification settings - Fork 3
/
search.js
34 lines (31 loc) · 1.07 KB
/
search.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
var Q = require("q");
var glob = Q.denodeify(require("glob"));
var fs = require("fs");
var path = require("path");
module.exports = search;
function search(args) {
return args.reduceRight(function (next, arg) {
return function (list) {
return Q.ninvoke(fs, "stat", arg)
.then(function (stats) {
if (stats.isFile()) {
return Q.ninvoke(fs, "realpath", arg)
.then(function (realpath) {
list.push(realpath);
return next(list);
});
} else if (stats.isDirectory()) {
return glob(path.join(process.cwd(), arg, "**/*-{spec,test}.js"))
.then(function (files) {
list.push.apply(list, files);
return list;
});
} else {
throw new Error("Arg must be a directory or file: " + arg);
}
});
};
}, function (list) {
return Q(list);
})([]);
}