Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fs UI bug fixes #464

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions components/src/file_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
}
14 changes: 12 additions & 2 deletions components/src/stores/base.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useState,
} from "react";
import { useDialog } from "../dialog.js";
import { cloneTree } from "../file_utils.js";
import {
FileSystemAccessFileSystemAdapter,
openNand2TetrisDirectory,
Expand Down Expand Up @@ -49,8 +50,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 Expand Up @@ -106,6 +113,9 @@ export function useBaseContext(): BaseContext {
async (force = false, createFiles = false) => {
if (!canUpgradeFs || (root && !force)) return;
const handler = await openNand2TetrisDirectory();
if (root) {
await removeLocalAdapterFromIndexedDB();
}
const adapter = await createAndStoreLocalAdapterInIndexedDB(handler);
await setLocalFs(adapter, createFiles);
},
Expand Down
2 changes: 2 additions & 0 deletions components/src/stores/chip.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ export function makeChipStore(

if (hdlProjects.length > 0) {
await actions.setProject(sortedNames[0]);
} else {
dispatch.current({ action: "setChips", payload: [] });
}

dispatch.current({ action: "clearChip" });
Expand Down
2 changes: 1 addition & 1 deletion projects/src/full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const resetTests = async (fs: FileSystem, projects?: number[]) => {
};

export const createFiles = async (fs: FileSystem) => {
await reset(fs, ProjectFiles);
await reset(fs, ProjectFiles, "/", false);
};

export const Assignments = {
Expand Down
12 changes: 10 additions & 2 deletions projects/src/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ export async function resetBySuffix(
}
}

export async function reset(fs: FileSystem, tree: Tree, base?: string) {
export async function reset(
fs: FileSystem,
tree: Tree,
base?: string,
override = true,
) {
const items = (await fs.scandir(base ?? "/")).map((item) => item.name);
for (const [key, value] of Object.entries(tree)) {
const path = `${base ? `${base}/` : ""}${key}`;
if (typeof value === "string") {
await fs.writeFile(path, value);
if (override || !items.includes(key)) {
await fs.writeFile(path, value);
}
} else {
await fs.mkdir(path);
await reset(fs, value as Tree, path);
Expand Down
1 change: 0 additions & 1 deletion web/src/shell/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import "../pico/button-group.scss";
import "../pico/property.scss";
import { TrackingDisclosure } from "../tracking";
import { getVersion, setVersion } from "../versions";
// import { useDialog } from "./dialog";
DavidSouther marked this conversation as resolved.
Show resolved Hide resolved

const showUpgradeFs = true;

Expand Down
Loading