13
13
# if a task fails, it will stop, and then to resume, simply run `foreach.sh` again (with or without the same command).
14
14
#
15
15
# 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
17
20
#
18
21
# to run the command only against the current module and its dependencies:
19
- # foreach.sh --up COMMAND
22
+ # foreach.sh [-u | --up] COMMAND
20
23
#
21
24
# to run the command only against the current module and its consumers:
22
- # foreach.sh -- down COMMAND
25
+ # foreach.sh [-d | -- down] COMMAND
23
26
#
24
27
# --------------------------------------------------------------------------------------------------
25
28
set -euo pipefail
@@ -41,52 +44,72 @@ function success {
41
44
printf " \e[32;5;81m$@ \e[0m\n"
42
45
}
43
46
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
48
71
fi
49
72
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
65
92
fi
66
93
67
94
direction=" "
68
95
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
76
101
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" )
84
103
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
86
111
fi
87
112
88
- command_arg=" ${@:- } "
89
-
90
113
if [ -f " ${statefile} " ] && [ -f " ${commandfile} " ]; then
91
114
command=" $( cat ${commandfile} ) "
92
115
if [ ! -z " ${command_arg} " ] && [ " ${command} " != " ${command_arg} " ]; then
110
133
111
134
next=" $( head -n1 ${statefile} ) "
112
135
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
115
138
exit 0
116
139
fi
117
140
0 commit comments