-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtree.js
48 lines (39 loc) · 1.09 KB
/
tree.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
var fs = require('fs')
, path = require('path')
, minimatch = require('minimatch');
var Tree = function (source_path, ignore_paths) {
this.source_path = source_path;
this.ignore_paths = ignore_paths || [];
this.folders = [];
this.files = [];
}
Tree.prototype = {
parse: function () {
this.parse_folder(this.source_path);
},
parse_folder: function (folder) {
var paths = fs.readdirSync(folder)
, source_path
, lstat;
this.folders.push(folder);
for (var x=0; x<paths.length; x++) {
source_path = path.join(folder, paths[x]);
if (!this.is_ignore_path(source_path)) {
lstat = fs.lstatSync(source_path);
if (lstat.isDirectory()) {
this.parse_folder(source_path);
} else if (lstat.isFile()) {
this.files.push(source_path);
}
}
}
},
is_ignore_path: function (p) {
p = p.replace(this.source_path, '').slice(1);
for (var x=0; x<this.ignore_paths.length; x++) {
if (minimatch(p, this.ignore_paths[x], { dot: true })) return true;
}
return false;
}
}
module.exports = Tree;