-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.js
More file actions
314 lines (276 loc) · 9.69 KB
/
updater.js
File metadata and controls
314 lines (276 loc) · 9.69 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
// Updater — checks GitHub Releases, then downloads + installs the DMG itself.
//
// Why custom (not electron-updater):
// - The app is ad-hoc signed (no Apple Developer ID). electron-updater's
// Squirrel.Mac flow rejects ad-hoc signatures with "could not get code
// signature for running application". A custom installer based on
// hdiutil + cp + open works regardless of signing identity.
//
// Flow:
// 1. checkForUpdates() hits the GitHub API, returns metadata + the DMG
// asset URL matching the running architecture (arm64/x64).
// 2. downloadAndInstall() streams the DMG to temp with progress callbacks.
// 3. On completion, writes a detached shell script that waits for our PID
// to exit, mounts the DMG, replaces the .app under /Applications (or
// wherever the running .app lives), removes quarantine, and relaunches.
// 4. app.quit() — the script takes over.
const https = require('https');
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const { app } = require('electron');
const GITHUB_OWNER = 'aby-agency';
const GITHUB_REPO = 'aby-claude-watcher';
const WEBSITE_URL = 'https://aby-agency.fr';
const CHECK_INTERVAL = 60 * 60 * 1000;
let lastCheck = 0;
function parseVersion(v) {
if (!v) return [0, 0, 0];
return String(v).replace(/^v/, '').split('.').map(n => parseInt(n, 10) || 0);
}
function isNewer(remote, local) {
const r = parseVersion(remote);
const l = parseVersion(local);
for (let i = 0; i < 3; i++) {
if ((r[i] || 0) > (l[i] || 0)) return true;
if ((r[i] || 0) < (l[i] || 0)) return false;
}
return false;
}
function pickDmgAsset(assets, arch) {
if (!Array.isArray(assets)) return null;
// electron-builder names: "Aby Claude Watcher-1.5.3-arm64.dmg" / "...-x64.dmg"
const tag = `-${arch}.dmg`;
return assets.find(a => a && typeof a.name === 'string' && a.name.endsWith(tag)) || null;
}
function fetchLatestRelease() {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.github.com',
path: `/repos/${GITHUB_OWNER}/${GITHUB_REPO}/releases/latest`,
headers: {
'User-Agent': 'Aby-Claude-Watcher-Updater',
'Accept': 'application/vnd.github.v3+json',
},
timeout: 10000,
};
const req = https.get(options, (res) => {
if (res.statusCode === 404) { resolve(null); return; }
if (res.statusCode !== 200) {
reject(new Error(`GitHub API returned ${res.statusCode}`));
return;
}
let body = '';
res.on('data', (chunk) => { body += chunk; });
res.on('end', () => {
try {
const data = JSON.parse(body);
const asset = pickDmgAsset(data.assets, process.arch);
resolve({
version: (data.tag_name || '').replace(/^v/, ''),
url: data.html_url,
body: data.body || '',
publishedAt: data.published_at,
dmgUrl: asset ? asset.browser_download_url : null,
dmgName: asset ? asset.name : null,
dmgSize: asset ? asset.size : null,
});
} catch (e) {
reject(e);
}
});
});
req.on('error', reject);
req.on('timeout', () => { req.destroy(new Error('timeout')); });
});
}
async function checkForUpdates(force = false) {
const now = Date.now();
if (!force && now - lastCheck < CHECK_INTERVAL) {
return { status: 'rate-limited', lastCheck };
}
lastCheck = now;
const current = app.getVersion();
try {
const latest = await fetchLatestRelease();
if (!latest || !latest.version) {
return { status: 'no-releases', current, lastCheck };
}
if (isNewer(latest.version, current)) {
return {
status: 'update-available',
current,
latest: latest.version,
url: latest.url,
body: latest.body,
dmgUrl: latest.dmgUrl,
dmgName: latest.dmgName,
dmgSize: latest.dmgSize,
canAutoInstall: !!latest.dmgUrl,
lastCheck,
};
}
return { status: 'up-to-date', current, latest: latest.version, lastCheck };
} catch (e) {
return { status: 'error', error: e.message, lastCheck };
}
}
// ─── download + install ───────────────────────────────────────────────────
let activeDownload = null; // { req, dest, abort }
function downloadDmg(dmgUrl, destPath, onProgress) {
return new Promise((resolve, reject) => {
let aborted = false;
let receivedFromHistory = 0;
const cleanup = (err) => {
activeDownload = null;
try { fs.unlinkSync(destPath); } catch {}
reject(err);
};
const open = (url, redirectsLeft = 5) => {
const req = https.get(url, {
headers: { 'User-Agent': 'Aby-Claude-Watcher-Updater' },
timeout: 30000,
}, (res) => {
// GitHub sends a 302 to S3 — follow redirects.
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
if (redirectsLeft <= 0) return cleanup(new Error('too many redirects'));
res.resume();
open(res.headers.location, redirectsLeft - 1);
return;
}
if (res.statusCode !== 200) {
return cleanup(new Error(`download failed: HTTP ${res.statusCode}`));
}
const total = parseInt(res.headers['content-length'] || '0', 10);
let received = receivedFromHistory;
const out = fs.createWriteStream(destPath);
res.on('data', (chunk) => {
received += chunk.length;
if (onProgress && total) {
onProgress({ received, total, percent: Math.min(100, (received / total) * 100) });
}
});
res.on('error', cleanup);
out.on('error', cleanup);
out.on('finish', () => {
activeDownload = null;
out.close(() => resolve(destPath));
});
res.pipe(out);
});
req.on('error', (e) => { if (!aborted) cleanup(e); });
req.on('timeout', () => { req.destroy(new Error('timeout')); });
activeDownload = {
req,
dest: destPath,
abort: () => {
aborted = true;
req.destroy(new Error('aborted'));
try { fs.unlinkSync(destPath); } catch {}
},
};
};
open(dmgUrl);
});
}
function shellEscape(s) {
// single-quote escaping for bash
return `'${String(s).replace(/'/g, `'\\''`)}'`;
}
function getRunningAppPath() {
// process.execPath is like /Applications/Aby Claude Watcher.app/Contents/MacOS/Aby Claude Watcher
// We want /Applications/Aby Claude Watcher.app
const exe = process.execPath;
const macOSDir = path.dirname(exe); // .../Contents/MacOS
const contents = path.dirname(macOSDir); // .../Contents
const appBundle = path.dirname(contents); // .../Aby Claude Watcher.app
return appBundle;
}
function buildInstallScript({ pid, dmgPath, installPath, logPath }) {
// Detached bash script. Stays alive after the parent quits.
// Uses set -u carefully and captures everything to a log for post-mortem.
return `#!/bin/bash
exec >>${shellEscape(logPath)} 2>&1
echo "[$(date)] installer starting (pid=$$, target pid=${pid})"
APP_PID=${pid}
DMG=${shellEscape(dmgPath)}
INSTALL_PATH=${shellEscape(installPath)}
# 1. Wait for the app to exit (max 30s).
for i in $(seq 1 30); do
kill -0 "$APP_PID" 2>/dev/null || break
sleep 1
done
if kill -0 "$APP_PID" 2>/dev/null; then
echo "app did not exit in 30s, forcing"
kill -TERM "$APP_PID" 2>/dev/null || true
sleep 2
fi
# 2. Mount the DMG.
MOUNT_OUT=$(hdiutil attach -nobrowse -readonly "$DMG" 2>&1)
echo "$MOUNT_OUT"
MOUNT_POINT=$(echo "$MOUNT_OUT" | grep -E "/Volumes/" | tail -1 | awk '{ for (i=3; i<=NF; i++) printf "%s%s", $i, (i==NF?"":" ") }')
if [ -z "$MOUNT_POINT" ] || [ ! -d "$MOUNT_POINT" ]; then
echo "failed to mount DMG"
exit 1
fi
SRC_APP="$MOUNT_POINT/Aby Claude Watcher.app"
if [ ! -d "$SRC_APP" ]; then
echo "app not found inside DMG at $SRC_APP"
hdiutil detach "$MOUNT_POINT" -force 2>/dev/null
exit 1
fi
# 3. Replace.
rm -rf "$INSTALL_PATH"
cp -R "$SRC_APP" "$INSTALL_PATH"
xattr -dr com.apple.quarantine "$INSTALL_PATH" 2>/dev/null || true
# 4. Unmount + cleanup.
hdiutil detach "$MOUNT_POINT" -force 2>/dev/null
rm -f "$DMG"
# 5. Relaunch.
open "$INSTALL_PATH"
echo "[$(date)] installer done"
`;
}
async function downloadAndInstall(release, onProgress) {
if (!release || !release.dmgUrl) {
throw new Error('no DMG asset for this architecture');
}
const tmpDir = app.getPath('temp');
const dmgName = release.dmgName || `aby-claude-watcher-${release.latest}-${process.arch}.dmg`;
const dmgPath = path.join(tmpDir, dmgName);
await downloadDmg(release.dmgUrl, dmgPath, onProgress);
const installPath = getRunningAppPath();
const scriptPath = path.join(tmpDir, `aby-claude-watcher-install-${process.pid}.sh`);
const logPath = path.join(tmpDir, `aby-claude-watcher-install-${process.pid}.log`);
fs.writeFileSync(
scriptPath,
buildInstallScript({ pid: process.pid, dmgPath, installPath, logPath }),
{ mode: 0o755 }
);
// Detached so it survives our quit. Stdio fully detached (ignore + unref).
const child = spawn('/bin/bash', [scriptPath], {
detached: true,
stdio: 'ignore',
});
child.unref();
// Give the script a beat to start, then quit.
setTimeout(() => {
try { app.quit(); } catch {}
}, 500);
return { scriptPath, logPath, dmgPath, installPath };
}
function abortActiveDownload() {
if (activeDownload && typeof activeDownload.abort === 'function') {
activeDownload.abort();
return true;
}
return false;
}
module.exports = {
checkForUpdates,
downloadAndInstall,
abortActiveDownload,
GITHUB_OWNER,
GITHUB_REPO,
WEBSITE_URL,
};