-
Notifications
You must be signed in to change notification settings - Fork 26
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
Support hooks befor/after execute run func #98
Comments
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 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 |
Actually what I need is a 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 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,
}) |
Emmmmm, It seems unnecessary to add a abort callback, in |
It seems like your needs can be completely solved by |
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 |
Describe the feature
Add defs in
defineCommand
function like interceptors or middlewaresexample:
Additional information
The text was updated successfully, but these errors were encountered: