Skip to content

Commit

Permalink
feat: backup plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
bajrangCoder committed Nov 4, 2024
1 parent 22a7d11 commit b0367a4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lang/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
"server port": "Server port",
"preview settings": "Preview settings",
"preview settings note": "If preview port and server port are different, app will not start server and it will instead open https://<host>:<preview port> in browser or in-app browser. This is useful when you are running a server somewhere else.",
"backup/restore note": "It will only backup your settings, custom theme and key bindings. It will not backup your FTP/SFTP.",
"backup/restore note": "It will only backup your settings, custom theme, installed plugins and key bindings. It will not backup your FTP/SFTP or App state.",
"host": "Host",
"retry ftp/sftp when fail": "Retry ftp/sftp when fail",
"more": "More",
Expand Down
58 changes: 56 additions & 2 deletions src/settings/backupRestore.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import settingsPage from "components/settingsPage";
import toast from "components/toast";
import alert from "dialogs/alert";
import fsOperation from "fileSystem";
import constants from "lib/constants";
import appSettings from "lib/settings";
import FileBrowser from "pages/fileBrowser";
import Uri from "utils/Uri";
import Url from "utils/Url";
import helpers from "utils/helpers";

function backupRestore() {
const title =
Expand Down Expand Up @@ -46,6 +49,9 @@ function backupRestore() {
try {
const settings = appSettings.value;
const keyBindings = await fsOperation(KEYBINDING_FILE).readFile("json");
const installedPlugins = (
await fsOperation(window.PLUGIN_DIR).lsDir()
).map((plugin) => plugin.name);

const { url } = await FileBrowser("folder", strings["select folder"]);

Expand All @@ -68,6 +74,7 @@ function backupRestore() {
const backupString = JSON.stringify({
settings,
keyBindings,
installedPlugins,
});

await backupFileFS.writeFile(backupString);
Expand Down Expand Up @@ -104,14 +111,61 @@ backupRestore.restore = async function (url) {
backup = JSON.parse(backup);
} catch (error) {
alert(strings.error.toUpperCase(), strings["invalid backup file"]);
return;
}

try {
const text = JSON.stringify(backup.keyBindings, undefined, 2);
await fsOperation(window.KEYBINDING_FILE).writeFile(text);
} catch (error) {}
} catch (error) {
console.error("Error restoring key bindings:", error);
}

const { settings, installedPlugins } = backup;

const { default: installPlugin } = await import("lib/installPlugin");

// Restore plugins
if (Array.isArray(installedPlugins)) {
for (const id of installedPlugins) {
try {
if (!id) continue;
const pluginUrl = Url.join(constants.API_BASE, `plugin/${id}`);
const remotePlugin = await fsOperation(pluginUrl)
.readFile("json")
.catch(() => null);

if (remotePlugin) {
let purchaseToken = null;
if (Number.parseFloat(remotePlugin.price) > 0) {
try {
const [product] = await helpers.promisify(iap.getProducts, [
remotePlugin.sku,
]);
if (product) {
async function getPurchase(sku) {
const purchases = await helpers.promisify(iap.getPurchases);
const purchase = purchases.find((p) =>
p.productIds.includes(sku),
);
return purchase;
}
const purchase = await getPurchase(product.productId);
purchaseToken = purchase?.purchaseToken;
}
} catch (error) {
helpers.error(error);
}
}
toast("Restoring plugins", 3000);
await installPlugin(id, remotePlugin.name, purchaseToken);
}
} catch (error) {
console.error(`Error restoring plugin ${id}:`, error);
}
}
}

const { settings } = backup;
await appSettings.update(settings);
location.reload();
} catch (err) {
Expand Down

0 comments on commit b0367a4

Please sign in to comment.