From 8f6194ff18a6d3dc163621028478eaafd79aec8d Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 20 Sep 2023 11:55:49 +0200 Subject: [PATCH] backend-test-utils: add directoryMocker.getContent() Signed-off-by: Patrik Oldsberg --- .../src/files/DirectoryMocker.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/backend-test-utils/src/files/DirectoryMocker.ts b/packages/backend-test-utils/src/files/DirectoryMocker.ts index 7e99f30086..66a6823a2f 100644 --- a/packages/backend-test-utils/src/files/DirectoryMocker.ts +++ b/packages/backend-test-utils/src/files/DirectoryMocker.ts @@ -81,6 +81,32 @@ export class DirectoryMocker { } } + async getContent(): Promise { + async function read(path: string): Promise { + if (!(await fs.pathExists(path))) { + return undefined; + } + const entries = await fs.readdir(path); + + return Object.fromEntries( + await Promise.all( + entries.map(async entry => { + const fullPath = resolvePath(path, entry); + const stat = await fs.stat(fullPath); + + if (stat.isDirectory()) { + return [entry, await read(fullPath)]; + } + const content = await fs.readFile(fullPath); + return [entry, content.toString('utf8')]; + }), + ), + ); + } + + return read(this.#root); + } + #transformInput(input: MockDirectory[string]): MockEntry[] { const entries: MockEntry[] = [];