Skip to content

Support hooks befor/after execute run func #98

Closed

Description

@tmg0

Describe the feature

Add defs in defineCommand function like interceptors or middlewares

example:

const customInterceptor1 = (ctx: CommandContext) => false
const customInterceptor2 = (ctx: CommandContext) => console.log('before run.')

defineCommand({
  meta: {
    name: "hello",
    version: "1.0.0",
    description: "My Awesome CLI App",
  },
  interceptors: [customInterceptor1, customInterceptor2]
  run({ args }) {
    // output: before run.
    // and this code will not be execute
    console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
  },
})

Additional information

  • Would you be willing to help implement this feature?

Activity

  1. nozomuikuta commented on Oct 31, 2023

    @nozomuikuta
    Member

    It sounds reasonable by itself to support a way to run some logic before/after main logic.

    Actually, other UnJS packages also have "hooks" options (e.g. Nitro) or onXYZ interceptors options (e.g. h3, ofetch) to do something before/after some logic.

    The difference between these and your code is that your interceptors can abort the command execution, rather than making a side effect and/or modifying options passed.

    So, to clarify, do you want to have "validators" to determine if the command should be run, and want to separate them as another option instead of writing them inside of run method body for better code organization?

  2. tmg0 commented on Oct 31, 2023

    @tmg0
    Author

    Actually what I need is a hook, such as checking the version of the CLI before/after run the main logic with a better code organization.

    I'm not sure if determining whether to abort through the return value is a good idea, but it seems quite convenient.

    Maybe add a abort callback function in CommandContext is a better way?

    e.g.

    const before= ({ abort }: CommandContext) => {
      const isLatest = version === "1.0.0"
      if (!isLatest) { abort() }
    }
    
    const after= () => { console.log('after run.') }
    
    defineCommand({
      meta: {
        name: "hello",
        version: "1.0.0",
        description: "My Awesome CLI App",
      },
      before,
      run({ args }) {
        console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
        // output: after run
      },
      after,
    })
  3. tmg0 commented on Oct 31, 2023

    @tmg0
    Author

    Emmmmm, It seems unnecessary to add a abort callback, in before hook can just throw an error to terminate the logic.

  4. changed the title [-]Support interceptors befor execute run func[/-] [+]Support hooks befor/after execute run func[/+] on Oct 31, 2023
  5. peterroe commented on Oct 31, 2023

    @peterroe
    Contributor

    It seems like your needs can be completely solved by setup/cleanup, maybe we need an idea with a clearer function orientation 👀

  6. jgoux commented on Mar 21, 2024

    @jgoux

    A middleware system would be super useful for stuff like telemetry:

    const telemetryMiddleware = async (ctx, next) => {
      await telemetry.captureEvent(`$command:${ctx.cmd.meta.name}:start`); 
      await next(ctx);
      await telemetry.captureEvent(`$command:${ctx.cmd.meta.name}:end`);
    }
    
    defineCommand({
      meta: {
        name: "hello",
        version: "1.0.0",
        description: "My Awesome CLI App",
      },
      middlewares: [telemetryMiddleware],
      run({ args }) {
        console.log(`${args.friendly ? "Hi" : "Greetings"} ${args.name}!`);
      },
    })

    The idea would be that the run handler would be passed to the middleware chain defined in middlewares.

  7. pi0 commented on Mar 21, 2024

    @pi0
    Member

    @jgoux having a context-full api makes sense. I think i have a better idea inspired from your usecase to introduce with plugins ~> #130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions