Skip to content

Commit

Permalink
Fix lazy content lookups for dir resources
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed Nov 28, 2024
1 parent 0096022 commit 00efc7c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
8 changes: 5 additions & 3 deletions packages/php-wasm/universal/src/lib/write-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ 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));
}
if (content instanceof Uint8Array || typeof content === 'string') {
await php.writeFile(filePath, content);
} else {
const fileTreeContent = content as MaybePromise<FileTree>;
await writeFiles(php, filePath, fileTreeContent);
await writeFiles(php, filePath, content);
}
}
}
35 changes: 18 additions & 17 deletions packages/playground/blueprints/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,33 +353,34 @@ export class BlueprintAssetDirectoryResource extends VFSDirectoryResource {
async function createLazyVFSFileTree(
path: string,
playground: UniversalPHP
): Promise<FileTree> {
const keys = await playground.listFiles(path);
const keySet = new Set(keys);
): Promise<FileTreeAsync> {
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<FileTree>(
{},
{
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;
}

if (await playground.isDir(fullPath)) {
return createLazyVFSFileTree(fullPath, playground);
} else {
return playground.readFileAsBuffer(joinPaths(path, prop));
return playground.readFileAsBuffer(fullPath);
}
},
}
);
});
}

return lazyFileTree;
}

/**
Expand Down

0 comments on commit 00efc7c

Please sign in to comment.