Skip to content

Instantly share code, notes, and snippets.

@chernjie
Last active February 7, 2021 22:41
Show Gist options
  • Save chernjie/46983c4bc7c07b77014a5ae60065ca1a to your computer and use it in GitHub Desktop.
Save chernjie/46983c4bc7c07b77014a5ae60065ca1a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Like `npm`, but executes the command in nested sub-directories within
# your current git repository
#
# @author CJ <lim@chernjie.com>
# Example:
#
# $ npmr ls lodash # List installed packages in nested node packages
#
# $ npmr i # install node modules recursively
for-each-args () {
local _command=$1
shift
for i in $@
do
npm $_command $i &
done
wait
}
for-each-package() {
for i in $(git ls-files --recurse-submodules | grep package.json | xargs -n1 dirname)
do
cd $i > /dev/null
npm $@ &
cd - > /dev/null
done
wait
}
case $1 in
info) for-each-args $@ ;;
*) for-each-package $@ ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment