Skip to content

Commit 3cf6a94

Browse files
authored
chore(scripts): allow --reset --up/--down to automatically run script (#8826)
**[ISSUE]** Honestly, kind of miss being able to use `lerna` for any kind of npm test through parameters with the old `builddown`. Wanted to be able to run something like `builddown build+test` or `builddown test`. **[APPROACH]** Added a `-r | --reset` flag to allow the `foreach.sh` script to reset and run in one line like `builddown`/`buildup`. **[NOTE]** Won't change anyone's current workflow because `--reset` is still in codebase. Also arguments for the script aren't restricted by order (i.e. `foreach.sh --reset --up yarn build ` and `builddown yarn build --up --reset` will produce the same results)! ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d0b3289 commit 3cf6a94

File tree

3 files changed

+68
-49
lines changed

3 files changed

+68
-49
lines changed

scripts/builddown

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ echo " for advanced usage, see ${scriptdir}/foreach.sh"
1111
echo "************************************************************"
1212

1313
if [ "$#" -eq 0 ]; then
14-
${scriptdir}/foreach.sh --reset
14+
${scriptdir}/foreach.sh --reset --down yarn build
1515
else
1616
if [ "$1" != "--resume" ]; then
1717
echo "Unknown option: $1"
1818
exit 1
1919
fi
20+
${scriptdir}/foreach.sh --down yarn build
2021
fi
2122

22-
${scriptdir}/foreach.sh --down yarn build
23-
${scriptdir}/foreach.sh --reset
24-
2523
echo "************************************************************"
2624
echo "builddown done"
2725
echo "************************************************************"

scripts/buildup

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ echo " for advanced usage, see ${scriptdir}/foreach.sh"
1111
echo "************************************************************"
1212

1313
if [ "$#" -eq 0 ]; then
14-
${scriptdir}/foreach.sh --reset
14+
${scriptdir}/foreach.sh --reset --up yarn build
1515
else
1616
if [ "$1" != "--resume" ]; then
1717
echo "Unknown option: $1"
1818
exit 1
1919
fi
20+
${scriptdir}/foreach.sh --up yarn build
2021
fi
2122

22-
${scriptdir}/foreach.sh --up yarn build
23-
${scriptdir}/foreach.sh --reset
24-
2523
echo "************************************************************"
2624
echo "buildup done"
2725
echo "************************************************************"

scripts/foreach.sh

+64-41
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
# if a task fails, it will stop, and then to resume, simply run `foreach.sh` again (with or without the same command).
1414
#
1515
# to reset the session (either when all tasks finished or if you wish to run a different session), run:
16-
# foreach.sh --reset
16+
# foreach.sh [-r | --reset]
17+
#
18+
# to force a reset and run a session with the current, run:
19+
# foreach.sh [-r | --reset] [-u | --up || -d | --down] COMMAND
1720
#
1821
# to run the command only against the current module and its dependencies:
19-
# foreach.sh --up COMMAND
22+
# foreach.sh [-u | --up] COMMAND
2023
#
2124
# to run the command only against the current module and its consumers:
22-
# foreach.sh --down COMMAND
25+
# foreach.sh [-d | --down] COMMAND
2326
#
2427
# --------------------------------------------------------------------------------------------------
2528
set -euo pipefail
@@ -41,52 +44,72 @@ function success {
4144
printf "\e[32;5;81m$@\e[0m\n"
4245
}
4346

44-
if [[ "${1:-}" == "--reset" ]]; then
45-
rm -f "${statedir}/.foreach."*
46-
success "state cleared. you are free to start a new command."
47-
exit 0
47+
function reset {
48+
rm -f "${statedir}/.foreach."*
49+
success "state cleared. you are free to start a new command."
50+
}
51+
52+
DIRECTION=""
53+
RESET=0
54+
SKIP=0
55+
command_arg=""
56+
57+
for arg in "$@"
58+
do
59+
case "$arg" in
60+
-r | --reset) RESET=1 ;;
61+
-s | --skip) SKIP=1 ;;
62+
-u | --up) DIRECTION="UP" ;;
63+
-d | --down) DIRECTION="DOWN" ;;
64+
*) command_arg="$command_arg$arg " ;;
65+
esac
66+
shift
67+
done
68+
69+
if [[ "$RESET" -eq 1 ]]; then
70+
reset
4871
fi
4972

50-
if [[ "${1:-}" == "--skip" ]]; then
51-
if [ ! -f ${statefile} ]; then
52-
error "skip failed. no active sessions found."
53-
exit 1
54-
fi
55-
next=$(head -1 ${statefile})
56-
if [ -z "${next}" ]; then
57-
error "skip failed. queue is empty. to reset:"
58-
error " $0 --reset"
59-
exit 1
60-
fi
61-
tail -n +2 "${statefile}" > "${statefile}.tmp"
62-
cp "${statefile}.tmp" "${statefile}"
63-
success "directory '$next' skipped. re-run the original foreach command to resume."
64-
exit 0
73+
if [[ "$RESET" -eq 1 && "$DIRECTION" == "" ]]; then
74+
exit 0
75+
fi
76+
77+
if [[ "$SKIP" -eq 1 ]]; then
78+
if [ ! -f ${statefile} ]; then
79+
error "skip failed. no active sessions found."
80+
exit 1
81+
fi
82+
next=$(head -1 ${statefile})
83+
if [ -z "${next}" ]; then
84+
error "skip failed. queue is empty. to reset:"
85+
error " $0 --reset"
86+
exit 1
87+
fi
88+
tail -n +2 "${statefile}" > "${statefile}.tmp"
89+
cp "${statefile}.tmp" "${statefile}"
90+
success "directory '$next' skipped. re-run the original foreach command (without --reset) to resume."
91+
exit 0
6592
fi
6693

6794
direction=""
6895
direction_desc=""
69-
if [[ "${1:-}" == "--up" || "${1:-}" == "--down" ]]; then
70-
if [ ! -f package.json ]; then
71-
echo "--up or --down can only be executed from within a module directory (looking for package.json)"
72-
exit 1
73-
fi
74-
75-
scope=$(node -p "require('./package.json').name")
96+
if [[ "$DIRECTION" == "UP" || "$DIRECTION" == "DOWN" ]]; then
97+
if [ ! -f package.json ]; then
98+
error "--up or --down can only be executed from within a module directory (looking for package.json)"
99+
exit 1
100+
fi
76101

77-
if [[ "${1:-}" == "--up" ]]; then
78-
direction=" --scope ${scope} --include-dependencies"
79-
direction_desc="('${scope}' and its dependencies)"
80-
else # --down
81-
direction=" --scope ${scope} --include-dependents"
82-
direction_desc="('${scope}' and its consumers)"
83-
fi
102+
scope=$(node -p "require('./package.json').name")
84103

85-
shift
104+
if [[ "$DIRECTION" == "UP" ]]; then
105+
direction=" --scope ${scope} --include-dependencies"
106+
direction_desc="('${scope}' and its dependencies)"
107+
else # --down
108+
direction=" --scope ${scope} --include-dependents"
109+
direction_desc="('${scope}' and its consumers)"
110+
fi
86111
fi
87112

88-
command_arg="${@:-}"
89-
90113
if [ -f "${statefile}" ] && [ -f "${commandfile}" ]; then
91114
command="$(cat ${commandfile})"
92115
if [ ! -z "${command_arg}" ] && [ "${command}" != "${command_arg}" ]; then
@@ -110,8 +133,8 @@ fi
110133

111134
next="$(head -n1 ${statefile})"
112135
if [ -z "${next}" ]; then
113-
success "done (queue is empty). to reset:"
114-
success " $0 --reset"
136+
success "done (queue is empty). reseting queue:"
137+
reset
115138
exit 0
116139
fi
117140

0 commit comments

Comments
 (0)