Add copy directory utility

cgen-44b39f42e95f481db120b871f5bdb8b1
This commit is contained in:
Builder.io 2025-09-18 04:40:56 +00:00
parent 2dc0a76cd7
commit cb97080ca0

View file

@ -93,3 +93,21 @@ export async function* walk(dir: FileSystemDirectoryHandle, pathPrefix = ""): As
}
}
}
export async function copyDirectory(src: FileSystemDirectoryHandle, dest: FileSystemDirectoryHandle): Promise<void> {
const ok = await ensureWritePerm(dest);
if (!ok) throw new Error("No write permission on destination");
// @ts-ignore
for await (const [name, entry] of (src as any).entries()) {
if (entry.kind === "directory") {
const sub = await (dest as any).getDirectoryHandle(name, { create: true });
await copyDirectory(entry as FileSystemDirectoryHandle, sub);
} else {
const file = await (entry as FileSystemFileHandle).getFile();
const fh = await (dest as any).getFileHandle(name, { create: true });
const w = await (fh as any).createWritable();
await w.write(await file.arrayBuffer());
await w.close();
}
}
}