From f5c0766cd21b48fd97926bc68ec08a3fa0c9ae2f Mon Sep 17 00:00:00 2001 From: Neta London Date: Tue, 1 Oct 2024 13:00:16 +0300 Subject: [PATCH] CR --- components/src/file_utils.ts | 37 ++++++++++++++++++++++++++ components/src/stores/base.context.ts | 38 +-------------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/components/src/file_utils.ts b/components/src/file_utils.ts index bf4cf39e4..9df6d7bf1 100644 --- a/components/src/file_utils.ts +++ b/components/src/file_utils.ts @@ -42,3 +42,40 @@ export function sortFiles(files: Stats[]) { } }); } + +export 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, + ); + } + } +} diff --git a/components/src/stores/base.context.ts b/components/src/stores/base.context.ts index 1bef869fa..10fda269e 100644 --- a/components/src/stores/base.context.ts +++ b/components/src/stores/base.context.ts @@ -11,6 +11,7 @@ import { useState, } from "react"; import { useDialog } from "../dialog.js"; +import { cloneTree } from "../file_utils.js"; import { FileSystemAccessFileSystemAdapter, openNand2TetrisDirectory, @@ -21,43 +22,6 @@ 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;