Comments (6)
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.
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.
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.
Emmmmm, It seems unnecessary to add a abort callback, in before
hook can just throw an error to terminate the logic.
from citty.
It seems like your needs can be completely solved by setup/cleanup
, maybe we need an idea with a clearer function orientation 👀
from citty.
@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)
- Hiding internal commands HOT 2
- Support `transformCommandToApi` util HOT 1
- Support `createMain(command)` return value from `run` method HOT 4
- Support argument normalizer/validator HOT 4
- Disabling Glob Pattern Parsing HOT 2
- Add support for custom error handling
- Support multiple (wildcard) positional args HOT 2
- Feat: return usage as data instead of just a string
- Undefined value of subcommand argument of run method HOT 3
- Support auto case transformation for arguments HOT 1
- Doesn't work as a binary HOT 2
- [Question] Process exit dont exit the terminal. HOT 1
- CLI Plugins HOT 1
- Non positional arguments of main command usage does not allow to use any other commands HOT 1
- Built-in `showUsage` is not tree-shaking, when put custom `showUsage` at `runMain`.
- Mechanism to customize each part displayed by `showUsage` HOT 2
- Support type `number` to args
- Support detecting args that is specified but with no values
- A way to override `--version`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from citty.