Skip to content

Commit

Permalink
Copy local storage data when downloading projects for the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
netalondon committed Sep 17, 2024
1 parent 3600214 commit dcfabf2
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions components/src/stores/base.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,43 @@ import {
removeLocalAdapterFromIndexedDB,
} from "./base/indexDb.js";

async function cloneTree(
sourceFs: FileSystem,
targetFs: FileSystem,
dir = "/",
pathTransform: (path: string) => string,
overwrite = false,
) {
const sourceDir = dir == "/" ? "" : dir;
const targetDir = pathTransform(sourceDir);

const sourceItems = await sourceFs.scandir(dir);

targetFs.mkdir(targetDir);
const targetItems = new Set(
(await targetFs.scandir(targetDir)).map((stat) => stat.name),
);

for (const item of sourceItems) {
if (item.isFile()) {
if (overwrite || !targetItems.has(item.name)) {
await targetFs.writeFile(
`${targetDir}/${item.name}`,
await sourceFs.readFile(`${sourceDir}/${item.name}`),
);
}
} else {
await cloneTree(
sourceFs,
targetFs,
`${sourceDir}/${item.name}`,
pathTransform,
overwrite,
);
}
}
}

export interface BaseContext {
fs: FileSystem;
localFsRoot?: string;
Expand Down Expand Up @@ -49,8 +86,14 @@ export function useBaseContext(): BaseContext {
new FileSystemAccessFileSystemAdapter(handle),
);
if (createFiles) {
const loaders = await import("@nand2tetris/projects/loader.js");
await loaders.createFiles(newFs);
if (root) {
const loaders = await import("@nand2tetris/projects/loader.js");
await loaders.createFiles(newFs);
} else {
await cloneTree(fs, newFs, "/projects", (path: string) =>
path.replace("/projects", "/").replace(/\/0*(\d+)/, "$1"),
);
}
}
setFs(newFs);
setRoot(handle.name);
Expand Down

0 comments on commit dcfabf2

Please sign in to comment.