Skip to content

CLI Plugins #130

Closed
Closed

Description

@pi0

inspired from #98 (comment) by @jgoux

With plugins, we can allow them to intercept to different hooks but also preserve their context. We can extend to more hooks if needed.

Example:

const telemetryPlugin = defineCittyPlugin(() => {
  const telemetry = new TelemetryClient();

  return {
    name: 'telemetry',
    async setup() {
      telemetry.init();
      await telemetry.captureEvent(`$command:${ctx.cmd.meta.name}:start`); 
    },
    async cleanup() {
      await telemetry.captureEvent(`$command:${ctx.cmd.meta.name}:end`);
      telemetry.flush();
    }
  }
})

defineCommand({
  meta: {
    name: "hello",
    version: "1.0.0",
    description: "My Awesome CLI App",
  },
  plugins: [telemetryPlugin],
  run({ args }) {
    console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
  },
})

Activity

  1. jgoux commented on Mar 21, 2024

    @jgoux

    Sounds good to me! 👍

  2. RaphaelDDL commented on Feb 21, 2025

    @RaphaelDDL

    Hello there, I was looking for a kind of interceptor/plugin for citty that I could hook without changing a command's code, and stumbled upon #98 and this issue. @Barbapapazes made some initial work on it, I was wondering if there was any other conversation to resume or abandon the idea?

    My use case is the following:

    There's a cli using citty, and we wanted to do a wrapper on top of it for a few commands of ours. So we import it's main defineCommand, merge with our commands and provide that to the main defineCommand as subcommands.

    For a command ABC from said cli, I'd like it to invoke N commands/functions of mine before/after, but not touch anything the other CLI does because I want it's behavior to happen.

    So, in that fashion, I think we can just make a setup() on our main defineCommand which will check the context.args._[0] to know which command was called and do what needs to be done before the other command runs for our case, and that should be enough.

    But I was wondering if a plugin would look cleaner? idk. Visually the suggested approach felt just like vue2 mixin which I liked to use back then so I'm biased lol.

  3. added a commit that references this issue on Mar 15, 2026
    42b92a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions