diff --git a/src/providers/plugins.ts b/src/providers/plugins.ts index 68d418a..185b78a 100644 --- a/src/providers/plugins.ts +++ b/src/providers/plugins.ts @@ -81,3 +81,7 @@ export const modifyCommunityPlugins = async ( childLogger.debug(`Modify action performed`) } + +export const deduplicatePlugins = (plugins: Plugin[], stagePlugin: Plugin) => { + return plugins.filter((plugin) => plugin.id !== stagePlugin.id) +} diff --git a/src/services/install.ts b/src/services/install.ts index 6472909..a980a1b 100644 --- a/src/services/install.ts +++ b/src/services/install.ts @@ -5,7 +5,10 @@ import { getPluginVersion, handleExceedRateLimitError, } from '../providers/github' -import { modifyCommunityPlugins } from '../providers/plugins' +import { + deduplicatePlugins, + modifyCommunityPlugins, +} from '../providers/plugins' import { getSelectedVaults, mapVaultsIteratorItem } from '../providers/vaults' import { FactoryFlagsWithVaults, @@ -64,7 +67,11 @@ const installVaultIterator: InstallCommandIterator = async (item) => { await modifyCommunityPlugins(stagePlugin, vault.path, 'enable') } - const updatedPlugins = new Set([...config.plugins, stagePlugin]) + const updatedPlugins = new Set([ + ...deduplicatePlugins(config.plugins, stagePlugin), + { ...stagePlugin, version }, + ]) + const updatedConfig = { ...config, plugins: [...updatedPlugins] } writeConfig(updatedConfig, flags.config) diff --git a/src/services/stats.ts b/src/services/stats.ts index 5602c15..837441d 100644 --- a/src/services/stats.ts +++ b/src/services/stats.ts @@ -54,8 +54,10 @@ const statsVaultIterator: StatsCommandIterator = async (item) => { if (await isPluginInstalled(stagePlugin.id, vault.path)) { stats.installedPlugins += 1 installedPlugins[pluginNameWithSize] = [ - ...(installedPlugins[pluginNameWithSize] || []), - vault.name, + ...new Set([ + ...(installedPlugins[pluginNameWithSize] || []), + vault.name, + ]), ] } } @@ -91,7 +93,7 @@ const action = async ( if (!error) { result.success = true - const sortedInstalledPlugins = Object.entries(installedPlugins) + const sortedInstalledPlugins = Object.entries(result.installedPlugins) .sort(([keyA], [keyB]) => keyA.localeCompare(keyB)) .reduce((acc, [key, value]) => { acc[key] = value diff --git a/src/services/uninstall.ts b/src/services/uninstall.ts index cdc8a52..aa731d6 100644 --- a/src/services/uninstall.ts +++ b/src/services/uninstall.ts @@ -1,6 +1,6 @@ import { each } from 'async' import { isPluginInstalled } from 'obsidian-utils' -import { removePluginDir } from '../providers/plugins' +import { deduplicatePlugins, removePluginDir } from '../providers/plugins' import { getSelectedVaults, mapVaultsIteratorItem } from '../providers/vaults' import { loadConfig, Plugin, writeConfig } from '../services/config' import { @@ -36,10 +36,11 @@ const uninstallVaultIterator: UninstallCommandIterator = async (item) => { try { await removePluginDir(stagePlugin.id, vault.path) - const updatedPlugins = new Set( - config.plugins.filter((plugin) => plugin.id !== stagePlugin.id), - ) + const updatedPlugins = new Set([ + ...deduplicatePlugins(config.plugins, stagePlugin), + ]) const updatedConfig = { ...config, plugins: [...updatedPlugins] } + writeConfig(updatedConfig, flags.config) childLogger.info(`Uninstalled plugin`) @@ -66,15 +67,10 @@ const action = async ( const config = await loadConfig(flags.config) const selectedVaults = await getSelectedVaults(flags.path) - // Check if pluginId is provided and install only that plugin - const configWithPlugins = args.pluginId - ? { plugins: [{ id: args.pluginId }] } - : config - const items = mapVaultsIteratorItem< UninstallArgs, FactoryFlagsWithVaults - >(selectedVaults, configWithPlugins, flags, args) + >(selectedVaults, config, flags, args) const uninstallCommandCallback: UninstallCommandCallback = (error) => { const result: UninstallCommandCallbackResult = { diff --git a/src/types/commands.d.ts b/src/types/commands.d.ts index ba5c809..62ea051 100644 --- a/src/types/commands.d.ts +++ b/src/types/commands.d.ts @@ -124,11 +124,11 @@ export type StatsCommandIterator = ( ) => Promise export type StatsCommandCallbackResult = CommandCallbackBaseResult & { - totalStats?: { + totalStats: { totalVaults: number totalPlugins: number } - installedPlugins?: InstalledPlugins + installedPlugins: InstalledPlugins } export type StatsCommandCallback = (