Giter VIP home page Giter VIP logo

Comments (6)

jgoux avatar jgoux commented on May 28, 2024 1

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.

from citty.

NozomuIkuta avatar NozomuIkuta commented on May 28, 2024

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?

from citty.

tmg0 avatar tmg0 commented on May 28, 2024

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,
})

from citty.

tmg0 avatar tmg0 commented on May 28, 2024

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

from citty.

peterroe avatar peterroe commented on May 28, 2024

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

from citty.

pi0 avatar pi0 commented on May 28, 2024

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

from citty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.