Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
npm install's all required packages in a file, using sed and xargs, --save-dev and --save optional
#!/bin/bash
#
# npm install all required packages
# example usage
#
# ./install-required-packages.sh gulpfile.sh --save-dev
# ./install-required-packages.sh gulpfile.sh --save
# ./install-required-packages.sh gulpfile.sh
#
# TODO: this matches require's that are commented out. Fix.
# TODO: doesn't match multiple requires on one line. But do you really want to ever write code like that?
FILENAME=$1
shift
sed -e "/\(.*\)require('\([-A-Za-z0-9]*\)')\(.*\)/!d" \
-e "s/\(.*\)require('\([-A-Za-z0-9]*\)')\(.*\)/\2/" $FILENAME \
| xargs -L1 npm install "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment