diff --git a/eleventy.config.js b/eleventy.config.js index 29d5437a..c556c776 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -7,28 +7,33 @@ export default async function (eleventyConfig) { eleventyConfig.addPassthroughCopy("src/css"); eleventyConfig.addPassthroughCopy("src/img"); eleventyConfig.addPassthroughCopy("src/manifest.json"); - eleventyConfig.addPassthroughCopy("plugins-local"); - eleventyConfig.addWatchTarget("plugins-local"); // Prevent sandbox from being treated as a template eleventyConfig.ignores.add("src/js/plugins/sandbox.html"); - // Local plugin index + // Add watch targets for local plugins + eleventyConfig.addWatchTarget("plugins-local"); + for (const entry of fs.readdirSync("plugins-local", { + withFileTypes: true, + })) { + if (!entry.isSymbolicLink()) continue; + const realPath = fs.realpathSync(path.join("plugins-local", entry.name)); + eleventyConfig.addWatchTarget(`${realPath}/{manifest.json,main.js}`); + } + + // Copy local plugins into build and generate index eleventyConfig.on("eleventy.before", () => { const localPluginsDir = "plugins-local"; - if (!fs.existsSync(localPluginsDir)) return; const listings = []; + fs.mkdirSync("build/plugins-local", { recursive: true }); for (const entry of fs.readdirSync(localPluginsDir, { withFileTypes: true, })) { - if (!entry.isDirectory() && !entry.isSymbolicLink()) continue; + if (!(entry.isDirectory() || entry.isSymbolicLink())) continue; if (entry.name.startsWith(".")) continue; - const manifestPath = path.join( - localPluginsDir, - entry.name, - "manifest.json", - ); - const mainPath = path.join(localPluginsDir, entry.name, "main.js"); + const pluginPath = path.join(localPluginsDir, entry.name); + const manifestPath = path.join(pluginPath, "manifest.json"); + const mainPath = path.join(pluginPath, "main.js"); if (!fs.existsSync(manifestPath) || !fs.existsSync(mainPath)) continue; const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8")); listings.push({ @@ -37,8 +42,11 @@ export default async function (eleventyConfig) { author: manifest.author, description: manifest.description, }); + const destDir = path.join("build/plugins-local", entry.name); + fs.mkdirSync(destDir, { recursive: true }); + fs.copyFileSync(manifestPath, path.join(destDir, "manifest.json")); + fs.copyFileSync(mainPath, path.join(destDir, "main.js")); } - fs.mkdirSync("build/plugins-local", { recursive: true }); fs.writeFileSync( "build/plugins-local/index.json", JSON.stringify(listings, null, 2), diff --git a/package.json b/package.json index 43baf82f..56371071 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "impro", - "version": "0.14.17", + "version": "0.14.18", "type": "module", "scripts": { "start": "rm -rf build && NODE_ENV=development eleventy --serve", diff --git a/src/js/plugins/pluginBridge.js b/src/js/plugins/pluginBridge.js index a70fd3c8..354e8323 100644 --- a/src/js/plugins/pluginBridge.js +++ b/src/js/plugins/pluginBridge.js @@ -256,10 +256,12 @@ export class PluginBridge { async loadPlugin(pluginId, version) { if (this._loadedPlugins.has(pluginId)) return; - const manifest = await this._provider.ensureManifest(pluginId, version); - if (!manifest) { - logger.warn(`failed to load "${pluginId}": invalid manifest`); - return; + let manifest; + try { + manifest = await this._provider.getManifest(pluginId, version); + } catch (error) { + logger.warn(`failed to load "${pluginId}": invalid manifest`, error); + throw new Error("Failed to load plugin manifest"); } let source; try { @@ -269,7 +271,7 @@ export class PluginBridge { `failed to load "${pluginId}": could not fetch main.js`, error, ); - return; + throw new Error("Failed to load plugin source"); } try { const pluginInstance = await PluginInstance.loadFromSource( @@ -287,7 +289,7 @@ export class PluginBridge { return pluginInstance; } catch (error) { logger.error(`"${pluginId}" failed during initialization:`, error); - throw error; + throw new Error("Plugin failed during initialization"); } } diff --git a/src/js/plugins/pluginService.js b/src/js/plugins/pluginService.js index dfef8cf0..e8b6f060 100644 --- a/src/js/plugins/pluginService.js +++ b/src/js/plugins/pluginService.js @@ -227,16 +227,21 @@ export class PluginService extends EventEmitter { async loadPluginsInfo() { const installedPluginsPreference = this._getInstalledPluginsPreference(); - const results = await Promise.all( + this._pluginsInfo = await Promise.all( installedPluginsPreference.map(async (entry) => { const [manifest, listing] = await Promise.all([ - this.sourceProvider.ensureManifest(entry.id, entry.version), + this.sourceProvider + .getManifest(entry.id, entry.version) + .catch(() => null), this.registry.getPluginListing(entry.id).catch(() => null), ]); - if (!manifest) return null; return { id: entry.id, - manifest, + name: manifest?.name ?? entry.id, + description: + manifest?.description ?? "Failed to load plugin manifest", + version: manifest?.version ?? "-", + author: manifest?.author ?? "Unknown", enabled: entry.enabled === true, loaded: this.pluginBridge.isLoaded(entry.id), hasSettings: this.registries.settingTabs.has(entry.id), @@ -244,7 +249,6 @@ export class PluginService extends EventEmitter { }; }), ); - this._pluginsInfo = results.filter((entry) => entry !== null); } async loadEnabledPlugins() { @@ -271,10 +275,9 @@ export class PluginService extends EventEmitter { this._getInstalledPluginsPreference().find( (plugin) => plugin.id === pluginId, ); - return this.sourceProvider.ensureManifest( - pluginId, - installedPluginsPreference?.version, - ); + return this.sourceProvider + .getManifest(pluginId, installedPluginsPreference?.version) + .catch(() => null); } _getInstalledPluginsPreference() { @@ -312,23 +315,33 @@ export class PluginService extends EventEmitter { const installedPluginsPreference = this._getInstalledPluginsPreference(); if (installedPluginsPreference.some((plugin) => plugin.id === pluginId)) return; - const version = listing.local - ? (await this.sourceProvider.getManifest(pluginId)).version - : (await this.sourceProvider.getLiveManifest(pluginId)).version; - await this._setInstalledPluginsPreference([ - ...installedPluginsPreference, - { id: pluginId, version, enabled: true }, - ]); - await this.pluginBridge.loadPlugin(pluginId, version); + let manifest = null; + try { + manifest = listing.local + ? await this.sourceProvider.getManifest(pluginId) + : await this.sourceProvider.getLiveManifest(pluginId); + } catch (e) { + console.error("Failed to fetch manifest", e); + throw new Error("Failed to fetch manifest"); + } + const version = manifest.version; + await this._addPluginPreferenceEntry({ + id: pluginId, + version, + enabled: true, + }); + try { + await this.pluginBridge.loadPlugin(pluginId, version); + } catch (e) { + await this._removePluginPreferenceEntry(pluginId); + throw e; + } } async uninstallPlugin(pluginId) { this.pluginBridge.unloadPlugin(pluginId); - const next = this._getInstalledPluginsPreference().filter( - (plugin) => plugin.id !== pluginId, - ); - await this._setInstalledPluginsPreference(next); - await this._reconcileCache(next); + await this._removePluginPreferenceEntry(pluginId); + await this._reconcileCache(this._getInstalledPluginsPreference()); } async enablePlugin(pluginId) { @@ -410,6 +423,21 @@ export class PluginService extends EventEmitter { return { updated, failed }; } + async _addPluginPreferenceEntry(entry) { + const installedPluginsPreference = this._getInstalledPluginsPreference(); + await this._setInstalledPluginsPreference([ + ...installedPluginsPreference, + entry, + ]); + } + + async _removePluginPreferenceEntry(pluginId) { + const next = this._getInstalledPluginsPreference().filter( + (plugin) => plugin.id !== pluginId, + ); + await this._setInstalledPluginsPreference(next); + } + async _updatePluginPreferenceEntry(pluginId, updateFunc) { const installedPluginsPreference = this._getInstalledPluginsPreference(); if (!installedPluginsPreference.some((plugin) => plugin.id === pluginId)) { diff --git a/src/js/plugins/sourceProvider.js b/src/js/plugins/sourceProvider.js index bcee6624..0050dfa8 100644 --- a/src/js/plugins/sourceProvider.js +++ b/src/js/plugins/sourceProvider.js @@ -27,29 +27,6 @@ export class SourceProvider { this.registry = registry; this.pluginCache = pluginCache; this._fetch = fetchImpl ?? ((...args) => window.fetch(...args)); - this._manifestCache = new Map(); - } - - async ensureManifest(pluginId, version) { - const cacheKey = `${pluginId}@${version ?? ""}`; - if (this._manifestCache.has(cacheKey)) { - return this._manifestCache.get(cacheKey); - } - let listing; - try { - listing = await this._resolveListing(pluginId); - } catch { - return null; - } - try { - const manifest = await this._fetchManifest(pluginId, listing, version); - // Local plugins are served from disk and may change between reads, - // so always refetch — only cache versioned remote manifests. - if (!listing.local) this._manifestCache.set(cacheKey, manifest); - return manifest; - } catch { - return null; - } } async _resolveListing(pluginId) { diff --git a/src/js/views/settings/communityPlugins.view.js b/src/js/views/settings/communityPlugins.view.js index 742bfeb4..89eefc98 100644 --- a/src/js/views/settings/communityPlugins.view.js +++ b/src/js/views/settings/communityPlugins.view.js @@ -65,7 +65,6 @@ class SettingsCommunityPluginsView extends View { { style: wasInstalled ? "default" : "success" }, ); } catch (error) { - state.error = error.message ?? String(error); showToast( wasInstalled ? `Failed to uninstall ${entry.name}` @@ -150,10 +149,13 @@ class SettingsCommunityPluginsView extends View { @click=${() => toggleInstall(entry)} > ${pending - ? html`
` + ? html`${entry.installed + ? "Uninstalling" + : "Installing"} +
` : entry.installed ? "Uninstall" : "Install"} diff --git a/src/js/views/settings/plugins.view.js b/src/js/views/settings/plugins.view.js index 1e82b264..a346a914 100644 --- a/src/js/views/settings/plugins.view.js +++ b/src/js/views/settings/plugins.view.js @@ -42,7 +42,7 @@ class SettingsPluginsView extends View { async function uninstallPlugin(plugin) { const confirmed = await confirm( - `"${plugin.manifest.name}" will be disabled and uninstalled.`, + `"${plugin.name}" will be disabled and uninstalled.`, { title: "Uninstall plugin?", confirmButtonStyle: "danger", @@ -55,7 +55,7 @@ class SettingsPluginsView extends View { try { await pluginService.uninstallPlugin(plugin.id); await loadPlugins(); - showToast(`Uninstalled ${plugin.manifest.name}`); + showToast(`Uninstalled ${plugin.name}`); } finally { state.uninstallingIds.delete(plugin.id); renderPage(); @@ -104,13 +104,13 @@ class SettingsPluginsView extends View { try { const result = await pluginService.updatePlugin(plugin.id); if (result?.updated) { - showToast(`Updated ${plugin.manifest.name} to v${result.version}`, { + showToast(`Updated ${plugin.name} to v${result.version}`, { style: "success", }); await loadPlugins(); } } catch (e) { - showToast(`Failed to update ${plugin.manifest.name}`, { + showToast(`Failed to update ${plugin.name}`, { style: "error", }); } finally { @@ -151,13 +151,13 @@ class SettingsPluginsView extends View { try { if (plugin.enabled) { await pluginService.disablePlugin(plugin.id); - showToast(`Disabled ${plugin.manifest.name}`); + showToast(`Disabled ${plugin.name}`); } else { try { await pluginService.enablePlugin(plugin.id); - showToast(`Enabled ${plugin.manifest.name}`, { style: "success" }); + showToast(`Enabled ${plugin.name}`, { style: "success" }); } catch (e) { - showToast(`Error when loading ${plugin.manifest.name}`, { + showToast(`Error when loading ${plugin.name}`, { style: "error", }); } @@ -281,25 +281,25 @@ class SettingsPluginsView extends View { >
- ${plugin.manifest.name} + ${plugin.name} ${plugin.local ? html`local` : ""}
- ${plugin.manifest.description + ${plugin.description ? html`
- ${plugin.manifest.description} + ${plugin.description}
` : ""}
- Version: ${plugin.manifest.version} + Version: ${plugin.version}
- By ${plugin.manifest.author} + By ${plugin.author}
@@ -320,23 +320,21 @@ class SettingsPluginsView extends View { ? html` ${settingsIconTemplate()} ` : ""} { }); }); -t.describe("SourceProvider.ensureManifest", (it) => { - it("caches manifests across calls with the same version", async () => { - let fetchCount = 0; - const pluginCache = { - async fetch() { - fetchCount++; - return jsonResponse({ id: "alpha", name: "A", version: "1.0.0" }); - }, - }; - const provider = new SourceProvider( - fakeRegistry({ alpha: { id: "alpha", repo: "ow/alpha" } }), - pluginCache, - ); - const first = await provider.ensureManifest("alpha", "1.0.0"); - const second = await provider.ensureManifest("alpha", "1.0.0"); - assertEquals(first.version, "1.0.0"); - assertEquals(second, first); - assertEquals(fetchCount, 1); - }); - - it("refetches when the requested version changes", async () => { - let fetchCount = 0; - const pluginCache = { - async fetch(url) { - fetchCount++; - const match = url.match(/\/([^/]+)\/manifest\.json$/); - const version = match[1]; - return jsonResponse({ id: "alpha", name: "A", version }); - }, - }; - const provider = new SourceProvider( - fakeRegistry({ alpha: { id: "alpha", repo: "ow/alpha" } }), - pluginCache, - ); - const first = await provider.ensureManifest("alpha", "1.0.0"); - const second = await provider.ensureManifest("alpha", "2.0.0"); - assertEquals(first.version, "1.0.0"); - assertEquals(second.version, "2.0.0"); - assertEquals(fetchCount, 2); - }); - - it("returns null when manifest fetch fails", async () => { - const provider = new SourceProvider(fakeRegistry({}), null); - assertEquals(await provider.ensureManifest("missing"), null); - }); - - it("re-fetches local manifests so version changes are picked up", async () => { - let version = "1.0.0"; - const fetchImpl = async () => - jsonResponse({ id: "alpha", name: "A", version }); - const provider = new SourceProvider( - fakeRegistry({ alpha: { id: "alpha", name: "A", local: true } }), - null, - { fetchImpl }, - ); - const first = await provider.ensureManifest("alpha"); - assertEquals(first.version, "1.0.0"); - version = "1.1.0"; - const second = await provider.ensureManifest("alpha"); - assertEquals(second.version, "1.1.0"); - }); -}); - t.describe("SourceProvider.getLiveManifest", (it) => { it("hits raw.githubusercontent.com at main", async () => { const liveUrl =