summary refs log tree commit diff
path: root/src/Utils.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-01-30 19:58:15 +0100
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-01-30 19:58:15 +0100
commit76000f8fa11dde2f67efdb22a87c4644ea49f9eb (patch)
tree67664d9c616068113b8fef6704b005726be8f0a7 /src/Utils.ts
parent:sparkles: Database (diff)
downloadserver-76000f8fa11dde2f67efdb22a87c4644ea49f9eb.tar.xz
:sparkles: Util
Diffstat (limited to 'src/Utils.ts')
-rw-r--r--src/Utils.ts38
1 files changed, 0 insertions, 38 deletions
diff --git a/src/Utils.ts b/src/Utils.ts
deleted file mode 100644

index 291372c1..00000000 --- a/src/Utils.ts +++ /dev/null
@@ -1,38 +0,0 @@ -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); -}