A video showcasing how pnpm autocompletions work on a test CLI command like
my-cli
tab#
Instant feedback for your CLI tool when hitting [TAB] in your terminal
As CLI tooling authors, if we can spare our users a second or two by not checking the documentation or writing the -h option, we're doing them a huge favor. The unconscious loves hitting the [TAB] key. It always expects feedback. So it feels disappointing when hitting that key in the terminal but then nothing happens. That frustration is apparent across the whole JavaScript CLI tooling ecosystem.
Autocompletions are the solution to not break the user's flow. The issue is they're not simple to add. zsh expects them in one way, and bash in another way. Then where do we provide them so the shell environment parses them? Too many headaches to ease the user's experience. Whether it's worth it or not is out of the question. Because tab is the solution to this complexity.
my-cli.ts:
import t from '@bombsh/tab';
t.name('my-cli');
t.command('start', 'start the development server');
if (process.argv[2] === 'complete') {
const [shell, ...args] = process.argv.slice(3);
if (shell === '--') {
t.parse(args);
} else {
t.setup(shell, x);
}
}
This my-cli.ts would be equipped with all the tools required to provide autocompletions.
node my-cli.ts complete -- "st"
start start the development server
:0
This output was generated by the t.parse method to autocomplete "st" to "start".
Obviously, the user won't be running that command directly in their terminal. They'd be running something like this.
source <(node my-cli.ts complete zsh)
Now whenever the shell sees my-cli, it would bring the autocompletions we wrote for this CLI tool. The node my-cli.ts complete zsh part would output the zsh script that loads the autocompletions provided by t.parse which then would be executed using source.
The autocompletions only live through the current shell session. To set them up across all terminal sessions, the autocompletion script should be injected in the .zshrc file.
my-cli complete zsh > ~/completion-for-my-cli.zsh && echo 'source ~/completion-for-my-cli.zsh' >> ~/.zshrc
Or
echo 'source <(npx --offline my-cli complete zsh)' >> ~/.zshrc
This is an example of autocompletions on a global CLI command that is usually installed using the -g flag (e.g. npm add -g my-cli) which is available across the computer.
While working on tab, we came to the realization that most JavaScript CLIs are not global CLI commands but rather, per-project dependencies.
For instance, Vite won't be installed globally and instead it'd be always installed on a project. Here's an example usage:
pnpm vite dev
Rather than installing it globally. This example is pretty rare:
vite dev
So in this case, a computer might have hundreds of Vite instances each installed separately and potentially from different versions on different projects.
We were looking for a fluid strategy that would be able to load the autocompletions from each of these dependencies on a per-project basis.
And that made us develop our own autocompletion abstraction over npm, pnpm and yarn. This would help tab identify which binaries are available in a project and which of these binaries provide autocompletions. So the user would not have to source anything or inject any script in their .zshrc.
They'd only have to run this command once and inject it in their shell config.
npx @bombsh/tab pnpm zsh
These autocompletions on top of the normal autocompletions that these package managers provide are going to be way more powerful.
These new autocompletions on top of package managers would help us with autocompletions on commands like pnpm vite and other global or per-project binaries. The only requirement would be that the npm binary itself would be a tab-compatible binary.
What is a tab-compatible binary? It's a tool that provides the complete subcommand that was showcased above. Basically any CLI tool that uses tab for its autocompletions is a tab-compatible binary.
pnpm my-cli complete --
start start the development server
:0
We are planning to maintain these package manager autocompletions on our own and turn them into full-fledged autocompletions that touch on every part of our package managers.