You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add defs in defineCommand function like interceptors or middlewares
example:
constcustomInterceptor1=(ctx: CommandContext)=>falseconstcustomInterceptor2=(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 executeconsole.log(`${args.friendly ? "Hi" : "Greetings"}${args.name}!`);},})
Additional information
Would you be willing to help implement this feature?
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?
Activity
nozomuikuta commentedon Oct 31, 2023
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
onXYZinterceptors 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
runmethod body for better code organization?tmg0 commentedon Oct 31, 2023
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
abortcallback function inCommandContextis a better way?e.g.
tmg0 commentedon Oct 31, 2023
Emmmmm, It seems unnecessary to add a abort callback, in
beforehook can just throw an error to terminate the logic.[-]Support interceptors befor execute run func[/-][+]Support hooks befor/after execute run func[/+]peterroe commentedon Oct 31, 2023
It seems like your needs can be completely solved by
setup/cleanup, maybe we need an idea with a clearer function orientation 👀jgoux commentedon Mar 21, 2024
A middleware system would be super useful for stuff like telemetry:
The idea would be that the run handler would be passed to the middleware chain defined in
middlewares.pi0 commentedon Mar 21, 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