Skip to content

Instantly share code, notes, and snippets.

@ChugunovRoman
Last active January 6, 2024 10:04
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ChugunovRoman/515da7848d496792d9b04e4b62c38dba to your computer and use it in GitHub Desktop.
Save ChugunovRoman/515da7848d496792d9b04e4b62c38dba to your computer and use it in GitHub Desktop.
Alias for npm run with auto completing a npm scripts from package.json from current directory.
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