Skip to content

Instantly share code, notes, and snippets.

@nagapavan
Created April 25, 2018 09:33
Show Gist options
  • Save nagapavan/22070c34522d15c072b7c258c09cb4ce to your computer and use it in GitHub Desktop.
Save nagapavan/22070c34522d15c072b7c258c09cb4ce to your computer and use it in GitHub Desktop.
npm hacks
function npm-freeze() {
# npm ls | grep -E "^(├|└)─" | cut -d" " -f2 | awk -v quote='"' 'BEGIN { FS = "@" } ; { print quote $1 quote,":",quote $2 quote"," }' | sed -e 's/ :/:/g'
entries=`npm list --depth=0 -prod true "$@" 2>/dev/null | grep -v "UNMET PEER DEPENDENCY\|peer dep missing" | grep -E "^(├|└)─" | cut -d" " -f2`
echo '"dependencies" : {'
for entry in ${entries}; do
if [[ $entry =~ ^@.* ]]; then
# echo $entry
echo $entry | awk -v quote='"' -v attherate='@' 'BEGIN { FS = "@" }; { print quote attherate $1$2 quote,":",quote $3 quote"," }'
else
echo $entry | awk -v quote='"' 'BEGIN { FS = "@" }; { print quote $1 quote,":",quote $2 quote"," }'
fi
done
echo "},"
entries=`npm list --depth=0 -dev true "$@" 2>/dev/null | grep -v "UNMET PEER DEPENDENCY\|peer dep missing" | grep -E "^(├|└)─" | cut -d" " -f2`
echo '"devDependencies" : {'
for entry in ${entries}; do
if [[ $entry =~ ^@.* ]]; then
# echo $entry
echo $entry | awk -v quote='"' -v attherate='@' 'BEGIN { FS = "@" }; { print quote attherate $1$2 quote,":",quote $3 quote"," }'
else
echo $entry | awk -v quote='"' 'BEGIN { FS = "@" }; { print quote $1 quote,":",quote $2 quote"," }'
fi
done
echo "},"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment