-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from thwonghin/feat/recursive-file-search
feat: support recursive file search
- Loading branch information
Showing
5 changed files
with
98 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { createModel } from "../../../dist"; | ||
import UUIDMixin from "../../mixins/UUID.mixin"; | ||
import UserModel from "../../models/User.model"; | ||
|
||
export default createModel((AuthModel) => { | ||
AuthModel | ||
.mixin(UUIDMixin) | ||
.string("hash") | ||
.string("salt") | ||
.relation("user", UserModel, { fields: ["userId"], references: ["email"] }) | ||
.string("userId", { unique: true }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
import { readdir } from "fs/promises"; | ||
import { readdir, stat } from "fs/promises"; | ||
import { join } from "path"; | ||
|
||
export const importAllFiles = async (basePath: string, folderName: string) => { | ||
const isDirectory = async (fullPath: string): Promise<boolean> => { | ||
return (await stat(fullPath)).isDirectory(); | ||
}; | ||
|
||
async function asyncFilter<T>( | ||
arr: T[], | ||
predicate: (value: T) => Promise<boolean> | ||
): Promise<T[]> { | ||
const results = await Promise.all(arr.map(predicate)); | ||
|
||
return arr.filter((_, index) => results[index]); | ||
} | ||
|
||
const getAllFilesRecursively = async ( | ||
basePath: string, | ||
folderName: string | ||
): Promise<string[]> => { | ||
const directoryPath = join(basePath, folderName); | ||
return readdir(directoryPath) | ||
.then((fileNames) => { | ||
const promises = fileNames.map( | ||
(fileName) => import(join(basePath, folderName, fileName)) | ||
); | ||
|
||
return Promise.all(promises); | ||
}) | ||
.catch(() => void 0); | ||
|
||
const fileNames = await readdir(directoryPath); | ||
const directories = await asyncFilter(fileNames, async (fileName) => | ||
isDirectory(join(directoryPath, fileName)) | ||
); | ||
const files = fileNames | ||
.filter((fileName) => !directories.includes(fileName)) | ||
.map((file) => join(directoryPath, file)); | ||
|
||
const filesInDirectories = ( | ||
await Promise.all( | ||
directories.map((directory) => | ||
getAllFilesRecursively(directoryPath, directory) | ||
) | ||
) | ||
).flat(); | ||
|
||
return [...filesInDirectories, ...files]; | ||
}; | ||
|
||
export const importAllFiles = async (basePath: string, folderName: string) => { | ||
return getAllFilesRecursively(basePath, folderName) | ||
.then((files) => Promise.all(files.map((fileName) => import(fileName)))) | ||
.catch(console.error); | ||
}; |