-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (64 loc) · 2.09 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
var fs = require("fs");
var pathLibrary = require("path");
var merge = require('merge2');
var foldersFound = [];
var dirBase = "";
var readDirectory = function(dir, eachFile) {
var files = fs.readdirSync(dir);
files.forEach(eachFile);
};
var recursiveReadFolder = function(dir, exclude, fileList) {
if (typeof fileList === "undefined") {
fileList = [];
}
readDirectory(dir, function(fileName) {
var filePath = pathLibrary.join(dir, fileName);
if (exclude.length < 1 || exclude.indexOf(fileName) < 0) {
if (fs.statSync(filePath).isDirectory()) {
foldersFound.push({
name: fileName,
path: filePath,
pathTarget: getPathTarget(filePath)
});
fileList = recursiveReadFolder(filePath, exclude, fileList);
} else {
fileList.push(filePath);
}
}
});
return fileList;
};
var getPathTarget = function(path) {
return pathLibrary.relative(dirBase, path);
};
var getFolderBase = function(baseDir) {
var folder = { name: "", path: "" };
readDirectory(baseDir, function(fileName) {
var fileTest = pathLibrary.join(baseDir, fileName);
if (!fs.statSync(fileTest).isDirectory()) {
var pathExample = fileTest.split(pathLibrary.sep);
folder.name = pathExample[pathExample.length - 2];
folder.path = pathLibrary.join(baseDir, "");
folder.pathTarget = getPathTarget(folder.path);
}
});
return folder;
};
var taskSelf = function(options, tasks) {
return function(done) {
// set global variables
dirBase = options.base;
foldersFound = [];
var folderBase = getFolderBase(options.base);
if (folderBase.name) {
foldersFound.push(folderBase);
}
recursiveReadFolder(options.base, options.exclude || []);
var streams = foldersFound.map(tasks);
if (streams.length === 0) {
done();
}
return merge(streams);
}
};
module.exports = taskSelf;