-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocationContent.js
98 lines (77 loc) · 2.64 KB
/
locationContent.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var fs = require("fs");
var path = require("path");
function filterIgnored(content, ignoredPaths) {
return content.filter(p => {
var keepFile = true;
ignoredPaths.forEach(ignored => {
if(path.resolve(p).startsWith(ignored)) {
keepFile = false
}
});
return keepFile;
})
}
function getFiles(location, ignorePaths) {
var content = fs.readdirSync(location);
content = filterIgnored(content.map(e => `${location}/${e}`), ignorePaths);
var dirs = [];
var files = content
.filter(c => {
if(fs.statSync(`${c}`).isFile()) {
return true;
} else {
dirs.push(`${c}`);
return false
}
})
.map(c => `${c}` );
dirs.forEach(dir =>
files = files.concat(getFiles(dir, ignorePaths)));
return files;
}
function getAll(location, ignorePaths) {
var content = fs.readdirSync(location);
var allContent = content.map(e => `${location}/${e}`);
allContent = filterIgnored(allContent, ignorePaths);
content
.filter(c => fs.statSync(`${location}/${c}`).isDirectory())
.map(dir => `${location}/${dir}` )
.forEach(dir => {
allContent = allContent.concat(getAll(dir, ignorePaths))
});
return allContent;
}
module.exports = getFiles;
module.exports.getFiles = function (location, options) {
var locationPath = path.resolve(location);
var ignorePaths = [];
if(options && options.ignore) {
ignorePaths = options.ignore.map(e => path.resolve(e));
}
var content = getFiles(locationPath, ignorePaths);
if(options && options.filter) {
content = content.filter(options.filter);
}
if(options && options.useRelative) {
if(location == "./") { location = "."; }
content = content.map(p => p.replace(__dirname, location));
}
content = content.map(e => e.replace(locationPath + "/", ""));
return content;
};
module.exports.getAll = function (location, options) {
var locationPath = path.resolve(location);
var ignorePaths = [];
if(options && options.ignore) {
ignorePaths = options.ignore.map(e => path.resolve(e));
}
var content = getAll(locationPath, ignorePaths);
if(options && options.filter) {
content = content.filter(options.filter);
}
if(options && options.useRelative) {
if(location == "./") { location = "."; }
content = content.map(p => p.replace(__dirname, location));
}
return content;
};