-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.js
76 lines (63 loc) · 1.92 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
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
/*
subDoc
Copyright (C) Subnodal Technologies. All Rights Reserved.
https://subnodal.com
Licenced by the Subnodal Open-Source Licence, which can be found at LICENCE.md.
*/
const fs = require("fs");
const path = require("path");
/*
@name tree.walk
Get a list of paths to files in a directory.
@param dir <String> Root path of directory to traverse
@returns <[String]> List of traversed file paths
*/
exports.walk = function(dir, excludePaths = [], treeResults = []) {
var dirResults = fs.readdirSync(dir);
dirResults.forEach(function(result) {
var shouldSkip = false;
excludePaths.forEach(function(exclusion) {
if (path.join(dir, result).startsWith(path.join(...exclusion.split(/\/|\\/g)))) {
shouldSkip = true;
}
});
if (shouldSkip) {
return;
}
try {
if (fs.statSync(path.join(dir, result)).isDirectory()) {
treeResults = exports.walk(path.join(dir, result), excludePaths, treeResults);
} else {
treeResults.push(path.join(dir, result));
}
} catch (e) {}
});
return treeResults;
};
/*
@name tree.clean
Delete directory and its contents if it doesn't exist.
@param dir <String> Directory to delete
*/
exports.clean = function(dir) {
if (fs.existsSync(dir)) {
fs.rmSync(dir, {recursive: true});
}
};
/*
@name tree.squash
Get all contents of specified JavaScript files.
@param files <[String]> List of files to extract contents of (may include non-JS files)
@returns <String> Concatenated contents of all JavaScript files
*/
exports.squash = function(files) {
var data = "";
files.forEach(function(file) {
try {
if (file.endsWith(".js")) {
data += fs.readFileSync(file) + "\n";
}
} catch (e) {}
});
return data;
};