zsh-users / zsh-completions

Additional completion definitions for Zsh.
Other
6.93k stars 713 forks source link

Yarn workspaces: autocompletions for scripts, .bin and file paths #847

Open kachkaev opened 2 years ago

kachkaev commented 2 years ago

👋 folks! I’ve been a heavy user of yarn workspaces for some time and would like to suggest an improvement to autocompletions. It might be quite a difficult one to implement, but is at least worth tracking.

Let’s take this repo as an example: https://github.com/ferreiro/example-monorepo

We have two folders: packages/client and packages/server. They correspond to workspaces named client and server, respectively (the names are located in package.json files).

In a broader case, any folder can be a workspace and the name of the package can be anything, e.g. a folder named /apps/docs may map to a workspace named @mycorp/my-docs-app.

This repo’s yarn completion works well up to this point:

yarn workspace c →
yarn workspace client

yarn workspace s →
yarn workspace server

yarn workspace @m →
yarn workspace @mycorp/
yarn workspace @mycorp/m →
yarn workspace @mycorp/my-docs-app

That's useful, especially if a workspace starts with @ and is therefore lengthy. If we press tab after the workspace name, we will always see:

add                  -- Installs a package and any packages that it depends on
bin                  -- Displays the location of the yarn bin folder
list                 -- List installed packages
remove               -- Remove installed package from dependencies updating package.json
upgrade              -- Upgrades packages to their latest version based on the specified range
upgrade-interactive  -- Interactively upgrade packages

That‘s cool, but the list is not as complete as for the root workspace. What would be really cool is to also get autocompletion for scripts, binaries and local file names within a workspace:

yarn workspace client st →
yarn workspace client start
## because we have packges/client/package.json → scripts → start

yarn workspace @mycorp/my-docs-app j →
yarn workspace @mycorp/my-docs-app jest
## because we have apps/docs/node_modules/.bin/jest

yarn workspace @mycorp/my-docs-app jest s→
yarn workspace @mycorp/my-docs-app jest src/
yarn workspace @mycorp/my-docs-app jest src/hello.te →
yarn workspace @mycorp/my-docs-app jest src/hello.test.js
## because we have apps/docs/src/hello.test.js

The are two main challenges:

I’d be keep to help if this plugin was written in JavaScript, but my knowledge of shell is way too limited to grab this improvement task. 😅 Happy to take part in testing if needed! 🙌

jmatsushita commented 1 year ago

Would love to see this too!

Note that in yarn >2.4.3 (also tested in 3.6.0 and 4.0.0-rc.46) yarn workspace client run returns the following which could be used to inform the completions.

> yarn workspace client run
➤ YN0000: clean               'rm -rf node_modules'
➤ YN0000: lint                'tsc --noEmit && eslint "src/**/*.ts*"'
➤ YN0000: start               'node index.js'
➤ YN0000: Done in 0s 2ms

Unfortunately the --json argument doesn't seem to work yet.