Skip to content

Commit 22a7d11

Browse files
committed
fix: installing plugin and its state clean
1 parent d69054c commit 22a7d11

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

src/lib/installPlugin.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,37 @@ export default async function installPlugin(id, name, purchaseToken) {
7878
}
7979

8080
const promises = Object.keys(zip.files).map(async (file) => {
81-
let correctFile = file;
82-
if (/\\/.test(correctFile)) {
83-
correctFile = correctFile.replace(/\\/g, "/");
84-
}
81+
try {
82+
let correctFile = file;
83+
if (/\\/.test(correctFile)) {
84+
correctFile = correctFile.replace(/\\/g, "/");
85+
}
8586

86-
const fileUrl = Url.join(pluginDir, correctFile);
87-
if (!state.exists(correctFile)) {
88-
await createFileRecursive(pluginDir, correctFile);
89-
}
87+
const fileUrl = Url.join(pluginDir, correctFile);
9088

91-
if (correctFile.endsWith("/")) return;
89+
if (!state.exists(correctFile)) {
90+
await createFileRecursive(pluginDir, correctFile);
91+
}
9292

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

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

99-
if (!(await state.isUpdated(correctFile, data))) {
98+
if (file === "plugin.json") {
99+
data = JSON.stringify(pluginJson);
100+
}
101+
102+
if (!(await state.isUpdated(correctFile, data))) return;
103+
await fsOperation(fileUrl).writeFile(data);
100104
return;
105+
} catch (error) {
106+
console.error(`Error processing file ${file}:`, error);
101107
}
102-
103-
await fsOperation(fileUrl).writeFile(data);
104-
return;
105108
});
106109

107-
await Promise.all(promises);
110+
// Wait for all files to be processed
111+
await Promise.allSettled(promises);
108112
await loadPlugin(id, true);
109113
await state.save();
110114
deleteRedundantFiles(pluginDir, state);

src/lib/installState.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@ export default class InstallState {
1515
* @returns
1616
*/
1717
static async new(id) {
18-
const state = new InstallState();
19-
state.id = await checksumText(id);
20-
state.updatedStore = {};
18+
try {
19+
const state = new InstallState();
20+
state.id = await checksumText(id);
21+
state.updatedStore = {};
2122

22-
if (!(await fsOperation(INSTALL_STATE_STORAGE).exists())) {
23-
await fsOperation(DATA_STORAGE).createDirectory(".install-state");
24-
}
23+
if (!(await fsOperation(INSTALL_STATE_STORAGE).exists())) {
24+
await fsOperation(DATA_STORAGE).createDirectory(".install-state");
25+
}
2526

26-
state.storeUrl = Url.join(INSTALL_STATE_STORAGE, state.id);
27-
if (await fsOperation(state.storeUrl).exists()) {
28-
state.store = JSON.parse(
29-
await fsOperation(state.storeUrl).readFile("utf-8"),
30-
);
31-
} else {
32-
state.store = {};
33-
await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id);
34-
}
27+
state.storeUrl = Url.join(INSTALL_STATE_STORAGE, state.id);
28+
if (await fsOperation(state.storeUrl).exists()) {
29+
state.store = JSON.parse(
30+
await fsOperation(state.storeUrl).readFile("utf-8"),
31+
);
32+
} else {
33+
state.store = {};
34+
await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id);
35+
}
3536

36-
return state;
37+
return state;
38+
} catch (e) {
39+
console.error(e);
40+
}
3741
}
3842

3943
/**
@@ -72,6 +76,12 @@ export default class InstallState {
7276
JSON.stringify(this.updatedStore),
7377
);
7478
}
79+
80+
async delete(url) {
81+
if (await fsOperation(url).exists()) {
82+
await fsOperation(url).delete();
83+
}
84+
}
7585
}
7686

7787
/**

src/pages/plugin/plugin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import purchaseListener from "handlers/purchase";
88
import actionStack from "lib/actionStack";
99
import constants from "lib/constants";
1010
import installPlugin from "lib/installPlugin";
11+
import InstallState from "lib/installState";
1112
import settings from "lib/settings";
1213
import markdownIt from "markdown-it";
1314
import MarkdownItGitHubAlerts from "markdown-it-github-alerts";
@@ -183,7 +184,12 @@ export default async function PluginInclude(
183184
async function uninstall() {
184185
try {
185186
const pluginDir = Url.join(PLUGIN_DIR, plugin.id);
186-
await Promise.all([loadAd(this), fsOperation(pluginDir).delete()]);
187+
const state = await InstallState.new(plugin.id);
188+
await Promise.all([
189+
loadAd(this),
190+
fsOperation(pluginDir).delete(),
191+
state.delete(state.storeUrl),
192+
]);
187193
acode.unmountPlugin(plugin.id);
188194
if (onUninstall) onUninstall(plugin.id);
189195
installed = false;

src/sidebarApps/extensions/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Sidebar from "components/sidebar";
55
import select from "dialogs/select";
66
import fsOperation from "fileSystem";
77
import constants from "lib/constants";
8+
import InstallState from "lib/installState";
89
import settings from "lib/settings";
910
import plugin from "pages/plugin";
1011
import Url from "utils/Url";
@@ -360,7 +361,12 @@ async function loadAd(el) {
360361
async function uninstall(id) {
361362
try {
362363
const pluginDir = Url.join(PLUGIN_DIR, id);
363-
await Promise.all([loadAd(this), fsOperation(pluginDir).delete()]);
364+
const state = await InstallState.new(id);
365+
await Promise.all([
366+
loadAd(this),
367+
fsOperation(pluginDir).delete(),
368+
state.delete(state.storeUrl),
369+
]);
364370
acode.unmountPlugin(id);
365371
if (!IS_FREE_VERSION && (await window.iad?.isLoaded())) {
366372
window.iad.show();

0 commit comments

Comments
 (0)