Skip to content

Commit

Permalink
Added proper way to restore old configs, only appearing in local envi…
Browse files Browse the repository at this point in the history
…ronment
  • Loading branch information
Lorthiz committed Oct 20, 2024
1 parent 67680e8 commit 2c6a448
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ export const prepareContextMenuOptions = (e: MouseEvent, item: TreeNodeDisplay):
theme: "flat dark",
clickCloseOnOutside: true,
items: [
// {
// label: "Restore old files",
// onClick: () => RestoreOldFileTreeAction(),
// customClass: "context-menu-item",
// hidden: item.storeId === FILE_SYSTEM_TYPE_KAITAI || item.fullPath !== ""
// },
FileTreeCtxActionOpenKsy(item),
FileTreeCtxActionOpenBinFile(item),
FileTreeCtxActionOpenDirectory(item),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {FileSystemItem, ITEM_MODE_DIRECTORY, ITEM_MODE_FILE} from "../FileSystemsTypes";

export class FsItemPathsCollector {
public static collectFiles(fsItem: FileSystemItem): string[] {
return new FsItemPathsCollector().collectFiles(fsItem);
export class FileSystemFilesCollector {
public static collectFileNames(fsItem: FileSystemItem): string[] {
return new FileSystemFilesCollector().collectFiles(fsItem);
}

private readonly collectedPaths: string[];
Expand Down
6 changes: 3 additions & 3 deletions src/Components/FileTree/Utils/FsItemHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {FileSystemItem, ITEM_MODE_DIRECTORY, ITEM_MODE_FILE} from "../FileSystemsTypes";
import {ArrayUtils} from "../../../Utils/ArrayUtils";
import {FsItemPathsCollector} from "../FileSystemVisitors/FsItemPathsCollector";
import {FileSystemFilesCollector} from "../FileSystemVisitors/FileSystemFilesCollector";

export interface FileSystemItemPathInfo {
isRoot: boolean;
Expand Down Expand Up @@ -36,13 +36,13 @@ export class FsItemHelper {
const isDeletingRoot = filePath.length === 0;

if (isDeletingRoot) {
const filesInRoot = FsItemPathsCollector.collectFiles(root);
const filesInRoot = FileSystemFilesCollector.collectFileNames(root);
Object.keys(root.children || {}).forEach(key => delete root.children[key]);
return filesInRoot;
} else {
const filePathParts = filePath.split("/");
let nodeToDelete = FsItemHelper.deleteNodeFromRoot(root, filePathParts);
return FsItemPathsCollector.collectFiles(nodeToDelete);
return FileSystemFilesCollector.collectFileNames(nodeToDelete);
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/Components/InfoPanel/InfoPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {useIdeSettingsStore} from "../../Stores/IdeSettingsStore";
import {ExportedValueUtils} from "../../Utils/ExportedValueUtils";
import {useWelcomeModalStore} from "../Modals/WelcomeModal/WelcomeModalStore";
import TextButton from "../UtilComponents/TextButton.vue";
import {RestoreBackupConfigFromBackup} from "../../GlobalActions/RestoreBackupConfigFromBackup";
const currentBinaryFileStore = useCurrentBinaryFileStore();
const hexViewerConfigStore = useHexViewerConfigStore();
Expand All @@ -34,6 +35,7 @@ const selectionPath = computed(() => {
? (currentBinaryFileStore.parsedFileFlatInfo.leafs[rangeIndex].path || []).join("/")
: "";
});
const isLocalEnv = process.env.NODE_ENV === 'development';
const about = () => {
useWelcomeModalStore().open();
Expand Down Expand Up @@ -61,6 +63,7 @@ const about = () => {
<EmojiCheckbox checked-emoji="" un-checked-emoji="" :state="ideSettingsStore.generateOnlyMainFile"
:toggle="ideSettingsStore.setGenerateOnlyMainFile" text="Generate only main file"/>
<TextButton :click="about" text="about webide"/>
<TextButton :click="RestoreBackupConfigFromBackup" text="Restore old config" v-if="isLocalEnv"/>
</div>
</template>

Expand Down
34 changes: 34 additions & 0 deletions src/GlobalActions/RestoreBackupConfigFromBackup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {FileActionsWrapper} from "../Utils/Files/FileActionsWrapper";
import {LocalForageForLocalStorageWrapper} from "../Components/FileTree/Utils/LocalForageForLocalStorageWrapper";
import {FileSystemItem} from "../Components/FileTree/FileSystemsTypes";
import {FileSystemFilesCollector} from "../Components/FileTree/FileSystemVisitors/FileSystemFilesCollector";

/**
* This is debug Action, you can add your config(value of `fs_files` key from localForage) in `public/Backup/backup.json` folder;
* Then put all the files also in that location: `public/Backup/{FILE_NAME}`
* Then just press the `Restore old config` button on Info Panel view to start a restore.
* THIS ACTION CLEARS THE WHOLE LOCAL STORE!
*/
export const RestoreBackupConfigFromBackup = async () => {
console.log("[RestoreBackupConfigFromBackup]: Restore action started.");
const backupConfig: FileSystemItem = await fetch("Backup/_backup.json").then(resp => resp.json());
if (!backupConfig) {
console.log("[RestoreBackupConfigFromBackup]: Backup config not found, restore action stopped");
return;
}
const filesToRestore = FileSystemFilesCollector.collectFileNames(backupConfig);

console.log("[RestoreBackupConfigFromBackup]: Config found.", backupConfig);
console.log(`[RestoreBackupConfigFromBackup]: Found ${filesToRestore.length} files to restore.`);
await LocalForageForLocalStorageWrapper.clear();
console.log("[RestoreBackupConfigFromBackup]: Old config erased.");
await LocalForageForLocalStorageWrapper.saveFsItem("fs_files", backupConfig);
console.log("[RestoreBackupConfigFromBackup]: Backup file tree saved under fs_files");

for (const fileName of filesToRestore) {
const fileContent = await FileActionsWrapper.fetchFileFromServer(`OldFiles/${fileName}`);
await LocalForageForLocalStorageWrapper.saveFile(`fs_file[${fileName}]`, fileContent);
console.log(`[RestoreBackupConfigFromBackup]: File restored and saved: ${fileName}`);
}
console.log("[RestoreBackupConfigFromBackup]: Restore action completed");
};

0 comments on commit 2c6a448

Please sign in to comment.