Skip to content

Instantly share code, notes, and snippets.

@ianjsikes
Created July 24, 2019 19:24
Show Gist options
  • Save ianjsikes/c5e9336986ce4726e5c0e4f657071d20 to your computer and use it in GitHub Desktop.
Save ianjsikes/c5e9336986ce4726e5c0e4f657071d20 to your computer and use it in GitHub Desktop.
Fish shell script to interactively find and run a script in your package.json
function yarg -d "Interactively choose a script to `yarn run`"
if not test -e ./package.json
echo "No package.json found in current directory"
return 1
end
if not type -q yarn; and not type -q npm
echo "You must have yarn or npm installed"
return 1
end
if not type -q fzf; or not type -q jq
echo "You need to install fzf and jq"
return 1
end
set hasScriptsProperty (jq -r 'has("scripts")' package.json)
if test "$hasScriptsProperty" = "false"
echo "No scripts found in package.json"
return 1
end
set hasScripts (jq -r '.scripts | keys | length' package.json)
if test "$hasScripts" = "0"
echo "No scripts found in package.json"
return 1
end
set cmdToRun (jq -r '.scripts | to_entries[] | (.key + "|||" + .value)' package.json | column -t -s "|||" | fzf --reverse --height 30% | awk '{print $1}')
if test "$cmdToRun" = ""
echo "No command selected"
return 1
end
if type -q yarn
yarn run "$cmdToRun"
else
npm run "$cmdToRun"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment