diff --git a/packages/php-wasm/universal/src/lib/write-files.ts b/packages/php-wasm/universal/src/lib/write-files.ts index c1c948a2d5..b35e60a032 100644 --- a/packages/php-wasm/universal/src/lib/write-files.ts +++ b/packages/php-wasm/universal/src/lib/write-files.ts @@ -45,7 +45,10 @@ export async function writeFiles( await php.rmdir(root, { recursive: true }); } } - for (const [relativePath, content] of Object.entries(newFiles)) { + newFiles = await newFiles; + for (const relativePath of Object.keys(newFiles)) { + const content = await newFiles[relativePath]; + const filePath = joinPaths(root, relativePath); if (!(await php.fileExists(dirname(filePath)))) { await php.mkdir(dirname(filePath)); @@ -53,8 +56,7 @@ export async function writeFiles( if (content instanceof Uint8Array || typeof content === 'string') { await php.writeFile(filePath, content); } else { - const fileTreeContent = content as MaybePromise; - await writeFiles(php, filePath, fileTreeContent); + await writeFiles(php, filePath, content); } } } diff --git a/packages/playground/blueprints/src/lib/resources.ts b/packages/playground/blueprints/src/lib/resources.ts index dc703c6ade..505a4474ba 100644 --- a/packages/playground/blueprints/src/lib/resources.ts +++ b/packages/playground/blueprints/src/lib/resources.ts @@ -353,21 +353,20 @@ export class BlueprintAssetDirectoryResource extends VFSDirectoryResource { async function createLazyVFSFileTree( path: string, playground: UniversalPHP -): Promise { - const keys = await playground.listFiles(path); - const keySet = new Set(keys); +): Promise { + const lazyFileTree: FileTreeAsync = {}; + + if (!(await playground.isDir(path))) { + throw new Error(`Path "${path}" is not a directory`); + } + + for (const fileName of await playground.listFiles(path)) { + Object.defineProperty(lazyFileTree, fileName, { + configurable: false, + enumerable: true, + async get() { + const fullPath = joinPaths(path, fileName); - return new Proxy( - {}, - { - ownKeys() { - return keys; - }, - async get(target, prop: string) { - if (!keySet.has(prop)) { - return undefined; - } - const fullPath = joinPaths(path, prop); if (!(await playground.fileExists(fullPath))) { return undefined; } @@ -375,11 +374,13 @@ async function createLazyVFSFileTree( if (await playground.isDir(fullPath)) { return createLazyVFSFileTree(fullPath, playground); } else { - return playground.readFileAsBuffer(joinPaths(path, prop)); + return playground.readFileAsBuffer(fullPath); } }, - } - ); + }); + } + + return lazyFileTree; } /**