Read json via python

Apparently that's actually faster than jq, and it's more likely to be
installed.

Also it should convince the arch packager to remove the jq dependency.

The indentation is weird, though.

[ci skip]
This commit is contained in:
Fabian Homborg 2019-03-09 18:00:52 +01:00
parent 9b1fb6938e
commit b0e9405b11
3 changed files with 31 additions and 11 deletions

View file

@ -44,13 +44,19 @@ function __bower_matching_pkgs
bower search (commandline -ct) | string match -r "\S+[^\s]" | string match -v "Search"
end
# Output of `bower list` is a) slow, b) convoluted. Use `jq` instead.
# Output of `bower list` is a) slow, b) convoluted. Use `python` or `jq` instead.
function __bower_list_installed
if not type -q jq
if not test -e bower.json
return 1
end
if not test -e bower.json
if set -l python (__fish_anypython)
$python -c 'import json, sys; data = json.load(sys.stdin);
for k,v in data["dependencies"].items(): print(k + "\t" + v[:18])' bower.json 2>/dev/null
return
end
if not type -q jq
return 1
end

View file

@ -84,10 +84,19 @@ function __fish_parse_npm_run_completions
end
function __fish_npm_run
# Like above, only try to call npm if there's a command by that name to facilitate aliases that call nvm.
if command -sq jq; and test -e package.json
jq -r '.scripts | to_entries[] | .key,.value' <package.json | __fish_parse_npm_run_completions
# Complete `npm run` scripts
# These are stored in package.json, which we need a tool to read.
# python is very probably installed (we use it for other things!),
# jq is slower but also a common tool,
# npm is dog-slow and might check for updates online!
if test -e package.json; and set -l python (__fish_anypython)
# Warning: That weird indentation is necessary, because python.
$python -c 'import json, sys; data = json.load(sys.stdin);
for k,v in data["scripts"].items(): print(k + "\t" + v[:18])' <package.json 2>/dev/null
else if command -sq jq; and test -e package.json
jq -r '.scripts | to_entries | map("\(.key)\t\(.value | tostring | .[0:20])") | .[]' package.json
else if command -sq npm
# Like above, only try to call npm if there's a command by that name to facilitate aliases that call nvm.
command npm run | string match -r -v '^[^ ]|^$' | string trim | __fish_parse_npm_run_completions
end
end

View file

@ -71,11 +71,16 @@ complete -f -c yarn -n '__fish_use_subcommand' -a remove
complete -f -c yarn -n '__fish_use_subcommand' -a run
function __fish_yarn_run
if test -e package.json; and type -q jq
jq -r '.scripts | to_entries | map("\(.key)\t\(.value | tostring | .[0:20])") | .[]' package.json
else if type -q jq
command yarn run --json 2> /dev/null | jq -r '.data.hints? | to_entries | map("\(.key)\t\(.value | tostring |.[0:20])") | .[]'
end
if test -e package.json; and set -l python (__fish_anypython)
# Warning: That weird indentation is necessary, because python.
$python -c 'import json, sys; data = json.load(sys.stdin);
for k,v in data["scripts"].items(): print(k + "\t" + v[:18])' <package.json 2>/dev/null
else if test -e package.json; and type -q jq
jq -r '.scripts | to_entries | map("\(.key)\t\(.value | tostring | .[0:20])") | .[]' package.json
else if type -q jq
# Yarn is quite slow and still requires `jq` because the normal format is unusable.
command yarn run --json 2> /dev/null | jq -r '.data.hints? | to_entries | map("\(.key)\t\(.value | tostring |.[0:20])") | .[]'
end
end
# Scripts can be used like normal subcommands, or with `yarn run SCRIPT`.