Skip to content

Commit

Permalink
Added script for getting backup from official IDE, small improvements…
Browse files Browse the repository at this point in the history
… to Action doc
  • Loading branch information
Lorthiz committed Oct 20, 2024
1 parent 7ded881 commit 19d11aa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/GlobalActions/RestoreBackupConfigFromBackup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {FileSystemFilesCollector} from "../Components/FileTree/FileSystemVisitor

/**
* THIS ACTION CLEANS AND REPLACES THE WHOLE LOCAL STORE!
* To create a backup check instructions in `src/Utils/GetBackupFromOfficialKaitaiIDE.js`
*
* 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}`
* It needs to be a FLAT structure - exactly how LocalStorage is working now!
* Then just press the `Restore old config` button on Info Panel view to start a restore.
* Once it's done just refresh the page to see the changes.
*
* Your `public/Backup` folder might look like this:
* > _backup.json
Expand Down
57 changes: 57 additions & 0 deletions src/Utils/GetBackupFromOfficialKaitaiIDE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* DEV SCRIPT, for backing up files you already have on: https://ide.kaitai.io
* Run this script in the dev console of `https:/ide.kaitai.io` it will download for you all the files.
* Remember to check file names in case you were downloading the file that already exist in your `Downloads` directory.
* Then you can put them under path: `public/Backup/{YOUR FILES GO HERE} of this project.
* That will allow you to use `Restore old config` button in development mode to reset LocalStorage to that backup.
* More info in file `src/GlobalActions/RestoreBackupConfigFromBackup.ts`
*
* */

(async () => {
const downloadFile = (data, filename) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.style.display = "none";
const blob = new Blob([data], {type: "octet/stream"});
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}

const collectFileNames = (root) => {
const collectedFiles = [];
const visitNode = (node) => {
switch (node.type) {
case "file": {
collectedFiles.push(node.fn);
break;
}
case "folder": {
Object.entries(node.children || {})
.forEach(([key, child]) => visitNode(child));
break;
}
}
}
visitNode(root);
return collectedFiles;
}

const localforage = require("localforage");
const filesRoot = await localforage.getItem("fs_files")

const collectedFileNames = collectFileNames(filesRoot)

console.log(filesRoot)
console.log(collectedFileNames)

downloadFile(filesRoot, "_backup.json")
for (const fileName of collectedFileNames) {
const fileContent = await localforage.getItem(`fs_file[${fileName}]`)
downloadFile(fileContent, fileName)
}
})()

0 comments on commit 19d11aa

Please sign in to comment.