diff --git a/bin/nodenv-each b/bin/nodenv-each index 2b8b17c..7e5bc54 100755 --- a/bin/nodenv-each +++ b/bin/nodenv-each @@ -2,7 +2,7 @@ # # Summary: Execute a command for each Node version # -# Usage: nodenv each [-g] [-v] [--] [arg1 arg2...] +# Usage: nodenv each [-g] [-p] [-v] [--] [arg1 arg2...] # # Executes a command for each Node version by setting NODENV_VERSION. # Failures are collected and reported at the end. @@ -10,6 +10,7 @@ # Options: # --help, -h Show help/usage # --system, -g Include the system `node`. +# --parallel -p Run everything in parallel. # --verbose, -v Verbose mode. Prints a header for each node. # set -e @@ -32,6 +33,9 @@ for arg in "$@"; do -g | --system ) include_system=1 ;; + -p | --parallel ) + parallel=1 + ;; -v | --verbose ) verbose=1 ;; @@ -53,6 +57,13 @@ if [ $# -eq 0 ]; then exit 1 fi +if [ -n "$parallel" ]; then + if ! command -v parallel &> /dev/null; then + echo "You must install the parallel tool in order to run jobs in parallel" + exit 1 + fi +fi + GRAY="" RED="" YELLOW="" @@ -69,21 +80,32 @@ failed_nodes="" trap "exit 1" INT -for node in ${include_system:+system} $(nodenv-versions --bare --skip-aliases); do +exec_node() { if [ -n "$verbose" ]; then - header="===[$node]===================================================================" - header="${header:0:72}" - header="${header/[/[${YELLOW}}" - header="${header/]/${GRAY}]}" + local header="===[$1]===================================================================" + local header="${header:0:72}" + local header="${header/[/[${YELLOW}}" + local header="${header/]/${GRAY}]}" echo -e "${GRAY}${header}${NORMAL}" fi - - NODENV_VERSION="$node" "$@" || failed_nodes="$failed_nodes $node" - + local version="$1" + shift + NODENV_VERSION="$version" "$@" [ -n "$verbose" ] && echo -done +} -if [ -n "$failed_nodes" ]; then - echo -e "${RED}FAILED IN:${failed_nodes}${NORMAL}" - exit 1 +NODES=(${include_system:+system} $(nodenv-versions --bare --skip-aliases)) + +if [ -n "$parallel" ]; then + export -f exec_node + export verbose YELLOW GRAY NORMAL + echo "${NODES[@]}" | tr ' ' '\n' | parallel --env YELLOW --env GRAY --env NORMAL --env verbose --env exec_node exec_node {0} "$@" +else + for node in "${NODES[@]}"; do + NODENV_VERSION="$node" "$@" || failed_nodes="$failed_nodes $node" + done + if [ -n "$failed_nodes" ]; then + echo -e "${RED}FAILED IN:${failed_nodes}${NORMAL}" + exit 1 + fi fi