-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.find_file.ts
60 lines (50 loc) · 1.95 KB
/
entry.find_file.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
import fs from "fs";
import path from "path";
import { fmt1$, verbose } from "./cli.output";
import { __DEFAULT_IS_TEST_FILES } from "./constants";
function directoryWalker(root: string, onFile: (file: string) => void = () => { }, onFolder: (folder: string) => /* skipWalkingOrNot: */ boolean = () => true, currentPath?: string) {
currentPath ??= root;
const files = fs.readdirSync(currentPath);
for (const file of files) {
const filePath = path.join(currentPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
onFolder(filePath) ? directoryWalker(root, onFile, onFolder, filePath) : void 0;
} else if (stats.isFile()) {
onFile(filePath);
}
}
}
function isTestFile(file: string) {
if ("isTestFile" in globalThis.config && typeof globalThis.config.isTestFile === "function") {
return globalThis.config.isTestFile(file);
} else if ("testFileExtensions" in globalThis.config && globalThis.config.testFileExtensions instanceof Array) {
return globalThis.config.testFileExtensions.some((ext) => file.endsWith(ext));
} else {
return __DEFAULT_IS_TEST_FILES(file);
}
}
function findTestFiles() {
let result: string[] = [];
const onFile = (file: string) => {
if (isTestFile(path.basename(file))) {
result.push(file);
}
}
const onFolder = (folder: string) => {
if ("ignoredFolders" in globalThis.config
&& globalThis.config.ignoredFolders instanceof Array
&& globalThis.config.ignoredFolders.includes(path.basename(folder))) {
return false;
}
return true;
}
directoryWalker(".", onFile, onFolder);
if (result.length == 0) {
verbose(`No test files found.`);
} else {
verbose(`Found ${fmt1$(result.length.toString())} test files. Pending to run...`);
}
return result;
}
export { directoryWalker, findTestFiles }