blob: 291372c19170f1e03795d42192a62f6b302f6b16 (
plain) (
blame)
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
|
import fs from "fs/promises";
import "missing-native-js-functions";
export interface traverseDirectoryOptions {
dirname: string;
filter?: RegExp;
excludeDirs?: RegExp;
recursive?: boolean;
}
const DEFAULT_EXCLUDE_DIR = /^\./;
const DEFAULT_FILTER = /^([^\.].*)\.js$/;
export async function traverseDirectory<T>(
options: traverseDirectoryOptions,
action: (path: string) => T
): Promise<T[]> {
if (!options.filter) options.filter = DEFAULT_FILTER;
if (!options.excludeDirs) options.excludeDirs = DEFAULT_EXCLUDE_DIR;
const routes = await fs.readdir(options.dirname);
const promises = <Promise<T | T[] | undefined>[]>routes.map(async (file) => {
const path = options.dirname + file;
const stat = await fs.lstat(path);
if (path.match(<RegExp>options.excludeDirs)) return;
if (stat.isFile() && path.match(<RegExp>options.filter)) {
return action(path);
} else if (options.recursive && stat.isDirectory()) {
return traverseDirectory({ ...options, dirname: path + "/" }, action);
}
});
const result = await Promise.all(promises);
const t = <(T | undefined)[]>result.flat();
return <T[]>t.filter((x) => x != undefined);
}
|