fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
a53db7256
master
1# localization: skip(private) 2# 3# This file is explicitly sourced by the npm and yarn completions 4# It is used to enumerate entries from the npm registry, interact with a locally installed 5# package.json file, and more. 6# Other completions that would depend on this include jspm, pnpm, etc. 7 8# If all-the-package-names is installed, it will be used to generate npm completions. 9# Install globally with `sudo npm install -g all-the-package-names`. Keep it up to date. 10function __npm_helper_installed 11# This function takes the command to globally install a package as $argv[1] 12if not type -q all-the-package-names 13if not set -qg __fish_npm_pkg_info_shown 14set -l old( commandline ) 15commandline -r"" 16echo \n fish: Run `$argv [ 1 ] all-the-package-names` to gain intelligent\ 17 package completion>&2 18commandline -f repaint 19commandline -r$old 20set -g __fish_npm_pkg_info_shown1 21end 22return 1 23end 24end 25 26# Entire list of packages is too long to be used efficiently in a `complete` subcommand. 27# Search it for matches instead. 28function __npm_filtered_list_packages 29# This function takes the command to globally install a package as $argv[1] 30if not __npm_helper_installed $argv [ 1 ] 31return 32end 33 34# Do not provide any completions if nothing has been entered yet to avoid long hang. 35if string match -rq -- .( commandline -ct) 36# Filter the results here rather than in the C++ code due to #5267 37# `all-the-package-names` reads a billion line JSON file using node (slow) then prints 38# it all (slow) using node (slow). Directly parse the `names.json` file that ships with it instead. 39for pathin { $HOME /.config/yarn/global, $HOME /.npm-global/lib, /usr/local/lib} /node_modules/all-the-package-names/names.json 40test -f $path || continue 41# Using `string replace` directly is slow because it doesn't know to stop looking after a match cannot be 42# found in the alphabetically sorted list. Use `look` for its binary search support. 43look ' "' ( commandline -ct) $path | string replace -rf'^ "(.*?)".*' '$1' 44break 45end 46end 47end 48 49function __npm_find_package_json 50set -l parents( __fish_parent_directories ( pwd -P)) 51 52for pin $parents 53if test -f " $p /package.json" 54echo " $p /package.json" 55return 0 56end 57end 58 59return 1 60end 61 62function __npm_installed_global_packages 63set -l prefix( npm prefix -g) 64set -l node_modules" $prefix /lib/node_modules" 65 66for pathin $node_modules /* 67set -l mod( path basename --$path ) 68 69if string match -rq"^@" $mod 70for sub_pathin $path /* 71set -l sub_mod( string split'/' $sub_path )[ -1 ] 72echo $mod /$sub_mod 73end 74else 75echo $mod 76end 77end 78end 79 80function __npm_installed_local_packages 81set -l package_json( __npm_find_package_json ) 82if not test $status -eq 0 83# no package.json in tree 84return 1 85end 86 87if set -l python( __fish_anypython ) 88$python -S -c'import json, sys; data = json.load(sys.stdin); 89print("\n".join(data.get("dependencies", []))); print("\n".join(data.get("devDependencies", [])))' < $package_json 2> /dev/null 90else if type -q jq 91jq -r'.dependencies as $a1 | .devDependencies as $a2 | ($a1 + $a2) | to_entries[] | .key' $package_json 92else 93set -l depsFound0 94for linein ( cat $package_json ) 95# echo "evaluating $line" 96if test $depsFound -eq 0 97# echo "mode: noDeps" 98if string match -qr'(devD|d)ependencies"' --$line 99# echo "switching to mode: deps" 100set depsFound1 101continue 102end 103continue 104end 105 106if string match -qr'\}' --$line 107# echo "switching to mode: noDeps" 108set depsFound0 109continue 110end 111 112# echo "mode: deps" 113 114string replace -r'^\s*"([^"]+)".*' '$1' --$line 115end 116end 117end 118 119function __fish_npm_helper 120# dummy function to allow sourcing this file 121end