arcanis / clipanion

Type-safe CLI library / framework with no runtime dependencies
https://mael.dev/clipanion/
1.12k stars 65 forks source link

Implements stream capture #103

Closed arcanis closed 3 years ago

arcanis commented 3 years ago

It may sound a little weird to have to use this.context.stdout.write to write to the console in a Node context (developers are used to just call console.log for that). While it's important to keep this distinction in order to let commands be chainable (ie one command may call others and capture their output), newest Node releases may provide us an interesting tool to improve the user experience.

This diff introduces a new option, enableCapture. When set (the default is false at the moment), passing non-standard streams to a CLI execution will cause Clipanion to register itself into the process.stdout.write pipeline (same for stderr), effectively capturing everything that JS code would print. Asynchronicity shouldn't be an issue thanks to AsyncLocalStorage, which will ensure that each command access their own streams, even if asynchronous.