-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·174 lines (149 loc) · 5.55 KB
/
update.sh
File metadata and controls
executable file
·174 lines (149 loc) · 5.55 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
#!/bin/bash
set -euo pipefail
# Ignore SIGPIPE: when PM2 restarts the server mid-update (watch detects
# git changes), the parent Node process dies and our stdout pipe breaks.
trap '' PIPE
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT_DIR"
mkdir -p "$ROOT_DIR/data"
# Log file for external command output — keeps noisy git/npm/node output
# off the parent pipe so broken-pipe errors don't abort the update
UPDATE_LOG="$ROOT_DIR/data/update.log"
: > "$UPDATE_LOG"
# Safe echo — swallows EPIPE so broken stdout doesn't trip set -e
log() {
echo "$@" 2>/dev/null || true
}
# Step output helper (parsed by updateExecutor for UI progress)
step() {
local name="$1" status="$2" message="$3"
log "STEP:$name:$status:$message"
}
# Run an external command, routing stdout/stderr to the log file so
# broken-pipe errors from the parent Node process don't abort the update.
# Returns the command's exit code.
run() {
"$@" >> "$UPDATE_LOG" 2>&1
}
log "==================================="
log " PortOS Update"
log "==================================="
log ""
# Resilient npm install — retries once after cleaning node_modules on failure
# Handles ENOTEMPTY and other transient npm bugs
safe_install() {
local dir="${1:-.}"
local label="${dir}"
[ "$dir" = "." ] && label="root"
log "📦 Installing deps ($label)..."
if (cd "$dir" && run npm install); then
return 0
fi
log "⚠️ npm install failed for $label — cleaning node_modules and retrying..."
rm -rf "$dir/node_modules"
if (cd "$dir" && run npm install); then
return 0
fi
log "❌ npm install failed for $label after retry"
return 1
}
# Pull latest — ensure we're on a branch first (old updater may have left
# the repo in detached HEAD after checking out a tag)
step "git-pull" "running" "Pulling latest changes..."
if ! git symbolic-ref -q HEAD >/dev/null 2>&1; then
log "⚠️ Detached HEAD detected — checking out main branch"
run git checkout main
fi
run git pull --rebase --autostash
step "git-pull" "done" "Latest changes pulled"
log ""
# Update submodules (slash-do and any others)
step "submodules" "running" "Updating submodules..."
run git submodule update --init --recursive
step "submodules" "done" "Submodules updated"
log ""
# Stop PM2 apps to release file locks before updating
step "pm2-stop" "running" "Stopping PortOS apps..."
run npm run pm2:stop || true
step "pm2-stop" "done" "Apps stopped"
log ""
# Update dependencies with retry logic
step "npm-install" "running" "Installing all dependencies..."
safe_install .
safe_install client
safe_install server
safe_install autofixer
# Run trusted install scripts skipped by ignore-scripts=true in .npmrc
log "🔧 Rebuilding esbuild & node-pty..."
run node client/node_modules/esbuild/install.js
run node server/node_modules/esbuild/install.js
run npm rebuild node-pty --prefix server
log ""
# Verify critical dependencies exist
if [ ! -f "client/node_modules/vite/bin/vite.js" ]; then
log "❌ Critical dependency missing: client/node_modules/vite"
log " Try running: npm run install:all"
exit 1
fi
step "npm-install" "done" "Dependencies installed"
# Run setup (data dirs + browser deps)
step "setup" "running" "Running setup..."
run npm run setup
run node scripts/setup-ghostty.js || true
step "setup" "done" "Setup complete"
log ""
# Run data migrations
step "migrations" "running" "Running data migrations..."
if [ -f "$ROOT_DIR/scripts/run-migrations.js" ]; then
run node "$ROOT_DIR/scripts/run-migrations.js"
fi
step "migrations" "done" "Migrations complete"
# Check for slash-do (optional, used by the PR Reviewer schedule task)
if ! command -v slash-do >/dev/null 2>&1; then
log "slash-do is not installed. It is used by the PR Reviewer schedule task."
if [ -t 0 ]; then
read -p "Install slash-do now? [y/N] " -n 1 -r
log ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
log "Installing slash-do..."
if ! run npm install -g slash-do@latest; then
log "⚠️ Failed to install slash-do. Continuing without it."
fi
else
log "Skipping slash-do install. You can install later with: npm install -g slash-do@latest"
fi
else
log "Skipping slash-do prompt (non-interactive). Install later with: npm install -g slash-do@latest"
fi
log ""
fi
# Build UI assets for production serving
step "build" "running" "Building client..."
run npm run build
step "build" "done" "Client built"
log ""
# Determine post-update version from package.json (fail if unreadable)
TAG=$(node -e 'const pkg = JSON.parse(require("fs").readFileSync("package.json","utf8")); if (typeof pkg.version !== "string") process.exit(1); process.stdout.write(pkg.version.trim());')
if [ -z "$TAG" ]; then
log "❌ Failed to determine package version from package.json"
exit 1
fi
# Write completion marker atomically via Node (version passed as env var to avoid injection)
TAG="$TAG" ROOT_DIR="$ROOT_DIR" node -e '
const fs = require("fs");
const path = require("path");
const marker = JSON.stringify({ version: process.env.TAG, completedAt: new Date().toISOString() });
fs.writeFileSync(path.join(process.env.ROOT_DIR, "data", "update-complete.json.tmp"), marker);
' && mv "$ROOT_DIR/data/update-complete.json.tmp" "$ROOT_DIR/data/update-complete.json"
# Restart PM2 apps — remove marker if restart fails so it isn't misread on boot
step "restart" "running" "Restarting PortOS..."
if ! run npm run pm2:restart; then
rm -f "$ROOT_DIR/data/update-complete.json"
exit 1
fi
step "restart" "done" "PortOS restarted"
log ""
log "==================================="
log " ✅ Update Complete!"
log "==================================="
log ""