-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaatchi.sh
More file actions
143 lines (123 loc) · 3.65 KB
/
Copy pathsaatchi.sh
File metadata and controls
143 lines (123 loc) · 3.65 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
#!/usr/bin/env bash
# composition + teardown trap. no flags, no arguments, ever.
set -euo pipefail
if [[ $# -ne 0 ]]; then
printf '%s\n' "saatchi: no flags, no arguments, ever"
exit 1
fi
SAATCHI_ROOT="${SAATCHI_ROOT:-$(cd "$(dirname "$0")" && pwd)}"
source "${SAATCHI_ROOT}/lib/lifecycle.sh"
cwd="$PWD"
dot="$cwd/.saatchi"
app_pid=""
run_dir=""
teardown() {
# ignore further signals: a second ^C in the grace window must not
# abort before KILL, and HUP/QUIT must not skip us
trap '' EXIT INT TERM HUP QUIT
if [[ -n "${app_pid}" ]]; then
kill_gracefully "${app_pid}"
fi
if [[ -n "${run_dir}" && -d "${run_dir}" ]]; then
rm -rf "${run_dir}"
fi
}
trap teardown EXIT INT TERM HUP QUIT
indent() {
while IFS= read -r line || [[ -n "${line}" ]]; do
printf ' %s\n' "${line}"
done
}
scaffold() {
mkdir -p "${dot}/fixtures"
cp "${SAATCHI_ROOT}/scaffold/mod.just" "${dot}/mod.just"
cp "${SAATCHI_ROOT}/scaffold/evidence.ts" "${dot}/evidence.ts"
cp "${SAATCHI_ROOT}/scaffold/gitignore" "${dot}/.gitignore"
if [[ -d "${SAATCHI_ROOT}/scaffold/fixtures" ]]; then
cp -r "${SAATCHI_ROOT}/scaffold/fixtures/." "${dot}/fixtures/"
fi
cat <<'EOF'
saatchi: no .saatchi/ here — scaffolded one:
.saatchi/mod.just ← EDIT ME: the `serve` recipe starts your app
.saatchi/evidence.ts ← the example section; make it yours
.saatchi/fixtures/ ← default data to serve
.saatchi/.gitignore
saatchi: edit mod.just, then run me again.
EOF
}
if [[ ! -d "${dot}" ]]; then
scaffold
exit 0
fi
if [[ ! -f "${dot}/evidence.ts" ]]; then
printf '%s\n' "saatchi: FAIL — .saatchi/evidence.ts is missing"
exit 1
fi
if [[ ! -f "${dot}/mod.just" ]]; then
printf '%s\n' "saatchi: FAIL — .saatchi/mod.just is missing"
exit 1
fi
mkdir -p "${dot}/home" "${dot}/shots"
# a run starts with a clean shot list; failure keeps this run's shots-so-far
(
shopt -s nullglob
for f in "${dot}/shots"/*; do
rm -rf "${f}"
done
)
if [[ -d "${dot}/data" ]]; then
DATA="${dot}/data"
else
DATA="${dot}/fixtures"
fi
HOME_DIR="${dot}/home"
PORT="$(bun -e 'const s = Bun.serve({ port: 0, fetch() { return new Response("") } }); process.stdout.write(String(s.port)); s.stop()')"
export PORT
now_ms() { bun -e 'process.stdout.write(String(Date.now()))'; }
t0="$(now_ms)"
export SAATCHI_START_MS="${t0}"
export SAATCHI_EVIDENCE="${dot}/evidence.ts"
export SAATCHI_SHOTS="${dot}/shots"
run_dir="$(mktemp -d "${TMPDIR:-/tmp}/saatchi.XXXXXX")"
mkdir -p "${run_dir}/node_modules"
ln -s "${PLAYWRIGHT_CORE:?PLAYWRIGHT_CORE is not set}" "${run_dir}/node_modules/playwright-core"
ln -s "${PLAYWRIGHT_CORE}" "${run_dir}/node_modules/playwright"
cp "${SAATCHI_ROOT}/lib/"*.ts "${run_dir}/"
: > "${dot}/app.log"
set -m
PORT="${PORT}" HOME="${HOME_DIR}" DATA="${DATA}" \
just -f "${dot}/mod.just" serve >>"${dot}/app.log" 2>&1 </dev/null &
app_pid=$!
set +m
export SAATCHI_APP_PID="${app_pid}"
code=0
bun "${run_dir}/drive.ts" || code=$?
elapsed() {
bun -e "process.stdout.write(((Date.now() - ${t0}) / 1000).toFixed(1))"
}
shot_count() {
(
shopt -s nullglob
n=0
# webm counts too: a recording whose transcode failed is still a kept shot
for f in "${dot}/shots"/*.png "${dot}/shots"/*.mp4 "${dot}/shots"/*.webm; do
n=$((n + 1))
done
printf '%s' "${n}"
)
}
if [[ "${code}" -eq 0 ]]; then
n="$(shot_count)"
printf 'saatchi: clean (%s shots, %ss)\n' "${n}" "$(elapsed)"
exit 0
fi
if [[ "${code}" -eq 2 ]]; then
printf 'saatchi: app.log ↓\n'
indent < "${dot}/app.log"
exit 2
fi
printf 'saatchi: app.log tail ↓\n'
tail -n 20 "${dot}/app.log" | indent
n="$(shot_count)"
printf 'saatchi: kept %s shots; .saatchi/ left as-is; exit 1\n' "${n}"
exit 1