Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(scripts): allow --reset --up/--down to automatically run script #8826

Merged
merged 10 commits into from
Jul 30, 2020
109 changes: 69 additions & 40 deletions scripts/foreach.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
# if a task fails, it will stop, and then to resume, simply run `foreach.sh` again (with or without the same command).
#
# to reset the session (either when all tasks finished or if you wish to run a different session), run:
# foreach.sh --reset
# foreach.sh [-r | --reset]
#
# to force a reset and run a session with the current, run:
# foreach.sh [-f | --force] [-u | --up || -d | --down] COMMAND
#
# to run the command only against the current module and its dependencies:
# foreach.sh --up COMMAND
# foreach.sh [-u | --up] COMMAND
#
# to run the command only against the current module and its consumers:
# foreach.sh --down COMMAND
# foreach.sh [-d | --down] COMMAND
#
# --------------------------------------------------------------------------------------------------
set -euo pipefail
Expand All @@ -41,52 +44,78 @@ function success {
printf "\e[32;5;81m$@\e[0m\n"
}

if [[ "${1:-}" == "--reset" ]]; then
rm -f "${statedir}/.foreach."*
success "state cleared. you are free to start a new command."
exit 0
function reset {
rm -f "${statedir}/.foreach."*
success "state cleared. you are free to start a new command."
}

DIRECTION=""
RESET=0
FORCE=0
SKIP=0
command_arg=""

for arg in "$@"
do
case "$arg" in
-r | --reset) RESET=1 ;;
-s | --skip) SKIP=1 ;;
-f | --force) FORCE=1 ;;
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved
-u | --up) DIRECTION="UP" ;;
-d | --down) DIRECTION="DOWN" ;;
*) command_arg="$command_arg$arg " ;;
esac
shift
done

if [[ "$FORCE" -eq 1 && "$DIRECTION" == "" ]]; then
error "running force requires a direction to run."
exit 1
fi
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved

if [[ "${1:-}" == "--skip" ]]; then
if [ ! -f ${statefile} ]; then
error "skip failed. no active sessions found."
exit 1
fi
next=$(head -1 ${statefile})
if [ -z "${next}" ]; then
error "skip failed. queue is empty. to reset:"
error " $0 --reset"
exit 1
fi
tail -n +2 "${statefile}" > "${statefile}.tmp"
cp "${statefile}.tmp" "${statefile}"
success "directory '$next' skipped. re-run the original foreach command to resume."
if [[ "$RESET" -eq 1 || "$FORCE" -eq 1 ]]; then
reset
if [[ "$RESET" -eq 1 ]]; then
exit 0
fi
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved
fi

if [[ "$SKIP" -eq 1 ]]; then
if [ ! -f ${statefile} ]; then
error "skip failed. no active sessions found."
exit 1
fi
next=$(head -1 ${statefile})
if [ -z "${next}" ]; then
error "skip failed. queue is empty. to reset:"
error " $0 --reset"
exit 1
fi
tail -n +2 "${statefile}" > "${statefile}.tmp"
cp "${statefile}.tmp" "${statefile}"
success "directory '$next' skipped. re-run the original foreach command to resume."
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved
exit 0
fi

direction=""
direction_desc=""
if [[ "${1:-}" == "--up" || "${1:-}" == "--down" ]]; then
if [ ! -f package.json ]; then
echo "--up or --down can only be executed from within a module directory (looking for package.json)"
exit 1
fi

scope=$(node -p "require('./package.json').name")
if [[ "$DIRECTION" == "UP" || "$DIRECTION" == "DOWN" ]]; then
if [ ! -f package.json ]; then
error "--up or --down can only be executed from within a module directory (looking for package.json)"
exit 1
fi

if [[ "${1:-}" == "--up" ]]; then
direction=" --scope ${scope} --include-dependencies"
direction_desc="('${scope}' and its dependencies)"
else # --down
direction=" --scope ${scope} --include-dependents"
direction_desc="('${scope}' and its consumers)"
fi
scope=$(node -p "require('./package.json').name")

shift
if [[ "$DIRECTION" == "UP" ]]; then
direction=" --scope ${scope} --include-dependencies"
direction_desc="('${scope}' and its dependencies)"
else # --down
direction=" --scope ${scope} --include-dependents"
direction_desc="('${scope}' and its consumers)"
fi
fi

command_arg="${@:-}"

if [ -f "${statefile}" ] && [ -f "${commandfile}" ]; then
command="$(cat ${commandfile})"
if [ ! -z "${command_arg}" ] && [ "${command}" != "${command_arg}" ]; then
Expand All @@ -110,8 +139,8 @@ fi

next="$(head -n1 ${statefile})"
if [ -z "${next}" ]; then
success "done (queue is empty). to reset:"
success " $0 --reset"
success "done (queue is empty). reseting queue:"
reset
exit 0
fi

Expand Down