Created
January 12, 2023 01:47
-
-
Save Surreal9/31683f9ec4516916010c4fa767ff48f5 to your computer and use it in GitHub Desktop.
npm run alias with autocomplete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias nr="npm run" | |
_npm_scripts() { | |
# check package.json file in current directory | |
if [ ! -f ./package.json ]; then | |
return | |
fi | |
local scripts="$(node -e 'const { scripts } = require(`./package.json`); if (!scripts) process.exit(); let a = Object.entries(scripts); for (let s in scripts) { console.log(s); }' | grep -E ^$2)" | |
local -a toks | |
local tmp | |
if [ -z "$scripts" ]; then | |
return; | |
fi | |
while read -r tmp; do | |
toks+=( "$tmp" ) | |
done <<< "$scripts" | |
if [[ ${#toks[@]} -ne 0 ]]; then | |
COMPREPLY+=( "${toks[@]}" ) | |
fi | |
} | |
complete -F _npm_scripts nr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment