Skip to content

Commit

Permalink
fix: installing plugin and its state clean
Browse files Browse the repository at this point in the history
  • Loading branch information
bajrangCoder committed Nov 3, 2024
1 parent d69054c commit 22a7d11
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
40 changes: 22 additions & 18 deletions src/lib/installPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,37 @@ export default async function installPlugin(id, name, purchaseToken) {
}

const promises = Object.keys(zip.files).map(async (file) => {
let correctFile = file;
if (/\\/.test(correctFile)) {
correctFile = correctFile.replace(/\\/g, "/");
}
try {
let correctFile = file;
if (/\\/.test(correctFile)) {
correctFile = correctFile.replace(/\\/g, "/");
}

const fileUrl = Url.join(pluginDir, correctFile);
if (!state.exists(correctFile)) {
await createFileRecursive(pluginDir, correctFile);
}
const fileUrl = Url.join(pluginDir, correctFile);

if (correctFile.endsWith("/")) return;
if (!state.exists(correctFile)) {
await createFileRecursive(pluginDir, correctFile);
}

let data = await zip.files[file].async("ArrayBuffer");
// Skip directories
if (correctFile.endsWith("/")) return;

if (file === "plugin.json") {
data = JSON.stringify(pluginJson);
}
let data = await zip.files[file].async("ArrayBuffer");

if (!(await state.isUpdated(correctFile, data))) {
if (file === "plugin.json") {
data = JSON.stringify(pluginJson);
}

if (!(await state.isUpdated(correctFile, data))) return;
await fsOperation(fileUrl).writeFile(data);
return;
} catch (error) {
console.error(`Error processing file ${file}:`, error);
}

await fsOperation(fileUrl).writeFile(data);
return;
});

await Promise.all(promises);
// Wait for all files to be processed
await Promise.allSettled(promises);
await loadPlugin(id, true);
await state.save();
deleteRedundantFiles(pluginDir, state);
Expand Down
42 changes: 26 additions & 16 deletions src/lib/installState.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,29 @@ export default class InstallState {
* @returns
*/
static async new(id) {
const state = new InstallState();
state.id = await checksumText(id);
state.updatedStore = {};
try {
const state = new InstallState();
state.id = await checksumText(id);
state.updatedStore = {};

if (!(await fsOperation(INSTALL_STATE_STORAGE).exists())) {
await fsOperation(DATA_STORAGE).createDirectory(".install-state");
}
if (!(await fsOperation(INSTALL_STATE_STORAGE).exists())) {
await fsOperation(DATA_STORAGE).createDirectory(".install-state");
}

state.storeUrl = Url.join(INSTALL_STATE_STORAGE, state.id);
if (await fsOperation(state.storeUrl).exists()) {
state.store = JSON.parse(
await fsOperation(state.storeUrl).readFile("utf-8"),
);
} else {
state.store = {};
await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id);
}
state.storeUrl = Url.join(INSTALL_STATE_STORAGE, state.id);
if (await fsOperation(state.storeUrl).exists()) {
state.store = JSON.parse(
await fsOperation(state.storeUrl).readFile("utf-8"),
);
} else {
state.store = {};
await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id);
}

return state;
return state;
} catch (e) {
console.error(e);
}
}

/**
Expand Down Expand Up @@ -72,6 +76,12 @@ export default class InstallState {
JSON.stringify(this.updatedStore),
);
}

async delete(url) {
if (await fsOperation(url).exists()) {
await fsOperation(url).delete();
}
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/pages/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import purchaseListener from "handlers/purchase";
import actionStack from "lib/actionStack";
import constants from "lib/constants";
import installPlugin from "lib/installPlugin";
import InstallState from "lib/installState";
import settings from "lib/settings";
import markdownIt from "markdown-it";
import MarkdownItGitHubAlerts from "markdown-it-github-alerts";
Expand Down Expand Up @@ -183,7 +184,12 @@ export default async function PluginInclude(
async function uninstall() {
try {
const pluginDir = Url.join(PLUGIN_DIR, plugin.id);
await Promise.all([loadAd(this), fsOperation(pluginDir).delete()]);
const state = await InstallState.new(plugin.id);
await Promise.all([
loadAd(this),
fsOperation(pluginDir).delete(),
state.delete(state.storeUrl),
]);
acode.unmountPlugin(plugin.id);
if (onUninstall) onUninstall(plugin.id);
installed = false;
Expand Down
8 changes: 7 additions & 1 deletion src/sidebarApps/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Sidebar from "components/sidebar";
import select from "dialogs/select";
import fsOperation from "fileSystem";
import constants from "lib/constants";
import InstallState from "lib/installState";
import settings from "lib/settings";
import plugin from "pages/plugin";
import Url from "utils/Url";
Expand Down Expand Up @@ -360,7 +361,12 @@ async function loadAd(el) {
async function uninstall(id) {
try {
const pluginDir = Url.join(PLUGIN_DIR, id);
await Promise.all([loadAd(this), fsOperation(pluginDir).delete()]);
const state = await InstallState.new(id);
await Promise.all([
loadAd(this),
fsOperation(pluginDir).delete(),
state.delete(state.storeUrl),
]);
acode.unmountPlugin(id);
if (!IS_FREE_VERSION && (await window.iad?.isLoaded())) {
window.iad.show();
Expand Down

0 comments on commit 22a7d11

Please sign in to comment.