Skip to content

Commit

Permalink
allow run jobs in parallel (nodenv#13)
Browse files Browse the repository at this point in the history
Closes: nodenv#13
  • Loading branch information
AeroNotix committed Aug 17, 2020
1 parent 6b9b58e commit 8227bb0
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions bin/nodenv-each
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#
# Summary: Execute a command for each Node version
#
# Usage: nodenv each [-g] [-v] [--] <command> [arg1 arg2...]
# Usage: nodenv each [-g] [-p] [-v] [--] <command> [arg1 arg2...]
#
# Executes a command for each Node version by setting NODENV_VERSION.
# Failures are collected and reported at the end.
#
# 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
Expand All @@ -32,6 +33,9 @@ for arg in "$@"; do
-g | --system )
include_system=1
;;
-p | --parallel )
parallel=1
;;
-v | --verbose )
verbose=1
;;
Expand All @@ -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=""
Expand All @@ -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

0 comments on commit 8227bb0

Please sign in to comment.