Skip to content

Commit

Permalink
feat: add deduplication of plugins in install and uninstall processes
Browse files Browse the repository at this point in the history
  • Loading branch information
msudgh committed Jan 2, 2025
1 parent a70c1cf commit a8d82a3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/providers/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 9 additions & 2 deletions src/services/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/services/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]),
]
}
}
Expand Down Expand Up @@ -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<InstalledPlugins>((acc, [key, value]) => {
acc[key] = value
Expand Down
16 changes: 6 additions & 10 deletions src/services/uninstall.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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`)
Expand All @@ -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<UninstallFlags>
>(selectedVaults, configWithPlugins, flags, args)
>(selectedVaults, config, flags, args)

const uninstallCommandCallback: UninstallCommandCallback = (error) => {
const result: UninstallCommandCallbackResult = {
Expand Down
4 changes: 2 additions & 2 deletions src/types/commands.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ export type StatsCommandIterator = (
) => Promise<StatsCommandIteratorResult>

export type StatsCommandCallbackResult = CommandCallbackBaseResult & {
totalStats?: {
totalStats: {
totalVaults: number
totalPlugins: number
}
installedPlugins?: InstalledPlugins
installedPlugins: InstalledPlugins
}

export type StatsCommandCallback = (
Expand Down

0 comments on commit a8d82a3

Please sign in to comment.