-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.ts
65 lines (55 loc) · 1.33 KB
/
files.ts
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
import { promises } from "fs";
const { readdir, stat } = promises;
import { resolve, join } from "path";
const ignoreList: Array<string> = [
".brain",
"__pycache__",
"node_modules",
".git",
".mypy",
".pytest",
".vscode",
"env",
];
function includesIgnored(path: Array<string> | string): boolean {
if (Array.isArray(path)) {
for (const p of path) {
for (const i of ignoreList) {
if (p.includes(i)) {
return true;
}
}
}
} else {
for (const i of ignoreList) {
if (path.includes(i)) {
return true;
}
}
}
return false;
}
export async function getAllFiles(
dirPath: string,
results?: Array<string>,
absolute?: boolean
): Promise<Array<string>> {
if (includesIgnored(dirPath)) return results;
const files = await readdir(dirPath);
results = results || [];
for (const file of files) {
const name = resolve(join(dirPath, file));
const stats = await stat(name);
if (stats.isDirectory()) {
results = await getAllFiles(name, results, true);
} else if (!includesIgnored(name)) {
results.push(name);
}
}
if (!absolute) {
results = results.map((f) => f.replace(dirPath, ""));
results = results.map((f) => f.replace(/^\/+/, ""));
results = results.map((f) => f.replace(/^\\+/, ""));
}
return results;
}