Add intelligent package completion to yarn

Now parses package.json and uses results to provide a list of possible
completions to `yarn remove`. There may be other subcommands that could
benefit from this.

Could have parsed yarn output, but yarn is slow and packages.json format
is generally standard since it's machine-generated json.
This commit is contained in:
Mahmoud Al-Qudsi 2018-03-05 08:28:19 -06:00
parent 225f748f79
commit a85d2bf27a
2 changed files with 49 additions and 0 deletions

View file

@ -56,6 +56,7 @@ This section is for changes merged to the `major` branch that are not also merge
- `git` (#4395, #4396, #4592)
- `brew`
- `diskutil`
- `yarn`
--

View file

@ -2,6 +2,54 @@
# see https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_seen_subcommand_from.fish
# and https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_use_subcommand.fish
function __yarn_find_package_json
set parents (__fish_parent_directories (pwd))
for p in $parents
if test -f "$p/package.json"
echo "$p/package.json"
return 0
end
end
return 1
end
function __yarn_list_packages
set -l package_json (__yarn_find_package_json)
if not test $status -eq 0
# no package.json in tree
return 1
end
set -l depsFound 0
for line in (cat $package_json)
# echo "evaluating $line"
if test $depsFound -eq 0
# echo "mode: noDeps"
if string match -qr '(devD|d)ependencies"' -- $line
# echo "switching to mode: deps"
set depsFound 1
continue
end
continue
end
if string match -qr '\}' -- $line
# echo "switching to mode: noDeps"
set depsFound 0
continue
end
# echo "mode: deps"
string replace -r '^\s*"([^"]+)".*' '$1' -- $line
end
end
complete -f -c yarn -n '__fish_seen_subcommand_from remove' -a (set -l packages (__yarn_list_packages); echo $packages)
complete -f -c yarn -n '__fish_use_subcommand' -a help
complete -f -c yarn -n '__fish_use_subcommand' -a access