Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-12465784 feat: add plugins:post* hooks #932

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-await-in-loop */
import {Args, Command, Errors, Flags, Interfaces, ux} from '@oclif/core'
import {Args, Command, Errors, Flags, Interfaces, Plugin, ux} from '@oclif/core'
import {bold, cyan} from 'ansis'
import validate from 'validate-npm-package-name'

Expand Down Expand Up @@ -195,6 +195,12 @@ Use the <%= config.scopedEnvVarKey('NPM_REGISTRY') %> environment variable to se
throw error
}

const pluginInstance = new Plugin(plugin)
await pluginInstance.load()
this.config.plugins.set(pluginInstance.name, pluginInstance)

await this.config.runHook('plugins:postinstall', {})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plugin-autocomplete needs config.plugins with the new plugin populated for it to be included in the refreshed autocomplete files:
https://github.com/oclif/plugin-autocomplete/blob/986c6ec238ff3764c78331de7ea7b49e0670a47c/src/commands/autocomplete/create.ts#L95


ux.action.stop(`installed v${plugin.version}`)
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export default class PluginsUninstall extends Command {
logLevel: determineLogLevel(this.config, flags, 'silent'),
})

const pluginNameToDelete: string[] = []

if (argv.length === 0) argv.push('.')
for (const plugin of argv as string[]) {
const friendly = removeTags(plugins.friendlyName(plugin))
Expand All @@ -64,15 +66,22 @@ export default class PluginsUninstall extends Command {

try {
const {name} = unfriendly
const displayName = friendly === '.' ? name : friendly ?? name
const displayName = friendly === '.' ? name : (friendly ?? name)
ux.action.start(`${this.config.name}: Uninstalling ${displayName}`)
await plugins.uninstall(name)
pluginNameToDelete.push(name)
} catch (error) {
ux.action.stop(bold.red('failed'))
throw error
}

ux.action.stop()
}

for (const p of pluginNameToDelete) {
this.config.plugins.delete(p)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we do the same here:
https://github.com/oclif/core/blob/213e9203fd7f0f5aaffdcc9eb28d2828f7679373/src/config/config.ts#L475

but for the autocomplete refresh-hook use-case it doesn't work because the hook runs before oclif/core calls plugins.delete:

example with some logs to show order of execution

➜  plugin-plugins git:(cd/post-hooks) ✗ sf plugins uninstall @cristiand391/sf-plugin-fzf-cmp && unfunction _sf && autoload -U _sf
 ›   Warning: @oclif/plugin-autocomplete is a linked ESM module and cannot be auto-transpiled. Existing compiled source will be used instead.
 ›   Warning: @oclif/plugin-plugins is a linked ESM module and cannot be auto-transpiled. Existing compiled source will be used instead.
@salesforce/cli: Uninstalling @cristiand391/sf-plugin-fzf-cmp... done
running plugins:postuninstall hook
 ›   Warning: @oclif/plugin-autocomplete is a linked ESM module and cannot be auto-transpiled. Existing compiled source will be used instead.
done plugins:postuninstall hook
called `plugins.delete` from oclif/core

maybe we can remove the plugins.delete logic from oclif/core safely?
it was added here to avoid running postrun hooks from uninstalled plugins:
oclif/core#805

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think we can safely remove that once this is merged


await this.config.runHook('plugins:postuninstall', {})
}
}