yarn workspace 要输整个包名但是包名好长怎么办


2 个赞

If you are using fish shell, I have a useful completion script for you, just put it in your fish config (like ~/.config/fish/completions/):

function __fish_yarn_get_workspaces
  if type -q jq
    command \
      # List all workspaces in the current project in json format
      yarn workspaces list --json 2>/dev/null \
      # feed every line to jq to get the name of the workspace
      | jq -r '.name' 2>/dev/null

complete -f -c yarn -n '__fish_seen_subcommand_from workspace' -xa "(__fish_yarn_get_workspaces)"


NOTE: this script requires jq to be installed.
PS: this script is a bit laggy, but I still have no idea to improve it.
PS2: this script is only tested on yarn berry.

1 个赞

Well I do a research on jq and found a faster method, still requires jq:

function __fish_yarn_get_workspaces
  if type -q jq && test -f package.json
    command \
      # List all workspaces glob patterns in the current workspaces
      jq -r '.workspaces | .[]' package.json 2>/dev/null |\
      # list nested package.json files in every glob pattern
      xargs -I {} -- fish -c "ls {}/package.json 2>/dev/null" |\
      # get name of the workspace from the package.json
      xargs -I {} -- fish -c "jq -r '.name' {} 2>/dev/null" |\
      # remove empty lines
      grep -v '^$' 2>/dev/null |\
      # remove duplicates
      sort -u 2>/dev/null

complete -f -c yarn -n '__fish_seen_subcommand_from workspace' -a "(__fish_yarn_get_workspaces)"
2 个赞