-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·346 lines (272 loc) · 7.79 KB
/
Copy pathrelease.sh
File metadata and controls
executable file
·346 lines (272 loc) · 7.79 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
cd "$ROOT_DIR"
APPROVAL_URL_REGEX='https://www\.npmjs\.com/auth/cli/[A-Za-z0-9._~:/?#\[\]@!$&()*+,;=%-]+'
CHECKS_PASSED=()
LIVE_VERSION=""
LOCAL_VERSION=""
RELEASE_VERSION=""
PUBLISH_SUCCEEDED="no"
TAG_CREATED="no"
log() {
printf '[release] %s\n' "$*"
}
die() {
printf '[release] Error: %s\n' "$*" >&2
exit 1
}
print_clickable_link() {
local label=$1
local url=$2
if [[ -t 1 ]]; then
printf '[release] %s: \033]8;;%s\a%s\033]8;;\a\n' "$label" "$url" "$label"
printf '[release] Approval URL: %s\n' "$url"
else
printf '[release] %s: %s\n' "$label" "$url"
fi
}
extract_approval_url() {
local line=${1//$'\r'/}
if [[ $line =~ $APPROVAL_URL_REGEX ]]; then
printf '%s\n' "${BASH_REMATCH[0]}"
return 0
fi
return 1
}
append_check() {
CHECKS_PASSED+=("$1")
}
join_checks() {
local joined=""
local item
for item in "${CHECKS_PASSED[@]}"; do
if [[ -n $joined ]]; then
joined+=", "
fi
joined+="$item"
done
printf '%s' "$joined"
}
print_summary() {
local exit_code=$1
local checks_summary="none"
set +e
if ((${#CHECKS_PASSED[@]} > 0)); then
checks_summary=$(join_checks)
fi
printf '\n[release] Summary\n'
if [[ -n $RELEASE_VERSION ]]; then
printf '[release] Version: %s\n' "$RELEASE_VERSION"
else
printf '[release] Version: not bumped\n'
fi
printf '[release] Publish succeeded: %s\n' "$PUBLISH_SUCCEEDED"
printf '[release] Git tag created: %s\n' "$TAG_CREATED"
printf '[release] Checks passed: %s\n' "$checks_summary"
if [[ $exit_code -ne 0 ]]; then
printf '[release] Exit code: %s\n' "$exit_code" >&2
fi
}
trap 'print_summary "$?"' EXIT
run_streamed_pty_command() {
local approval_label=$1
local command_string=$2
local command_status
command -v script >/dev/null 2>&1 || die "The 'script' command is required for interactive npm steps."
set +e
script -qefc "$command_string" /dev/null 2>&1 | {
declare -A seen_urls=()
line=""
url=""
while IFS= read -r line; do
printf '%s\n' "$line"
if url=$(extract_approval_url "$line"); then
if [[ -z ${seen_urls["$url"]+x} ]]; then
seen_urls["$url"]=1
print_clickable_link "$approval_label" "$url"
fi
fi
done
}
command_status=${PIPESTATUS[0]}
set -e
return "$command_status"
}
assert_simple_semver() {
local version=$1
[[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "Expected a simple x.y.z version, got '$version'."
}
ensure_npm_auth() {
local whoami_output=""
local whoami_status=0
log "Checking npm auth with npm whoami"
set +e
whoami_output=$(npm whoami 2>&1)
whoami_status=$?
set -e
if [[ $whoami_status -eq 0 ]]; then
printf '%s\n' "$whoami_output"
return 0
fi
printf '%s\n' "$whoami_output" >&2
if grep -q '401' <<<"$whoami_output"; then
log "npm auth is missing, starting npm login"
run_streamed_pty_command "Approve npm login" "env BROWSER=true npm login"
log "Re-checking npm auth after login"
npm whoami >/dev/null
return 0
fi
die "npm whoami failed before release verification."
}
load_versions() {
log "Checking live npm version"
LIVE_VERSION=$(npm view letmecode version | tr -d '\r\n')
[[ -n $LIVE_VERSION ]] || die "npm view letmecode version returned an empty result."
assert_simple_semver "$LIVE_VERSION"
log "Reading local package version"
LOCAL_VERSION=$(node -p "require('./package.json').version")
[[ -n $LOCAL_VERSION ]] || die "package.json version is empty."
assert_simple_semver "$LOCAL_VERSION"
log "Live version: $LIVE_VERSION"
log "Local version: $LOCAL_VERSION"
}
compute_release_version() {
local highest_version
local major
local minor
local patch
highest_version=$(printf '%s\n%s\n' "$LIVE_VERSION" "$LOCAL_VERSION" | sort -V | tail -n 1)
IFS=. read -r major minor patch <<<"$highest_version"
RELEASE_VERSION="$major.$minor.$((patch + 1))"
[[ $RELEASE_VERSION != "$LOCAL_VERSION" ]] || die "Refusing to publish the current version unchanged."
log "Next release version: $RELEASE_VERSION"
}
run_verification_checks() {
log "Running pnpm test"
pnpm test
append_check "pnpm test"
log "Running npm pack --dry-run"
npm pack --dry-run
append_check "npm pack --dry-run"
log "Running pnpm start smoke test"
ROOT_DIR_FOR_SMOKE="$ROOT_DIR" python3 - <<'PY'
import errno
import fcntl
import os
import pty
import select
import subprocess
import sys
import termios
import time
root_dir = os.environ["ROOT_DIR_FOR_SMOKE"]
ready_token = "LetMeCode Usage Dashboard"
deadline = time.time() + 180
master_fd, slave_fd = pty.openpty()
if sys.stdout.isatty():
try:
winsize = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, b"\0" * 8)
fcntl.ioctl(slave_fd, termios.TIOCSWINSZ, winsize)
except OSError:
pass
process = subprocess.Popen(
["pnpm", "start"],
cwd=root_dir,
stdin=slave_fd,
stdout=slave_fd,
stderr=slave_fd,
env=os.environ.copy(),
close_fds=True,
)
os.close(slave_fd)
seen_output = ""
sent_quit = False
try:
while True:
if process.poll() is not None:
break
if time.time() > deadline:
process.terminate()
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
process.wait(timeout=5)
print("\n[release] Smoke test timed out before the dashboard appeared.", file=sys.stderr)
sys.exit(1)
ready, _, _ = select.select([master_fd], [], [], 0.1)
if master_fd not in ready:
continue
try:
chunk = os.read(master_fd, 4096)
except OSError as exc:
if exc.errno == errno.EIO:
break
raise
if not chunk:
break
os.write(sys.stdout.fileno(), chunk)
seen_output = (seen_output + chunk.decode("utf-8", "ignore"))[-200000:]
if not sent_quit and ready_token in seen_output:
os.write(master_fd, b"q")
sent_quit = True
while True:
try:
chunk = os.read(master_fd, 4096)
except OSError as exc:
if exc.errno == errno.EIO:
break
raise
if not chunk:
break
os.write(sys.stdout.fileno(), chunk)
finally:
os.close(master_fd)
exit_code = process.wait()
if not sent_quit:
print("\n[release] Smoke test exited before the dashboard was detected.", file=sys.stderr)
sys.exit(1)
sys.exit(exit_code)
PY
append_check "pnpm start"
}
bump_package_version() {
log "Bumping package.json to $RELEASE_VERSION"
TARGET_VERSION="$RELEASE_VERSION" node - <<'NODE'
const fs = require("node:fs");
const path = require("node:path");
const packageJsonPath = path.join(process.cwd(), "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
if (!packageJson.bin || packageJson.bin.letmecode !== "./bin/letmecode.js") {
throw new Error('Expected packageJson.bin.letmecode to remain "./bin/letmecode.js".');
}
packageJson.version = process.env.TARGET_VERSION;
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);
NODE
}
commit_version_bump() {
log "Committing version bump"
git commit -am "Bump version to $RELEASE_VERSION"
}
publish_release() {
log "Publishing letmecode@$RELEASE_VERSION"
run_streamed_pty_command "Approve npm publish" "env BROWSER=true npm publish"
PUBLISH_SUCCEEDED="yes"
}
create_release_tag() {
log "Creating git tag v$RELEASE_VERSION"
git tag "v$RELEASE_VERSION"
TAG_CREATED="yes"
}
main() {
ensure_npm_auth
load_versions
compute_release_version
run_verification_checks
bump_package_version
commit_version_bump
publish_release
create_release_tag
}
main "$@"