-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.sh
More file actions
executable file
·145 lines (121 loc) · 2.39 KB
/
Copy pathclean.sh
File metadata and controls
executable file
·145 lines (121 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
DRY_RUN=0
ASSUME_YES=0
TARGETS=(
"$ROOT_DIR/node_modules"
"$ROOT_DIR/.next"
"$ROOT_DIR/.cache"
"$ROOT_DIR/bin"
"$ROOT_DIR/build"
"$ROOT_DIR/out"
"$ROOT_DIR/coverage"
"$ROOT_DIR/.vercel"
"$ROOT_DIR/public/tiles/osm"
"$ROOT_DIR/tsconfig.tsbuildinfo"
)
log() {
printf '[clean] %s\n' "$*"
}
fail() {
printf '[clean] error: %s\n' "$*" >&2
exit 1
}
run() {
if [[ "$DRY_RUN" == "1" ]]; then
printf '+'
printf ' %q' "$@"
printf '\n'
return 0
fi
"$@"
}
usage() {
cat <<'EOF'
Usage: ./clean.sh [options]
Remove local build/runtime artifacts and leave the repository close to a fresh clone.
This removes only regenerable local outputs such as:
- node_modules
- .next
- .cache
- bin
- build / out / coverage / .vercel
- public/tiles/osm
- tsconfig.tsbuildinfo
Options:
--dry-run Print the actions without deleting anything.
-y, --yes Skip the confirmation prompt.
-h, --help Show this help text.
EOF
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run)
DRY_RUN=1
;;
-y|--yes)
ASSUME_YES=1
;;
-h|--help)
usage
exit 0
;;
*)
fail "Unknown option: $1"
;;
esac
shift
done
}
collect_existing_targets() {
local path
for path in "${TARGETS[@]}"; do
if [[ -e "$path" ]]; then
printf '%s\n' "$path"
fi
done
}
confirm_cleanup() {
local -a existing_targets=("$@")
local answer
if [[ "$DRY_RUN" == "1" || "$ASSUME_YES" == "1" ]]; then
return 0
fi
if [[ ! -t 0 ]]; then
fail "Confirmation required. Re-run with --yes in non-interactive mode."
fi
echo
log "This will remove the following local artifacts:"
printf ' %s\n' "${existing_targets[@]}"
echo
printf 'Proceed? [y/N] '
read -r answer
case "$answer" in
y|Y|yes|YES)
return 0
;;
*)
log "Cancelled."
exit 0
;;
esac
}
main() {
local -a existing_targets
parse_args "$@"
mapfile -t existing_targets < <(collect_existing_targets)
if [[ "${#existing_targets[@]}" -eq 0 ]]; then
log "Nothing to clean."
return
fi
confirm_cleanup "${existing_targets[@]}"
local path
for path in "${existing_targets[@]}"; do
log "Removing $path"
run rm -rf "$path"
done
log "Done."
}
main "$@"