diff --git a/package.json b/package.json index 8c332ac6..2e56e666 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "impro", - "version": "0.14.12", + "version": "0.14.13", "type": "module", "scripts": { "start": "rm -rf build && NODE_ENV=development eleventy --serve", diff --git a/src/css/style.css b/src/css/style.css index ecf58190..b2fb1f1e 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -7583,3 +7583,36 @@ image-cropper { width: 100%; } } + +.installed-plugins-header { + padding: 16px; + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; +} + +.installed-plugins-header h2 { + font-size: 16px; +} + +.plugin-reload-button { + display: inline-flex; + align-items: center; + justify-content: center; + padding: 0; + background: none; + border: none; + color: inherit; + cursor: pointer; +} + +.plugin-reload-button:disabled { + opacity: 0.5; + cursor: default; +} + +.plugin-reload-button .icon { + width: 24px; + height: 24px; +} diff --git a/src/js/plugins/pluginBridge.js b/src/js/plugins/pluginBridge.js index 0fa10f09..7c707956 100644 --- a/src/js/plugins/pluginBridge.js +++ b/src/js/plugins/pluginBridge.js @@ -349,4 +349,9 @@ export class PluginBridge { instance.unload(); this._loadedPlugins.delete(pluginId); } + + async reloadPlugin(pluginId, version) { + this.unloadPlugin(pluginId); + return this.loadPlugin(pluginId, version); + } } diff --git a/src/js/plugins/pluginRegistry.js b/src/js/plugins/pluginRegistry.js index b3d045c5..a88253a4 100644 --- a/src/js/plugins/pluginRegistry.js +++ b/src/js/plugins/pluginRegistry.js @@ -52,11 +52,4 @@ export class PluginRegistry { const all = await this.getPluginListings(opts); return all.find((listing) => listing.id === id) ?? null; } - - async fetchLiveManifest(listing) { - const url = `https://raw.githubusercontent.com/${listing.repo}/HEAD/manifest.json`; - const response = await this._fetch(url, { cache: "no-store" }); - if (!response.ok) throw new Error(`manifest HTTP ${response.status}`); - return await response.json(); - } } diff --git a/src/js/plugins/pluginService.js b/src/js/plugins/pluginService.js index 6669f24d..4da8ae5f 100644 --- a/src/js/plugins/pluginService.js +++ b/src/js/plugins/pluginService.js @@ -180,6 +180,26 @@ export class PluginService extends EventEmitter { return this._pluginsInfo; } + async reloadPlugins() { + const installedPluginsPreference = this._getInstalledPluginsPreference(); + const results = await Promise.allSettled( + installedPluginsPreference + .filter((entry) => entry.enabled === true) + .map(async (entry) => { + this.pluginBridge.unloadPlugin(entry.id); + try { + await this.pluginBridge.loadPlugin(entry.id, entry.version); + } catch (e) { + await this._setPluginDisabled(entry.id); + throw e; + } + }), + ); + await this.loadPluginsInfo(); + const failure = results.find((result) => result.status === "rejected"); + if (failure) throw failure.reason; + } + async loadPluginsInfo() { const installedPluginsPreference = this._getInstalledPluginsPreference(); const results = await Promise.all( @@ -250,44 +270,15 @@ export class PluginService extends EventEmitter { await this.preferencesProvider.savePreferences(preferences); } - // TODO - // async _applyAutoUpdates() { - // const installedPluginsPreference = this._getInstalledPluginsPreference(); - // if (installedPluginsPreference.length === 0) return installedPluginsPreference; - // let listings; - // try { - // listings = await this.registry.getPluginListings(); - // } catch { - // return installedPluginsPreference; - // } - // const listingById = new Map( - // listings.map((listing) => [listing.id, listing]), - // ); - // const liveVersions = await Promise.all( - // installedPluginsPreference.map(async (entry) => { - // const listing = listingById.get(entry.id); - // if (!listing || listing.local) return null; - // try { - // const manifest = await this.registry.fetchLiveManifest(listing); - // return manifest.version; - // } catch { - // return null; - // } - // }), - // ); - // let changed = false; - // const next = installedPluginsPreference.map((entry, index) => { - // const liveVersion = liveVersions[index]; - // if (!liveVersion) return entry; - // if (compareVersions(liveVersion, entry.version) > 0) { - // changed = true; - // return { ...entry, version: liveVersion }; - // } - // return entry; - // }); - // if (changed) await this._setInstalledPluginsPreference(next); - // return next; - // } + async updatePlugins() { + const installedPluginsPreference = this._getInstalledPluginsPreference(); + return await Promise.all( + installedPluginsPreference.map(async (plugin) => { + const result = await this.updatePlugin(plugin.id); + return { pluginId: plugin.id, result }; + }), + ); + } async _reconcileCache(installed) { const urlLists = await Promise.all( @@ -308,7 +299,7 @@ export class PluginService extends EventEmitter { return; const version = listing.local ? (await this.sourceProvider.getManifest(pluginId)).version - : (await this.registry.fetchLiveManifest(listing)).version; + : (await this.sourceProvider.getLiveManifest(pluginId)).version; await this._setInstalledPluginsPreference([ ...installedPluginsPreference, { id: pluginId, version, enabled: true }, @@ -339,17 +330,10 @@ export class PluginService extends EventEmitter { } async _setPluginEnabled(pluginId) { - const installedPluginsPreference = this._getInstalledPluginsPreference(); - const entry = installedPluginsPreference.find( - (plugin) => plugin.id === pluginId, - ); - if (!entry) throw new Error(`not installed: ${pluginId}`); - if (entry.enabled) return; - await this._setInstalledPluginsPreference( - installedPluginsPreference.map((plugin) => - plugin.id === pluginId ? { ...plugin, enabled: true } : plugin, - ), - ); + await this._updatePluginPreferenceEntry(pluginId, (entry) => ({ + ...entry, + enabled: true, + })); } async disablePlugin(pluginId) { @@ -358,44 +342,49 @@ export class PluginService extends EventEmitter { } async _setPluginDisabled(pluginId) { - const installedPluginsPreference = this._getInstalledPluginsPreference(); - if (!installedPluginsPreference.some((plugin) => plugin.id === pluginId)) - return; - await this._setInstalledPluginsPreference( - installedPluginsPreference.map((plugin) => - plugin.id === pluginId ? { ...plugin, enabled: false } : plugin, - ), - ); + await this._updatePluginPreferenceEntry(pluginId, (entry) => ({ + ...entry, + enabled: false, + })); } - async checkForUpdates(pluginId) { - const listing = await this.registry.getPluginListing(pluginId); - if (!listing || listing.local) return null; + async updatePlugin(pluginId) { const installedPluginsPreference = this._getInstalledPluginsPreference().find( (plugin) => plugin.id === pluginId, ); if (!installedPluginsPreference) return null; - const liveManifest = await this.registry.fetchLiveManifest(listing); + const liveManifest = await this.sourceProvider.getLiveManifest(pluginId); if ( compareVersions( liveManifest.version, installedPluginsPreference.version, ) > 0 ) { - // Re-read after the await and merge by id so a concurrent - // enable/disable/uninstall isn't clobbered. - const next = this._getInstalledPluginsPreference().map((plugin) => - plugin.id === pluginId - ? { ...plugin, version: liveManifest.version } - : plugin, - ); - await this._setInstalledPluginsPreference(next); - return { updated: true, version: liveManifest.version }; + const newVersion = liveManifest.version; + await this._updatePluginPreferenceEntry(pluginId, (entry) => ({ + ...entry, + version: newVersion, + })); + await this.pluginBridge.reloadPlugin(pluginId, newVersion); + return { updated: true, version: newVersion }; } return { updated: false }; } + async _updatePluginPreferenceEntry(pluginId, updateFunc) { + const installedPluginsPreference = this._getInstalledPluginsPreference(); + if (!installedPluginsPreference.some((plugin) => plugin.id === pluginId)) { + throw new Error( + `Tried to update preference for uninstalled plugin: ${pluginId}`, + ); + } + const updated = installedPluginsPreference.map((plugin) => + plugin.id === pluginId ? updateFunc(plugin) : plugin, + ); + await this._setInstalledPluginsPreference(updated); + } + async listRegistryPlugins() { const listings = await this.registry.getPluginListings(); const installedIds = new Set( diff --git a/src/js/plugins/sourceProvider.js b/src/js/plugins/sourceProvider.js index dd11e679..1ba70a07 100644 --- a/src/js/plugins/sourceProvider.js +++ b/src/js/plugins/sourceProvider.js @@ -77,6 +77,21 @@ export class SourceProvider { return parsePluginManifest(pluginId, await response.json()); } + async getLiveManifest(pluginId) { + const listing = await this._resolveListing(pluginId); + if (listing.local) { + const response = await this._fetch( + `/plugins-local/${pluginId}/manifest.json`, + ); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + return parsePluginManifest(pluginId, await response.json()); + } + const url = remoteAssetUrl(listing.repo, "HEAD", "manifest.json"); + const response = await this._fetch(url, { cache: "no-store" }); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + return parsePluginManifest(pluginId, await response.json()); + } + async getSource(pluginId, version) { const listing = await this._resolveListing(pluginId); if (listing.local) { diff --git a/src/js/templates/icons/reloadIcon.template.js b/src/js/templates/icons/reloadIcon.template.js new file mode 100644 index 00000000..85dfc80c --- /dev/null +++ b/src/js/templates/icons/reloadIcon.template.js @@ -0,0 +1,16 @@ +import { html } from "/js/lib/lit-html.js"; + +// Source: https://github.com/halfmage/majesticons/blob/main/line/reload-line.svg +export function reloadIconTemplate() { + return html`
+ + + +
`; +} diff --git a/src/js/views/settings/plugins.view.js b/src/js/views/settings/plugins.view.js index cf19c406..bd77e5fc 100644 --- a/src/js/views/settings/plugins.view.js +++ b/src/js/views/settings/plugins.view.js @@ -7,6 +7,7 @@ import { settingsIconTemplate } from "/js/templates/icons/settingsIcon.template. import { globeIconTemplate } from "/js/templates/icons/globeIcon.template.js"; import { chevronRightIconTemplate } from "/js/templates/icons/chevronRight.template.js"; import { trashCanIconTemplate } from "/js/templates/icons/trashCanIcon.template.js"; +import { reloadIconTemplate } from "/js/templates/icons/reloadIcon.template.js"; import { confirm } from "/js/modals.js"; import { showToast } from "/js/toasts.js"; import "/js/components/toggle-switch.js"; @@ -28,6 +29,7 @@ class SettingsPluginsView extends View { uninstallingIds: new Set(), enablingIds: new Set(), disablingIds: new Set(), + reloading: false, }; async function loadPlugins() { @@ -57,6 +59,21 @@ class SettingsPluginsView extends View { } } + async function reloadPlugins() { + if (state.reloading) return; + state.reloading = true; + renderPage(); + try { + await pluginService.reloadPlugins(); + showToast("Reloaded plugins"); + } catch (e) { + showToast("Failed to reload plugins", { style: "error" }); + } finally { + state.reloading = false; + renderPage(); + } + } + async function togglePlugin(plugin) { const pendingSet = plugin.enabled ? state.disablingIds @@ -72,7 +89,9 @@ class SettingsPluginsView extends View { await pluginService.enablePlugin(plugin.id); showToast(`Enabled ${plugin.manifest.name}`, { style: "success" }); } catch (e) { - showToast("Plugin failed to load", { style: "error" }); + showToast(`Error when loading ${plugin.manifest.name}`, { + style: "error", + }); } } await loadPlugins(); @@ -124,6 +143,17 @@ class SettingsPluginsView extends View { >${chevronRightIconTemplate()} +
+

Installed plugins

+ +
${!pluginsInfo ? html`

Loading…

` : pluginsInfo.length === 0 diff --git a/tests/unit/specs/pluginRegistry.test.js b/tests/unit/specs/pluginRegistry.test.js index 63809500..867524fb 100644 --- a/tests/unit/specs/pluginRegistry.test.js +++ b/tests/unit/specs/pluginRegistry.test.js @@ -149,18 +149,4 @@ t.describe("PluginRegistry.getPluginListing", (it) => { }); }); -t.describe("PluginRegistry.fetchLiveManifest", (it) => { - it("hits raw.githubusercontent.com at HEAD", async () => { - const liveUrl = - "https://raw.githubusercontent.com/ow/alpha/HEAD/manifest.json"; - const { fetchImpl, calls } = fakeFetcher({ - [liveUrl]: { id: "alpha", version: "9.9.9" }, - }); - const registry = new PluginRegistry(REGISTRY_URL, { fetchImpl }); - const manifest = await registry.fetchLiveManifest({ repo: "ow/alpha" }); - assertEquals(manifest.version, "9.9.9"); - assert(calls.includes(liveUrl)); - }); -}); - await t.run(); diff --git a/tests/unit/specs/sourceProvider.test.js b/tests/unit/specs/sourceProvider.test.js index 1de40e21..c2ba4f61 100644 --- a/tests/unit/specs/sourceProvider.test.js +++ b/tests/unit/specs/sourceProvider.test.js @@ -227,4 +227,24 @@ t.describe("SourceProvider.ensureManifest", (it) => { }); }); +t.describe("SourceProvider.getLiveManifest", (it) => { + it("hits raw.githubusercontent.com at HEAD", async () => { + const liveUrl = + "https://raw.githubusercontent.com/ow/alpha/HEAD/manifest.json"; + let fetchedUrl = null; + const fetchImpl = async (url) => { + fetchedUrl = url; + return jsonResponse({ id: "alpha", name: "Alpha", version: "9.9.9" }); + }; + const provider = new SourceProvider( + fakeRegistry({ alpha: { id: "alpha", repo: "ow/alpha" } }), + null, + { fetchImpl }, + ); + const manifest = await provider.getLiveManifest("alpha"); + assertEquals(manifest.version, "9.9.9"); + assertEquals(fetchedUrl, liveUrl); + }); +}); + await t.run();