CommandRegistry API, allowing you to build custom tools that integrate seamlessly with the shell.
Command Interface
All commands in Lifo implement theCommand type, which is a function that receives a CommandContext and returns a promise that resolves to an exit code.
CommandContext
The context object provides everything your command needs to interact with the system:Stream Interfaces
Creating a Simple Command
Working with the Filesystem
Commands can read and write files through the VFS:Handling Standard Input
Commands can read from stdin to support pipes:Environment Variables
Access environment variables through the context:Lazy Loading Commands
For better performance, register commands with lazy loading:Command:
Complete Example: JSON Formatter
Here’s a complete command that formats JSON files:Handling Cancellation
Use thesignal for long-running operations:
Best Practices
- Exit codes: Return
0for success, non-zero for errors (typically1) - Error handling: Catch
VFSErrorfor filesystem operations - Path resolution: Always use
resolve(ctx.cwd, path)for relative paths - Output: Write to
stdoutfor data,stderrfor errors and diagnostics - Newlines: Include
\nat the end of output lines - Stdin support: Check for
ctx.stdinto enable piping - Cancellation: Check
ctx.signal.abortedin long operations - Help text: Show usage information when called incorrectly