fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMark private functions that don't need localizationa53db7256

master
4.2 KiB121 linesraw
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]
12    if not type -q all-the-package-names
13        if not set -qg __fish_npm_pkg_info_shown
14            set -l old (commandline)
15            commandline -r ""
16            echo \nfish: Run `$argv[1] all-the-package-names` to gain intelligent \
17                package completion >&2
18            commandline -f repaint
19            commandline -r $old
20            set -g __fish_npm_pkg_info_shown 1
21        end
22        return 1
23    end
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]
30    if not __npm_helper_installed $argv[1]
31        return
32    end
33
34    # Do not provide any completions if nothing has been entered yet to avoid long hang.
35    if 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.
39        for path in {$HOME/.config/yarn/global,$HOME/.npm-global/lib,/usr/local/lib}/node_modules/all-the-package-names/names.json
40            test -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.
43            look '  "'(commandline -ct) $path | string replace -rf '^  "(.*?)".*' '$1'
44            break
45        end
46    end
47end
48
49function __npm_find_package_json
50    set -l parents (__fish_parent_directories (pwd -P))
51
52    for p in $parents
53        if test -f "$p/package.json"
54            echo "$p/package.json"
55            return 0
56        end
57    end
58
59    return 1
60end
61
62function __npm_installed_global_packages
63    set -l prefix (npm prefix -g)
64    set -l node_modules "$prefix/lib/node_modules"
65
66    for path in $node_modules/*
67        set -l mod (path basename -- $path)
68
69        if string match -rq "^@" $mod
70            for sub_path in $path/*
71                set -l sub_mod (string split '/' $sub_path)[-1]
72                echo $mod/$sub_mod
73            end
74        else
75            echo $mod
76        end
77    end
78end
79
80function __npm_installed_local_packages
81    set -l package_json (__npm_find_package_json)
82    if not test $status -eq 0
83        # no package.json in tree
84        return 1
85    end
86
87    if 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
90    else if type -q jq
91        jq -r '.dependencies as $a1 | .devDependencies as $a2 | ($a1 + $a2) | to_entries[] | .key' $package_json
92    else
93        set -l depsFound 0
94        for line in (cat $package_json)
95            # echo "evaluating $line"
96            if test $depsFound -eq 0
97                # echo "mode: noDeps"
98                if string match -qr '(devD|d)ependencies"' -- $line
99                    # echo "switching to mode: deps"
100                    set depsFound 1
101                    continue
102                end
103                continue
104            end
105
106            if string match -qr '\}' -- $line
107                # echo "switching to mode: noDeps"
108                set depsFound 0
109                continue
110            end
111
112            # echo "mode: deps"
113
114            string replace -r '^\s*"([^"]+)".*' '$1' -- $line
115        end
116    end
117end
118
119function __fish_npm_helper
120    # dummy function to allow sourcing this file
121end