-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·542 lines (474 loc) · 20.3 KB
/
release.sh
File metadata and controls
executable file
·542 lines (474 loc) · 20.3 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#!/bin/bash
# Interactive release script for TypeScript/JavaScript projects
# Usage: ./release.sh [--yes|-y] [version]
# Example: ./release.sh 0.2.0
# ./release.sh --yes # silent mode, auto-accept defaults
# ./release.sh --yes 0.2.0 # silent mode with specific version
#
# Auto-detects project name from package.json and GitHub repo from git remote.
# Override with environment variables: PROJECT_NAME, GITHUB_REPO
# Note: set -e is disabled to allow step-by-step execution
# Individual steps will handle their own error handling
# Silent mode flag (--yes or -y to auto-accept all defaults)
SILENT=false
POSITIONAL_ARGS=()
for arg in "$@"; do
case "$arg" in
--yes|-y) SILENT=true ;;
*) POSITIONAL_ARGS+=("$arg") ;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Auto-detect project name from package.json (unless set via env)
if [ -z "$PROJECT_NAME" ]; then
PROJECT_NAME=$(node -e "console.log(require('./package.json').name)" 2>/dev/null)
if [ -z "$PROJECT_NAME" ]; then
echo -e "${RED}Error: Could not determine project name from package.json${NC}"
echo -e "${YELLOW}Set PROJECT_NAME environment variable or check package.json${NC}"
exit 1
fi
fi
# Auto-detect GitHub repo from git remote (unless set via env)
if [ -z "$GITHUB_REPO" ]; then
REMOTE_URL=$(git remote get-url origin 2>/dev/null)
if [ -n "$REMOTE_URL" ]; then
GITHUB_REPO=$(echo "$REMOTE_URL" | sed -E 's|.*github\.com[:/]||; s|\.git$||')
fi
if [ -z "$GITHUB_REPO" ]; then
echo -e "${YELLOW}Warning: Could not detect GitHub repo from git remote${NC}"
echo -e "${YELLOW}Set GITHUB_REPO environment variable (e.g. owner/repo)${NC}"
fi
fi
# Get version from argument or package.json
if [ -z "$1" ]; then
VERSION=$(node -e "console.log(require('./package.json').version)" 2>/dev/null)
if [ -z "$VERSION" ]; then
echo -e "${RED}Error: Could not determine version from package.json${NC}"
exit 1
fi
else
VERSION="$1"
fi
TAG="typescript/v${VERSION}"
echo -e "${CYAN}╔══════════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║ ${PROJECT_NAME} Release Script v${VERSION}${NC}"
echo -e "${CYAN}╚══════════════════════════════════════════════════════════╝${NC}"
echo -e " Project: ${CYAN}${PROJECT_NAME}${NC}"
[ -n "$GITHUB_REPO" ] && echo -e " GitHub: ${CYAN}${GITHUB_REPO}${NC}"
echo ""
# Function to check if step is already done
check_tag_exists() {
if git rev-parse "${TAG}" >/dev/null 2>&1; then
if git ls-remote --tags origin | grep -q "refs/tags/${TAG}$"; then
return 0 # Tag exists on remote
fi
fi
return 1 # Tag doesn't exist
}
check_npm_uploaded() {
# Check if version exists on npm registry
npm view "${PROJECT_NAME}" versions --json 2>/dev/null \
| grep -qF "\"${VERSION}\"" && return 0 || return 1
}
check_release_exists() {
if command -v gh &> /dev/null; then
gh release view "${TAG}" &>/dev/null && return 0 || return 1
fi
return 1
}
# Function to ask yes/no with default
ask_yn() {
local prompt="$1"
local default="$2"
local answer
# Silent mode: auto-accept default
if [ "$SILENT" = true ]; then
if [ "$default" = "y" ]; then
echo -e "${YELLOW}${prompt} [Y/n] y (auto)${NC}"
return 0
else
echo -e "${YELLOW}${prompt} [y/N] n (auto)${NC}"
return 1
fi
fi
if [ "$default" = "y" ]; then
prompt="${prompt} [Y/n]"
else
prompt="${prompt} [y/N]"
fi
read -p "$(echo -e "${YELLOW}${prompt}${NC}") " answer
answer=${answer:-$default}
[[ $answer =~ ^[Yy]$ ]]
}
# Function to show main menu with all steps
show_main_menu() {
# Silent mode: auto-select "all"
if [ "$SILENT" = true ]; then
echo -e "${CYAN}Silent mode: auto-selecting all steps${NC}"
MENU_SELECTION="all"
return
fi
echo ""
echo -e "${CYAN}╔══════════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║ Release Steps Selection ║${NC}"
echo -e "${CYAN}╚══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${CYAN}Select a step to execute:${NC}"
echo -e " ${CYAN}all) Execute all steps (with interactive prompts)${NC}"
echo -e " ${CYAN}1) Step 1: Version Verification${NC}"
echo -e " ${CYAN}2) Step 2: Check Current Status${NC}"
echo -e " ${CYAN}3) Step 3: Clean Build Files${NC}"
echo -e " ${CYAN}4) Step 4: Build Package${NC}"
echo -e " ${CYAN}5) Step 5: Check Package${NC}"
echo -e " ${CYAN}6) Step 6: Git Tag${NC}"
echo -e " ${CYAN}7) Step 6.5: Create GitHub Release${NC}"
echo -e " ${CYAN}8) Step 7: Upload to npm${NC}"
echo -e " ${CYAN}9) Show Summary${NC}"
echo -e " ${CYAN}0) Exit${NC}"
echo ""
read -p "$(echo -e "${YELLOW}Select option [all]:${NC} ") " MENU_SELECTION
MENU_SELECTION=${MENU_SELECTION:-all}
}
# Step functions
step1_version_verification() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 1: Version Verification${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
PKG_VERSION=$(node -e "console.log(require('./package.json').version)" 2>/dev/null)
echo -e " package.json: ${CYAN}${PKG_VERSION}${NC}"
echo -e " Script version: ${CYAN}${VERSION}${NC}"
if [ "$PKG_VERSION" != "$VERSION" ]; then
echo -e "${RED}Version mismatch detected!${NC}"
return 1
fi
echo -e "${GREEN}All versions match${NC}"
echo ""
return 0
}
step2_check_status() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 2: Checking Current Status${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
# Check Git tag
if check_tag_exists; then
echo -e " Git Tag: ${GREEN}Tag ${TAG} exists on remote${NC}"
else
echo -e " Git Tag: Tag ${TAG} not found on remote"
fi
# Check build files
if [ -d "dist" ] && [ "$(ls -A dist/*.js dist/*.cjs 2>/dev/null | wc -l)" -gt 0 ]; then
echo -e " Build Files: ${GREEN}Found in dist/${NC}"
ls -lh dist/ | tail -n +2 | sed 's/^/ /'
else
echo -e " Build Files: Not found"
fi
# Check npm registry
if command -v npm &> /dev/null; then
if check_npm_uploaded; then
echo -e " npm Upload: ${GREEN}Version ${VERSION} found on npm${NC}"
else
echo -e " npm Upload: Version ${VERSION} not found on npm"
fi
else
echo -e " npm Upload: (cannot check - npm not available)"
fi
echo ""
return 0
}
step3_clean_build() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 3: Clean Build Files${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if [ -d "dist" ]; then
echo -e "${YELLOW}Found existing build files${NC}"
if ask_yn "Clean build files? (dist/, node_modules/.cache/)" "y"; then
rm -rf dist/ node_modules/.cache/
echo -e "${GREEN}Cleaned${NC}"
else
echo -e "${YELLOW}Skipped cleaning${NC}"
fi
else
echo -e "${GREEN}No build files to clean${NC}"
fi
echo ""
return 0
}
step4_build_package() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 4: Build Package${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if [ -d "dist" ] && [ "$(ls -A dist/*.js 2>/dev/null | wc -l)" -gt 0 ]; then
echo -e "${GREEN}Build files already exist${NC}"
if ask_yn "Rebuild package?" "n"; then
SKIP_BUILD=false
else
SKIP_BUILD=true
fi
else
SKIP_BUILD=false
fi
if [ "$SKIP_BUILD" = false ]; then
if [ ! -d "node_modules" ]; then
echo -e "${YELLOW}Installing dependencies...${NC}"
if ! pnpm install; then
echo -e "${RED}Dependency installation failed${NC}"
return 1
fi
fi
echo -e "${YELLOW}Building package...${NC}"
if ! pnpm build; then
echo -e "${RED}Build failed${NC}"
return 1
fi
echo -e "${GREEN}Package built successfully${NC}"
echo ""
echo -e "${CYAN}Built files:${NC}"
ls -lh dist/ | tail -n +2 | sed 's/^/ /'
else
echo -e "${YELLOW}Skipped build (using existing files)${NC}"
fi
echo ""
return 0
}
step5_check_package() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 5: Check Package${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if ask_yn "Check package with npm pack --dry-run?" "y"; then
if ! npm pack --dry-run; then
echo -e "${RED}Package check failed${NC}"
return 1
fi
echo -e "${GREEN}Package check passed${NC}"
else
echo -e "${YELLOW}Skipped package check${NC}"
fi
echo ""
return 0
}
step6_git_tag() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 6: Git Tag${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if check_tag_exists; then
echo -e "${GREEN}Tag ${TAG} already exists on remote${NC}"
if ask_yn "Create/update tag anyway?" "n"; then
SKIP_TAG=false
else
SKIP_TAG=true
fi
else
SKIP_TAG=false
if git rev-parse "${TAG}" >/dev/null 2>&1; then
echo -e "${YELLOW}Tag ${TAG} exists locally but not on remote${NC}"
if ask_yn "Push existing tag to remote?" "y"; then
git push origin "${TAG}"
echo -e "${GREEN}Tag pushed${NC}"
SKIP_TAG=true
fi
fi
fi
if [ "$SKIP_TAG" = false ]; then
if ask_yn "Create Git tag ${TAG}?" "y"; then
if ! git diff-index --quiet HEAD --; then
echo -e "${YELLOW}Warning: You have uncommitted changes${NC}"
git status --short
if ! ask_yn "Continue anyway?" "n"; then
return 1
fi
fi
git tag -a "${TAG}" -m "Release version ${VERSION}"
echo -e "${GREEN}Tag created${NC}"
if ask_yn "Push tag to remote?" "y"; then
git push origin "${TAG}"
echo -e "${GREEN}Tag pushed to remote${NC}"
else
echo -e "${YELLOW}Tag not pushed. Push manually with: git push origin ${TAG}${NC}"
fi
else
echo -e "${YELLOW}Skipped tag creation${NC}"
fi
fi
echo ""
return 0
}
step6_5_create_github_release() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 6.5: Create GitHub Release${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if [ -z "$GITHUB_REPO" ]; then
echo -e "${YELLOW}GitHub repo not detected. Set GITHUB_REPO env variable (e.g. owner/repo)${NC}"
echo ""
return 0
fi
SKIP_RELEASE=false
if ! command -v gh &> /dev/null; then
echo -e "${YELLOW}GitHub CLI (gh) not found. Install with: brew install gh${NC}"
echo -e "${CYAN}Create release manually at: https://github.com/${GITHUB_REPO}/releases/new${NC}"
SKIP_RELEASE=true
else
if ! gh auth status &>/dev/null; then
echo -e "${YELLOW}GitHub CLI not authenticated. Run: gh auth login${NC}"
SKIP_RELEASE=true
fi
fi
if [ "$SKIP_RELEASE" = false ]; then
if check_release_exists; then
echo -e "${GREEN}Release ${TAG} already exists${NC}"
if ! ask_yn "Update existing release?" "n"; then
SKIP_RELEASE=true
fi
fi
fi
if [ "$SKIP_RELEASE" = false ]; then
if ask_yn "Create GitHub Release ${TAG}?" "y"; then
RELEASE_NOTES=""
if [ -f "CHANGELOG.md" ]; then
RELEASE_NOTES=$(awk "
/^## \[${VERSION}\]/ {found=1; next}
found && /^## \[/ {exit}
found {print}
" CHANGELOG.md)
RELEASE_NOTES=$(echo "$RELEASE_NOTES" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
fi
if [ -z "$RELEASE_NOTES" ]; then
RELEASE_NOTES="Release version ${VERSION}"
fi
NOTES_FILE=$(mktemp)
echo "$RELEASE_NOTES" > "$NOTES_FILE"
if gh release create "${TAG}" \
--title "Release ${VERSION}" \
--notes-file "$NOTES_FILE" \
--repo "${GITHUB_REPO}"; then
echo -e "${GREEN}GitHub Release created successfully${NC}"
echo -e "${CYAN}https://github.com/${GITHUB_REPO}/releases/tag/${TAG}${NC}"
else
echo -e "${RED}Failed to create GitHub Release${NC}"
fi
rm -f "$NOTES_FILE"
fi
fi
echo ""
return 0
}
step7_upload_npm() {
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Step 7: Upload to npm${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if check_npm_uploaded; then
echo -e "${GREEN}Version ${VERSION} already exists on npm${NC}"
if ! ask_yn "Upload anyway? (will fail if version exists)" "n"; then
echo -e "${YELLOW}Skipped npm upload (version already exists)${NC}"
echo ""
return 0
fi
fi
if ask_yn "Upload to npm?" "y"; then
echo -e "${YELLOW}Publishing to npm...${NC}"
if ! npm publish --access public; then
echo -e "${RED}Upload to npm failed${NC}"
return 1
fi
echo -e "${GREEN}Successfully published to npm!${NC}"
else
echo -e "${YELLOW}Skipped npm upload${NC}"
echo -e "${CYAN}Upload manually with: npm publish --access public${NC}"
fi
echo ""
return 0
}
step_summary() {
echo -e "${CYAN}╔══════════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║ Release Summary ║${NC}"
echo -e "${CYAN}╚══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e " Version: ${CYAN}${VERSION}${NC}"
echo -e " Tag: ${CYAN}${TAG}${NC}"
echo ""
if [ -n "$GITHUB_REPO" ]; then
if check_release_exists; then
echo -e " ${GREEN}GitHub Release:${NC}"
echo -e " https://github.com/${GITHUB_REPO}/releases/tag/${TAG}"
else
echo -e " GitHub Release: Not created yet"
fi
fi
if [ -d "dist" ] && [ "$(ls -A dist/*.js 2>/dev/null | wc -l)" -gt 0 ]; then
echo -e " ${GREEN}Package built: dist/${NC}"
else
echo -e " Package: Not built"
fi
if check_npm_uploaded; then
echo -e " ${GREEN}npm: https://www.npmjs.com/package/${PROJECT_NAME}/v/${VERSION}${NC}"
else
echo -e " npm: Not uploaded yet"
fi
echo ""
echo -e "${GREEN}Release script completed!${NC}"
echo ""
echo -e "${CYAN}Next steps:${NC}"
echo " 1. Verify installation: npm install ${PROJECT_NAME}@${VERSION}"
echo " 2. Update CHANGELOG.md with [Unreleased] section for next version"
echo ""
return 0
}
# Main execution logic
while true; do
show_main_menu
SELECTION="$MENU_SELECTION"
if [ -z "$SELECTION" ]; then
echo -e "${RED}No selection made. Please try again.${NC}"
continue
fi
case "$SELECTION" in
all|ALL|a|A)
echo ""
echo -e "${CYAN}Executing all steps with interactive prompts...${NC}"
echo ""
# Helper: run a step, abort in silent mode on failure
run_step() {
local step_name="$1"
shift
if ! "$@"; then
if [ "$SILENT" = true ]; then
echo -e "${RED}${step_name} failed. Aborting (silent mode).${NC}"
exit 1
else
echo -e "${YELLOW}${step_name} failed, continuing...${NC}"
fi
fi
}
if ! step1_version_verification; then
echo -e "${RED}Version verification failed. Exiting.${NC}"
exit 1
fi
step2_check_status
run_step "Step 3" step3_clean_build
run_step "Step 4" step4_build_package
run_step "Step 5" step5_check_package
run_step "Step 6" step6_git_tag
run_step "Step 6.5" step6_5_create_github_release
run_step "Step 7" step7_upload_npm
step_summary
break
;;
1) step1_version_verification || echo -e "${RED}Version verification failed.${NC}" ;;
2) step2_check_status ;;
3) step3_clean_build ;;
4) step4_build_package ;;
5) step5_check_package ;;
6) step6_git_tag ;;
7) step6_5_create_github_release ;;
8) step7_upload_npm ;;
9) step_summary ;;
0) echo -e "${CYAN}Exiting...${NC}"; exit 0 ;;
*) echo -e "${RED}Invalid selection. Please choose a valid option.${NC}" ;;
esac
done