@jmanuelcorral/bond-app 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -15
- package/app/mods/bond-agents/main.js +3 -3
- package/app/mods/bond-agents/model.js +24 -2
- package/app/mods/bond-board/main.js +327 -18
- package/app/mods/bond-board/model.js +74 -6
- package/app/mods/bond-settings/main.js +66 -3
- package/package.json +1 -1
- package/runner/versions.env +1 -1
- package/scripts/app-install.ts +262 -26
- package/src/cli.ts +512 -21
- package/src/config.ts +15 -3
- package/src/core/agy-login.ts +2 -1
- package/src/core/delivery.ts +122 -5
- package/src/core/doctor.ts +748 -0
- package/src/core/hub-session.ts +108 -91
- package/src/core/hub.ts +40 -8
- package/src/core/orchestrator.ts +755 -119
- package/src/core/paths.ts +107 -0
- package/src/core/permissions.ts +143 -0
- package/src/core/pr-source.ts +147 -0
- package/src/core/service-windows.ts +180 -0
- package/src/core/service.ts +861 -0
- package/src/core/setup-install.ts +1380 -0
- package/src/db/database.ts +13 -6
- package/src/db/ticket-repo.ts +29 -7
- package/src/runner/azure-devops-client.ts +113 -3
- package/src/runner/docker-client.ts +34 -0
- package/src/runner/github-client.ts +118 -0
- package/src/server/app.ts +94 -7
- package/src/types.ts +11 -0
|
@@ -0,0 +1,1380 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import readline from 'node:readline/promises';
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
|
|
10
|
+
import { loadConfig } from '../config.ts';
|
|
11
|
+
import { MIN_HC_VERSION, MIN_HERDR_APP_VERSION, compareVersions } from '../runner/hc-client.ts';
|
|
12
|
+
import { install, removeApp, removeLauncher, type InstallOptions, type InstallResult } from '../../scripts/app-install.ts';
|
|
13
|
+
import {
|
|
14
|
+
normalizePlatform,
|
|
15
|
+
readBondVersionFromEnv,
|
|
16
|
+
runDoctor,
|
|
17
|
+
type CommandOutput,
|
|
18
|
+
type DoctorDeps,
|
|
19
|
+
type DoctorPlatform,
|
|
20
|
+
} from './doctor.ts';
|
|
21
|
+
import {
|
|
22
|
+
getServiceStatus,
|
|
23
|
+
installService,
|
|
24
|
+
restartService,
|
|
25
|
+
uninstallService,
|
|
26
|
+
type ServiceActionResult,
|
|
27
|
+
type ServiceDeps,
|
|
28
|
+
type ServiceOptions,
|
|
29
|
+
} from './service.ts';
|
|
30
|
+
import {
|
|
31
|
+
defaultDataDir,
|
|
32
|
+
defaultHerdrAppHome,
|
|
33
|
+
defaultHerdrAppInstallDir,
|
|
34
|
+
defaultLauncherDataHome,
|
|
35
|
+
defaultLocalBinDir,
|
|
36
|
+
} from './paths.ts';
|
|
37
|
+
import { restrictPath } from './permissions.ts';
|
|
38
|
+
|
|
39
|
+
const execFileAsync = promisify(execFile);
|
|
40
|
+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
41
|
+
|
|
42
|
+
export const HERDR_APP_MANIFEST_URL = 'https://releases.herdr.dev/app/latest.json';
|
|
43
|
+
export const HERDR_CORE_MANIFEST_URL = 'https://releases.herdr.dev/core/latest.json';
|
|
44
|
+
export const NPM_PUBLIC_REGISTRY_ARG = '--@jmanuelcorral:registry=https://registry.npmjs.org/';
|
|
45
|
+
|
|
46
|
+
export interface ReleaseTarget {
|
|
47
|
+
url: string;
|
|
48
|
+
sha256: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ReleaseManifest {
|
|
52
|
+
version: string;
|
|
53
|
+
targets: Record<string, ReleaseTarget>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface FetchResponseLike {
|
|
57
|
+
ok: boolean;
|
|
58
|
+
status: number;
|
|
59
|
+
statusText?: string;
|
|
60
|
+
json: () => Promise<unknown>;
|
|
61
|
+
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type FetchLike = (url: string) => Promise<FetchResponseLike>;
|
|
65
|
+
|
|
66
|
+
export interface SetupRecord {
|
|
67
|
+
version: string;
|
|
68
|
+
at: string;
|
|
69
|
+
herdr_app: string | null;
|
|
70
|
+
hc: string | null;
|
|
71
|
+
bond: string | null;
|
|
72
|
+
platform: DoctorPlatform;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface SetupOptions {
|
|
76
|
+
yes?: boolean;
|
|
77
|
+
noLauncher?: boolean;
|
|
78
|
+
noService?: boolean;
|
|
79
|
+
dryRun?: boolean;
|
|
80
|
+
port?: number;
|
|
81
|
+
host?: string;
|
|
82
|
+
dataDir?: string;
|
|
83
|
+
home?: string;
|
|
84
|
+
runnerImage?: string;
|
|
85
|
+
deps?: SetupDeps;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface SetupDeps {
|
|
89
|
+
fetch?: FetchLike;
|
|
90
|
+
runCommand?: (cmd: string, args: string[], timeoutMs?: number) => Promise<CommandOutput>;
|
|
91
|
+
homeDir?: string;
|
|
92
|
+
dataDir?: string;
|
|
93
|
+
herdrAppHome?: string;
|
|
94
|
+
xdgDataHome?: string;
|
|
95
|
+
localBinDir?: string;
|
|
96
|
+
herdrAppInstallDir?: string;
|
|
97
|
+
platform?: NodeJS.Platform;
|
|
98
|
+
arch?: string;
|
|
99
|
+
nodeVersion?: string;
|
|
100
|
+
pathEnv?: string;
|
|
101
|
+
isTTY?: boolean;
|
|
102
|
+
prompt?: (question: string) => Promise<string>;
|
|
103
|
+
log?: (line: string) => void;
|
|
104
|
+
errorLog?: (line: string) => void;
|
|
105
|
+
openHerdrApp?: (opts: { home?: string | null; herdrAppBin?: string | null }) => Promise<void>;
|
|
106
|
+
installApp?: (opts: InstallOptions) => InstallResult;
|
|
107
|
+
removeApp?: (home: string) => { removed: string[]; kept: string[] };
|
|
108
|
+
removeLauncher?: (dataDir: string, opts?: { platform?: NodeJS.Platform }) => { removed: string[]; kept: string[] };
|
|
109
|
+
doctorDeps?: Partial<DoctorDeps>;
|
|
110
|
+
serviceDeps?: ServiceDeps;
|
|
111
|
+
installService?: (opts: ServiceOptions) => Promise<ServiceActionResult>;
|
|
112
|
+
restartService?: (opts: ServiceOptions) => Promise<ServiceActionResult>;
|
|
113
|
+
uninstallService?: (opts: ServiceOptions) => Promise<ServiceActionResult>;
|
|
114
|
+
checkHealth?: (host: string, port: number, timeoutMs?: number) => Promise<{ ok: boolean; statusCode?: number; data?: unknown }>;
|
|
115
|
+
serviceWaitTimeoutMs?: number;
|
|
116
|
+
now?: () => Date;
|
|
117
|
+
bondAppVersion?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface SetupStepResult {
|
|
121
|
+
id: string;
|
|
122
|
+
ok: boolean;
|
|
123
|
+
skipped?: boolean;
|
|
124
|
+
detail: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface SetupResult {
|
|
128
|
+
ok: boolean;
|
|
129
|
+
dryRun: boolean;
|
|
130
|
+
steps: SetupStepResult[];
|
|
131
|
+
record: SetupRecord | null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function readPackageVersion(): string {
|
|
135
|
+
try {
|
|
136
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8')) as { version?: unknown };
|
|
137
|
+
return typeof pkg.version === 'string' && pkg.version ? pkg.version : '0.0.0';
|
|
138
|
+
} catch {
|
|
139
|
+
return '0.0.0';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function defaultRunCommand(cmd: string, args: string[], timeoutMs = 120_000): Promise<CommandOutput> {
|
|
144
|
+
try {
|
|
145
|
+
const needsShell = process.platform === 'win32' && (cmd === 'npm' || cmd === 'bond' || cmd === 'bond-app');
|
|
146
|
+
const { stdout, stderr } = await execFileAsync(cmd, args, {
|
|
147
|
+
encoding: 'utf8',
|
|
148
|
+
timeout: timeoutMs,
|
|
149
|
+
shell: needsShell,
|
|
150
|
+
});
|
|
151
|
+
return { ok: true, stdout: stdout || '', stderr: stderr || '' };
|
|
152
|
+
} catch (err: unknown) {
|
|
153
|
+
const e = err as { stdout?: string; stderr?: string; message?: string };
|
|
154
|
+
return {
|
|
155
|
+
ok: false,
|
|
156
|
+
stdout: typeof e.stdout === 'string' ? e.stdout : '',
|
|
157
|
+
stderr: typeof e.stderr === 'string' ? e.stderr : '',
|
|
158
|
+
error: e.message || String(err),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function defaultPrompt(question: string): Promise<string> {
|
|
164
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
165
|
+
try {
|
|
166
|
+
return await rl.question(question);
|
|
167
|
+
} finally {
|
|
168
|
+
rl.close();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function extractSemver(text: string): string | null {
|
|
173
|
+
const m = /(\d+\.\d+\.\d+)/.exec(text);
|
|
174
|
+
return m ? m[1] : null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function resolveTargetKey(platform: NodeJS.Platform, arch: string): string | null {
|
|
178
|
+
if (platform === 'linux' && arch === 'x64') return 'linux-x86_64';
|
|
179
|
+
if (platform === 'linux' && arch === 'arm64') return 'linux-aarch64';
|
|
180
|
+
if (platform === 'darwin' && arch === 'arm64') return 'macos-aarch64';
|
|
181
|
+
if (platform === 'darwin' && arch === 'x64') return 'macos-x86_64';
|
|
182
|
+
if (platform === 'win32' && arch === 'x64') return 'windows-x86_64';
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function isYesAnswer(answer: string, defaultYes = false): boolean {
|
|
187
|
+
const norm = answer.trim().toLowerCase();
|
|
188
|
+
if (!norm) return defaultYes;
|
|
189
|
+
return norm === 'y' || norm === 'yes' || norm === 's' || norm === 'si' || norm === 'sí';
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function isDirInPath(dir: string, pathEnv: string, platform: NodeJS.Platform = process.platform): boolean {
|
|
193
|
+
const resolved = path.resolve(dir);
|
|
194
|
+
const norm = platform === 'win32' ? resolved.toLowerCase() : resolved;
|
|
195
|
+
return pathEnv
|
|
196
|
+
.split(path.delimiter)
|
|
197
|
+
.filter(Boolean)
|
|
198
|
+
.some((entry) => {
|
|
199
|
+
const r = path.resolve(entry);
|
|
200
|
+
return (platform === 'win32' ? r.toLowerCase() : r) === norm;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function detectBinaryVersion(
|
|
205
|
+
name: string,
|
|
206
|
+
localBinDir: string,
|
|
207
|
+
platform: NodeJS.Platform,
|
|
208
|
+
runCommand: (cmd: string, args: string[], timeoutMs?: number) => Promise<CommandOutput>
|
|
209
|
+
): Promise<{ version: string | null; binPath: string | null }> {
|
|
210
|
+
const direct = await runCommand(name, ['--version'], 10_000);
|
|
211
|
+
if (direct.ok) {
|
|
212
|
+
return { version: extractSemver(direct.stdout), binPath: name };
|
|
213
|
+
}
|
|
214
|
+
const exe = platform === 'win32' ? `${name}.exe` : name;
|
|
215
|
+
const candidate = path.join(localBinDir, exe);
|
|
216
|
+
if (fs.existsSync(candidate)) {
|
|
217
|
+
const localRes = await runCommand(candidate, ['--version'], 10_000);
|
|
218
|
+
if (localRes.ok) {
|
|
219
|
+
return { version: extractSemver(localRes.stdout), binPath: candidate };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return { version: null, binPath: null };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function fetchManifest(fetchFn: FetchLike, url: string): Promise<ReleaseManifest> {
|
|
226
|
+
const res = await fetchFn(url);
|
|
227
|
+
if (!res.ok) {
|
|
228
|
+
throw new Error(`HTTP ${res.status}${res.statusText ? ` ${res.statusText}` : ''} fetching ${url}`);
|
|
229
|
+
}
|
|
230
|
+
const data = (await res.json()) as Partial<ReleaseManifest>;
|
|
231
|
+
if (!data || typeof data.version !== 'string' || !data.targets || typeof data.targets !== 'object') {
|
|
232
|
+
throw new Error(`Invalid release manifest format from ${url}`);
|
|
233
|
+
}
|
|
234
|
+
return data as ReleaseManifest;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function downloadAndVerify(
|
|
238
|
+
fetchFn: FetchLike,
|
|
239
|
+
url: string,
|
|
240
|
+
expectedSha256: string
|
|
241
|
+
): Promise<{ ok: true; buffer: Buffer } | { ok: false; actualSha256: string; error: string }> {
|
|
242
|
+
const res = await fetchFn(url);
|
|
243
|
+
if (!res.ok) {
|
|
244
|
+
return {
|
|
245
|
+
ok: false,
|
|
246
|
+
actualSha256: '',
|
|
247
|
+
error: `HTTP ${res.status}${res.statusText ? ` ${res.statusText}` : ''} downloading ${url}`,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
251
|
+
const actual = crypto.createHash('sha256').update(buffer).digest('hex').toLowerCase();
|
|
252
|
+
const expected = expectedSha256.trim().toLowerCase();
|
|
253
|
+
if (actual !== expected) {
|
|
254
|
+
return {
|
|
255
|
+
ok: false,
|
|
256
|
+
actualSha256: actual,
|
|
257
|
+
error: `sha256 mismatch (expected ${expected}, got ${actual})`,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return { ok: true, buffer };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function extractArchive(
|
|
264
|
+
archivePath: string,
|
|
265
|
+
destDir: string,
|
|
266
|
+
platform: NodeJS.Platform,
|
|
267
|
+
runCommand: (cmd: string, args: string[], timeoutMs?: number) => Promise<CommandOutput>
|
|
268
|
+
): Promise<void> {
|
|
269
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
270
|
+
if (archivePath.endsWith('.zip')) {
|
|
271
|
+
if (platform === 'win32') {
|
|
272
|
+
const esc = (p: string) => p.replace(/'/g, "''");
|
|
273
|
+
const res = await runCommand(
|
|
274
|
+
'powershell',
|
|
275
|
+
['-NoProfile', '-Command', `Expand-Archive -LiteralPath '${esc(archivePath)}' -DestinationPath '${esc(destDir)}' -Force`],
|
|
276
|
+
60_000
|
|
277
|
+
);
|
|
278
|
+
if (!res.ok) throw new Error(res.stderr || res.error || 'Expand-Archive failed');
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const res = await runCommand('unzip', ['-q', archivePath, '-d', destDir], 60_000);
|
|
282
|
+
if (!res.ok) throw new Error(res.stderr || res.error || 'unzip failed');
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const res = await runCommand('tar', ['-xzf', archivePath, '-C', destDir], 60_000);
|
|
286
|
+
if (!res.ok) throw new Error(res.stderr || res.error || 'tar extraction failed');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function copyDirRecursive(src: string, dest: string): void {
|
|
290
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
291
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
292
|
+
const from = path.join(src, entry.name);
|
|
293
|
+
const to = path.join(dest, entry.name);
|
|
294
|
+
if (entry.isDirectory()) {
|
|
295
|
+
copyDirRecursive(from, to);
|
|
296
|
+
} else if (entry.isSymbolicLink()) {
|
|
297
|
+
const target = fs.readlinkSync(from);
|
|
298
|
+
fs.rmSync(to, { force: true });
|
|
299
|
+
fs.symlinkSync(target, to);
|
|
300
|
+
} else if (entry.isFile()) {
|
|
301
|
+
fs.copyFileSync(from, to);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function findFileRecursive(rootDir: string, fileName: string): string | null {
|
|
307
|
+
for (const entry of fs.readdirSync(rootDir, { withFileTypes: true })) {
|
|
308
|
+
const full = path.join(rootDir, entry.name);
|
|
309
|
+
if (entry.isFile() && entry.name === fileName) return full;
|
|
310
|
+
if (entry.isDirectory()) {
|
|
311
|
+
const found = findFileRecursive(full, fileName);
|
|
312
|
+
if (found) return found;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function replaceSymlinkOrCopy(target: string, linkPath: string, isWindows: boolean): void {
|
|
319
|
+
fs.mkdirSync(path.dirname(linkPath), { recursive: true });
|
|
320
|
+
fs.rmSync(linkPath, { force: true });
|
|
321
|
+
if (isWindows) {
|
|
322
|
+
fs.copyFileSync(target, linkPath);
|
|
323
|
+
} else {
|
|
324
|
+
fs.symlinkSync(target, linkPath);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async function installHerdrAppFromManifest(params: {
|
|
329
|
+
fetchFn: FetchLike;
|
|
330
|
+
runCommand: (cmd: string, args: string[], timeoutMs?: number) => Promise<CommandOutput>;
|
|
331
|
+
platform: NodeJS.Platform;
|
|
332
|
+
arch: string;
|
|
333
|
+
herdrAppInstallDir: string;
|
|
334
|
+
localBinDir: string;
|
|
335
|
+
installedVersion: string | null;
|
|
336
|
+
dryRun: boolean;
|
|
337
|
+
}): Promise<{ ok: boolean; version: string | null; installed: boolean; detail: string; binPath?: string }> {
|
|
338
|
+
const { fetchFn, runCommand, platform, arch, herdrAppInstallDir, localBinDir, installedVersion, dryRun } = params;
|
|
339
|
+
const targetKey = resolveTargetKey(platform, arch);
|
|
340
|
+
if (!targetKey) {
|
|
341
|
+
return { ok: false, version: installedVersion, installed: false, detail: `unsupported platform ${platform}/${arch}` };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
let manifest: ReleaseManifest;
|
|
345
|
+
try {
|
|
346
|
+
manifest = await fetchManifest(fetchFn, HERDR_APP_MANIFEST_URL);
|
|
347
|
+
} catch (err: unknown) {
|
|
348
|
+
return {
|
|
349
|
+
ok: false,
|
|
350
|
+
version: installedVersion,
|
|
351
|
+
installed: false,
|
|
352
|
+
detail: err instanceof Error ? err.message : String(err),
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const target = manifest.targets[targetKey];
|
|
357
|
+
if (!target || typeof target.url !== 'string' || typeof target.sha256 !== 'string') {
|
|
358
|
+
const hint =
|
|
359
|
+
platform === 'darwin' && arch === 'x64'
|
|
360
|
+
? ' (macOS Intel is not published yet; Apple Silicon only)'
|
|
361
|
+
: '';
|
|
362
|
+
return {
|
|
363
|
+
ok: false,
|
|
364
|
+
version: installedVersion,
|
|
365
|
+
installed: false,
|
|
366
|
+
detail: `no herdr-app release target for '${targetKey}'${hint}`,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (installedVersion && compareVersions(installedVersion, manifest.version) >= 0) {
|
|
371
|
+
return {
|
|
372
|
+
ok: true,
|
|
373
|
+
version: installedVersion,
|
|
374
|
+
installed: false,
|
|
375
|
+
detail: `${installedVersion} already >= ${manifest.version}`,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (dryRun) {
|
|
380
|
+
return {
|
|
381
|
+
ok: true,
|
|
382
|
+
version: manifest.version,
|
|
383
|
+
installed: false,
|
|
384
|
+
detail: `would download herdr-app ${manifest.version} (${targetKey}) and verify sha256`,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const dl = await downloadAndVerify(fetchFn, target.url, target.sha256);
|
|
389
|
+
if (!dl.ok) {
|
|
390
|
+
return {
|
|
391
|
+
ok: false,
|
|
392
|
+
version: installedVersion,
|
|
393
|
+
installed: false,
|
|
394
|
+
detail: `aborted without modifying installed files: ${dl.error}`,
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'bond-app-herdr-app-'));
|
|
399
|
+
try {
|
|
400
|
+
const ext = target.url.endsWith('.zip') ? '.zip' : '.tar.gz';
|
|
401
|
+
const archivePath = path.join(tmpRoot, `herdr-app${ext}`);
|
|
402
|
+
const extractDir = path.join(tmpRoot, 'extract');
|
|
403
|
+
fs.writeFileSync(archivePath, dl.buffer);
|
|
404
|
+
await extractArchive(archivePath, extractDir, platform, runCommand);
|
|
405
|
+
|
|
406
|
+
const exeName = platform === 'win32' ? 'herdr-app.exe' : 'herdr-app';
|
|
407
|
+
const nestedDir = path.join(extractDir, 'herdr-app');
|
|
408
|
+
const nestedBin = path.join(nestedDir, exeName);
|
|
409
|
+
const flatBin = path.join(extractDir, exeName);
|
|
410
|
+
const srcDir = fs.existsSync(nestedBin) ? nestedDir : fs.existsSync(flatBin) ? extractDir : null;
|
|
411
|
+
if (!srcDir) {
|
|
412
|
+
return {
|
|
413
|
+
ok: false,
|
|
414
|
+
version: installedVersion,
|
|
415
|
+
installed: false,
|
|
416
|
+
detail: `archive did not contain '${exeName}'`,
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const installDir = herdrAppInstallDir;
|
|
421
|
+
const stageDir = path.join(path.dirname(installDir), `.herdr-app.tmp-${process.pid}`);
|
|
422
|
+
fs.rmSync(stageDir, { recursive: true, force: true });
|
|
423
|
+
copyDirRecursive(srcDir, stageDir);
|
|
424
|
+
const stagedBin = path.join(stageDir, exeName);
|
|
425
|
+
if (platform !== 'win32') {
|
|
426
|
+
fs.chmodSync(stagedBin, 0o755);
|
|
427
|
+
}
|
|
428
|
+
fs.rmSync(installDir, { recursive: true, force: true });
|
|
429
|
+
fs.mkdirSync(path.dirname(installDir), { recursive: true });
|
|
430
|
+
fs.renameSync(stageDir, installDir);
|
|
431
|
+
|
|
432
|
+
const installedBin = path.join(installDir, exeName);
|
|
433
|
+
const linkPath = path.join(localBinDir, exeName);
|
|
434
|
+
replaceSymlinkOrCopy(installedBin, linkPath, platform === 'win32');
|
|
435
|
+
|
|
436
|
+
return {
|
|
437
|
+
ok: true,
|
|
438
|
+
version: manifest.version,
|
|
439
|
+
installed: true,
|
|
440
|
+
detail: `installed ${manifest.version} to ${installDir} (linked ${linkPath})`,
|
|
441
|
+
binPath: linkPath,
|
|
442
|
+
};
|
|
443
|
+
} finally {
|
|
444
|
+
fs.rmSync(tmpRoot, { recursive: true, force: true });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
async function installHcFromManifest(params: {
|
|
449
|
+
fetchFn: FetchLike;
|
|
450
|
+
runCommand: (cmd: string, args: string[], timeoutMs?: number) => Promise<CommandOutput>;
|
|
451
|
+
platform: NodeJS.Platform;
|
|
452
|
+
arch: string;
|
|
453
|
+
localBinDir: string;
|
|
454
|
+
installedVersion: string | null;
|
|
455
|
+
dryRun: boolean;
|
|
456
|
+
}): Promise<{ ok: boolean; version: string | null; installed: boolean; detail: string }> {
|
|
457
|
+
const { fetchFn, runCommand, platform, arch, localBinDir, installedVersion, dryRun } = params;
|
|
458
|
+
const targetKey = resolveTargetKey(platform, arch);
|
|
459
|
+
if (!targetKey) {
|
|
460
|
+
return { ok: false, version: installedVersion, installed: false, detail: `unsupported platform ${platform}/${arch}` };
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
let manifest: ReleaseManifest;
|
|
464
|
+
try {
|
|
465
|
+
manifest = await fetchManifest(fetchFn, HERDR_CORE_MANIFEST_URL);
|
|
466
|
+
} catch (err: unknown) {
|
|
467
|
+
return {
|
|
468
|
+
ok: false,
|
|
469
|
+
version: installedVersion,
|
|
470
|
+
installed: false,
|
|
471
|
+
detail: err instanceof Error ? err.message : String(err),
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const target = manifest.targets[targetKey];
|
|
476
|
+
if (!target || typeof target.url !== 'string' || typeof target.sha256 !== 'string') {
|
|
477
|
+
return {
|
|
478
|
+
ok: false,
|
|
479
|
+
version: installedVersion,
|
|
480
|
+
installed: false,
|
|
481
|
+
detail: `no herdr-core release target for '${targetKey}'`,
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
if (installedVersion && compareVersions(installedVersion, manifest.version) >= 0) {
|
|
486
|
+
return {
|
|
487
|
+
ok: true,
|
|
488
|
+
version: installedVersion,
|
|
489
|
+
installed: false,
|
|
490
|
+
detail: `${installedVersion} already >= ${manifest.version}`,
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (dryRun) {
|
|
495
|
+
return {
|
|
496
|
+
ok: true,
|
|
497
|
+
version: manifest.version,
|
|
498
|
+
installed: false,
|
|
499
|
+
detail: `would download herdr-core ${manifest.version} (${targetKey}) and verify sha256`,
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const dl = await downloadAndVerify(fetchFn, target.url, target.sha256);
|
|
504
|
+
if (!dl.ok) {
|
|
505
|
+
return {
|
|
506
|
+
ok: false,
|
|
507
|
+
version: installedVersion,
|
|
508
|
+
installed: false,
|
|
509
|
+
detail: `aborted without modifying installed files: ${dl.error}`,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'bond-app-hc-'));
|
|
514
|
+
try {
|
|
515
|
+
const ext = target.url.endsWith('.zip') ? '.zip' : '.tar.gz';
|
|
516
|
+
const archivePath = path.join(tmpRoot, `herdr-core${ext}`);
|
|
517
|
+
const extractDir = path.join(tmpRoot, 'extract');
|
|
518
|
+
fs.writeFileSync(archivePath, dl.buffer);
|
|
519
|
+
await extractArchive(archivePath, extractDir, platform, runCommand);
|
|
520
|
+
|
|
521
|
+
const coreName = platform === 'win32' ? 'herdr-core.exe' : 'herdr-core';
|
|
522
|
+
const hcName = platform === 'win32' ? 'hc.exe' : 'hc';
|
|
523
|
+
const extractedCore = findFileRecursive(extractDir, coreName);
|
|
524
|
+
if (!extractedCore) {
|
|
525
|
+
return {
|
|
526
|
+
ok: false,
|
|
527
|
+
version: installedVersion,
|
|
528
|
+
installed: false,
|
|
529
|
+
detail: `archive did not contain '${coreName}'`,
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
fs.mkdirSync(localBinDir, { recursive: true });
|
|
534
|
+
const coreDest = path.join(localBinDir, coreName);
|
|
535
|
+
const coreStage = path.join(localBinDir, `.${coreName}.tmp-${process.pid}`);
|
|
536
|
+
fs.copyFileSync(extractedCore, coreStage);
|
|
537
|
+
if (platform !== 'win32') {
|
|
538
|
+
fs.chmodSync(coreStage, 0o755);
|
|
539
|
+
}
|
|
540
|
+
fs.renameSync(coreStage, coreDest);
|
|
541
|
+
|
|
542
|
+
const hcLink = path.join(localBinDir, hcName);
|
|
543
|
+
if (platform === 'win32') {
|
|
544
|
+
replaceSymlinkOrCopy(coreDest, hcLink, true);
|
|
545
|
+
} else {
|
|
546
|
+
replaceSymlinkOrCopy(coreName, hcLink, false);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return {
|
|
550
|
+
ok: true,
|
|
551
|
+
version: manifest.version,
|
|
552
|
+
installed: true,
|
|
553
|
+
detail: `installed ${manifest.version} to ${coreDest} (linked ${hcLink} -> ${coreName})`,
|
|
554
|
+
};
|
|
555
|
+
} finally {
|
|
556
|
+
fs.rmSync(tmpRoot, { recursive: true, force: true });
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export function readSetupRecord(dataDir: string): SetupRecord | null {
|
|
561
|
+
const filePath = path.join(dataDir, 'setup.json');
|
|
562
|
+
try {
|
|
563
|
+
const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8')) as Partial<SetupRecord>;
|
|
564
|
+
if (parsed && typeof parsed.version === 'string' && typeof parsed.at === 'string') {
|
|
565
|
+
return parsed as SetupRecord;
|
|
566
|
+
}
|
|
567
|
+
return null;
|
|
568
|
+
} catch {
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function sameSetupRecord(a: SetupRecord | null, b: SetupRecord): boolean {
|
|
574
|
+
if (!a) return false;
|
|
575
|
+
return (
|
|
576
|
+
a.version === b.version &&
|
|
577
|
+
a.herdr_app === b.herdr_app &&
|
|
578
|
+
a.hc === b.hc &&
|
|
579
|
+
a.bond === b.bond &&
|
|
580
|
+
a.platform === b.platform
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export async function runSetup(options: SetupOptions = {}): Promise<SetupResult> {
|
|
585
|
+
const deps = options.deps ?? {};
|
|
586
|
+
const fetchFn: FetchLike = deps.fetch ?? (globalThis.fetch as unknown as FetchLike);
|
|
587
|
+
const runCommand = deps.runCommand ?? defaultRunCommand;
|
|
588
|
+
const platform = deps.platform ?? process.platform;
|
|
589
|
+
const arch = deps.arch ?? process.arch;
|
|
590
|
+
const normPlatform = normalizePlatform(platform);
|
|
591
|
+
const homeDir = deps.homeDir ?? os.homedir();
|
|
592
|
+
const pathCtx = { platform, homeDir };
|
|
593
|
+
const dataDir = options.dataDir ?? deps.dataDir ?? defaultDataDir(pathCtx);
|
|
594
|
+
const herdrAppHome = options.home ?? deps.herdrAppHome ?? defaultHerdrAppHome(pathCtx);
|
|
595
|
+
const xdgDataHome = deps.xdgDataHome ?? defaultLauncherDataHome(pathCtx);
|
|
596
|
+
const localBinDir = deps.localBinDir ?? defaultLocalBinDir(pathCtx);
|
|
597
|
+
const herdrAppInstallDir = deps.herdrAppInstallDir ?? defaultHerdrAppInstallDir(pathCtx);
|
|
598
|
+
const pathEnv = deps.pathEnv ?? process.env.PATH ?? '';
|
|
599
|
+
const isTTY = deps.isTTY ?? Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
600
|
+
const promptFn = deps.prompt ?? defaultPrompt;
|
|
601
|
+
const log = deps.log ?? console.log;
|
|
602
|
+
const errorLog = deps.errorLog ?? console.error;
|
|
603
|
+
const installAppFn = deps.installApp ?? install;
|
|
604
|
+
const nowFn = deps.now ?? (() => new Date());
|
|
605
|
+
const dryRun = Boolean(options.dryRun);
|
|
606
|
+
const port = options.port ?? parseInt(process.env.BOND_APP_PORT || '3000', 10);
|
|
607
|
+
const host = options.host ?? process.env.BOND_APP_HOST ?? '127.0.0.1';
|
|
608
|
+
const bondAppVersion = deps.bondAppVersion ?? readPackageVersion();
|
|
609
|
+
|
|
610
|
+
const steps: SetupStepResult[] = [];
|
|
611
|
+
const logStep = (icon: '✓' | '✗' | '!', label: string, detail: string, fix?: string) => {
|
|
612
|
+
log(` ${icon} ${label}: ${detail}`);
|
|
613
|
+
if (fix) log(` fix: ${fix}`);
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
log(`bond-app setup (${normPlatform}${dryRun ? ', dry-run' : ''})`);
|
|
617
|
+
|
|
618
|
+
// Step 1: Requirements (Node >= 22.15, Docker usable without sudo) via doctor
|
|
619
|
+
const doctorReport = await runDoctor({
|
|
620
|
+
port,
|
|
621
|
+
host,
|
|
622
|
+
dataDir,
|
|
623
|
+
home: herdrAppHome,
|
|
624
|
+
runnerImage: options.runnerImage,
|
|
625
|
+
deps: {
|
|
626
|
+
...deps.doctorDeps,
|
|
627
|
+
runCommand,
|
|
628
|
+
nodeVersion: deps.nodeVersion,
|
|
629
|
+
platform,
|
|
630
|
+
},
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
const nodeCheck = doctorReport.checks.find((c) => c.id === 'node')!;
|
|
634
|
+
const dockerCheck = doctorReport.checks.find((c) => c.id === 'docker')!;
|
|
635
|
+
|
|
636
|
+
if (nodeCheck.status === 'fail') {
|
|
637
|
+
logStep('✗', nodeCheck.name, nodeCheck.detail, nodeCheck.fix);
|
|
638
|
+
steps.push({ id: 'node', ok: false, detail: nodeCheck.detail });
|
|
639
|
+
return { ok: false, dryRun, steps, record: null };
|
|
640
|
+
}
|
|
641
|
+
logStep('✓', nodeCheck.name, nodeCheck.detail);
|
|
642
|
+
steps.push({ id: 'node', ok: true, detail: nodeCheck.detail });
|
|
643
|
+
|
|
644
|
+
if (dockerCheck.status === 'fail') {
|
|
645
|
+
logStep('✗', dockerCheck.name, dockerCheck.detail, dockerCheck.fix);
|
|
646
|
+
steps.push({ id: 'docker', ok: false, detail: dockerCheck.detail });
|
|
647
|
+
return { ok: false, dryRun, steps, record: null };
|
|
648
|
+
}
|
|
649
|
+
logStep(dockerCheck.status === 'warn' ? '!' : '✓', dockerCheck.name, dockerCheck.detail, dockerCheck.fix);
|
|
650
|
+
steps.push({ id: 'docker', ok: true, detail: dockerCheck.detail });
|
|
651
|
+
|
|
652
|
+
// Step 2: herdr-app & hc (only install/update if missing or below minimum; verify sha256 before extract)
|
|
653
|
+
const detectedHerdrApp = await detectBinaryVersion('herdr-app', localBinDir, platform, runCommand);
|
|
654
|
+
let herdrAppVersion = detectedHerdrApp.version;
|
|
655
|
+
let herdrAppBinPath = detectedHerdrApp.binPath;
|
|
656
|
+
const herdrAppNeedsInstall =
|
|
657
|
+
!herdrAppVersion || compareVersions(herdrAppVersion, MIN_HERDR_APP_VERSION) < 0;
|
|
658
|
+
|
|
659
|
+
if (!herdrAppNeedsInstall) {
|
|
660
|
+
const detail = `${herdrAppVersion} (>= ${MIN_HERDR_APP_VERSION})`;
|
|
661
|
+
logStep('✓', 'herdr-app', detail);
|
|
662
|
+
steps.push({ id: 'herdr-app', ok: true, skipped: true, detail });
|
|
663
|
+
} else {
|
|
664
|
+
const res = await installHerdrAppFromManifest({
|
|
665
|
+
fetchFn,
|
|
666
|
+
runCommand,
|
|
667
|
+
platform,
|
|
668
|
+
arch,
|
|
669
|
+
herdrAppInstallDir,
|
|
670
|
+
localBinDir,
|
|
671
|
+
installedVersion: herdrAppVersion,
|
|
672
|
+
dryRun,
|
|
673
|
+
});
|
|
674
|
+
if (!res.ok) {
|
|
675
|
+
logStep('✗', 'herdr-app', res.detail);
|
|
676
|
+
steps.push({ id: 'herdr-app', ok: false, detail: res.detail });
|
|
677
|
+
return { ok: false, dryRun, steps, record: null };
|
|
678
|
+
}
|
|
679
|
+
herdrAppVersion = res.version;
|
|
680
|
+
if (res.binPath) herdrAppBinPath = res.binPath;
|
|
681
|
+
logStep('✓', 'herdr-app', res.detail);
|
|
682
|
+
steps.push({ id: 'herdr-app', ok: true, detail: res.detail });
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const detectedHc = await detectBinaryVersion('hc', localBinDir, platform, runCommand);
|
|
686
|
+
let hcVersion = detectedHc.version;
|
|
687
|
+
const hcNeedsInstall = !hcVersion || compareVersions(hcVersion, MIN_HC_VERSION) < 0;
|
|
688
|
+
|
|
689
|
+
if (!hcNeedsInstall) {
|
|
690
|
+
const detail = `${hcVersion} (>= ${MIN_HC_VERSION})`;
|
|
691
|
+
logStep('✓', 'hc (herdr-core)', detail);
|
|
692
|
+
steps.push({ id: 'hc', ok: true, skipped: true, detail });
|
|
693
|
+
} else {
|
|
694
|
+
const res = await installHcFromManifest({
|
|
695
|
+
fetchFn,
|
|
696
|
+
runCommand,
|
|
697
|
+
platform,
|
|
698
|
+
arch,
|
|
699
|
+
localBinDir,
|
|
700
|
+
installedVersion: hcVersion,
|
|
701
|
+
dryRun,
|
|
702
|
+
});
|
|
703
|
+
if (!res.ok) {
|
|
704
|
+
logStep('✗', 'hc (herdr-core)', res.detail);
|
|
705
|
+
steps.push({ id: 'hc', ok: false, detail: res.detail });
|
|
706
|
+
return { ok: false, dryRun, steps, record: null };
|
|
707
|
+
}
|
|
708
|
+
hcVersion = res.version;
|
|
709
|
+
logStep('✓', 'hc (herdr-core)', res.detail);
|
|
710
|
+
steps.push({ id: 'hc', ok: true, detail: res.detail });
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
if (!isDirInPath(localBinDir, pathEnv, platform)) {
|
|
714
|
+
logStep(
|
|
715
|
+
'!',
|
|
716
|
+
'PATH',
|
|
717
|
+
`${localBinDir} is not in PATH`,
|
|
718
|
+
normPlatform === 'windows'
|
|
719
|
+
? `Add ${localBinDir} to your user PATH`
|
|
720
|
+
: `Add to your shell profile: export PATH="$HOME/.local/bin:$PATH"`
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// Step 3: bond CLI and bond-app global npm packages (only if missing or below minimum)
|
|
725
|
+
const targetBondVersion = readBondVersionFromEnv();
|
|
726
|
+
const bondRes = await runCommand('bond', ['--version'], 10_000);
|
|
727
|
+
let bondVersion = bondRes.ok ? extractSemver(bondRes.stdout) : null;
|
|
728
|
+
const bondNeedsInstall = !bondRes.ok || (bondVersion !== null && compareVersions(bondVersion, targetBondVersion) < 0);
|
|
729
|
+
|
|
730
|
+
if (!bondNeedsInstall) {
|
|
731
|
+
const detail = `${bondVersion ?? 'installed'} (>= ${targetBondVersion})`;
|
|
732
|
+
logStep('✓', 'bond CLI', detail);
|
|
733
|
+
steps.push({ id: 'bond', ok: true, skipped: true, detail });
|
|
734
|
+
} else if (dryRun) {
|
|
735
|
+
const detail = `would run: npm install -g @jmanuelcorral/bond@${targetBondVersion} ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
736
|
+
logStep('✓', 'bond CLI', detail);
|
|
737
|
+
steps.push({ id: 'bond', ok: true, skipped: true, detail });
|
|
738
|
+
bondVersion = targetBondVersion;
|
|
739
|
+
} else {
|
|
740
|
+
const npmRes = await runCommand(
|
|
741
|
+
'npm',
|
|
742
|
+
['install', '-g', `@jmanuelcorral/bond@${targetBondVersion}`, NPM_PUBLIC_REGISTRY_ARG],
|
|
743
|
+
180_000
|
|
744
|
+
);
|
|
745
|
+
if (!npmRes.ok) {
|
|
746
|
+
const fix = `npm install -g @jmanuelcorral/bond@${targetBondVersion} ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
747
|
+
logStep('✗', 'bond CLI', npmRes.stderr.trim() || npmRes.error || 'npm install failed', fix);
|
|
748
|
+
steps.push({ id: 'bond', ok: false, detail: 'npm install failed' });
|
|
749
|
+
return { ok: false, dryRun, steps, record: null };
|
|
750
|
+
}
|
|
751
|
+
const postBond = await runCommand('bond', ['--version'], 10_000);
|
|
752
|
+
bondVersion = (postBond.ok ? extractSemver(postBond.stdout) : null) ?? targetBondVersion;
|
|
753
|
+
const detail = `installed ${bondVersion}`;
|
|
754
|
+
logStep('✓', 'bond CLI', detail);
|
|
755
|
+
steps.push({ id: 'bond', ok: true, detail });
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const bondAppRes = await runCommand('bond-app', ['--version'], 10_000);
|
|
759
|
+
const installedBondAppVer = bondAppRes.ok ? extractSemver(bondAppRes.stdout) : null;
|
|
760
|
+
const bondAppNeedsInstall =
|
|
761
|
+
!bondAppRes.ok || (installedBondAppVer !== null && compareVersions(installedBondAppVer, bondAppVersion) < 0);
|
|
762
|
+
|
|
763
|
+
if (!bondAppNeedsInstall) {
|
|
764
|
+
const detail = `${installedBondAppVer ?? bondAppVersion} (>= ${bondAppVersion})`;
|
|
765
|
+
logStep('✓', 'bond-app CLI', detail);
|
|
766
|
+
steps.push({ id: 'bond-app', ok: true, skipped: true, detail });
|
|
767
|
+
} else if (dryRun) {
|
|
768
|
+
const detail = `would run: npm install -g @jmanuelcorral/bond-app@${bondAppVersion} ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
769
|
+
logStep('✓', 'bond-app CLI', detail);
|
|
770
|
+
steps.push({ id: 'bond-app', ok: true, skipped: true, detail });
|
|
771
|
+
} else {
|
|
772
|
+
const npmRes = await runCommand(
|
|
773
|
+
'npm',
|
|
774
|
+
['install', '-g', `@jmanuelcorral/bond-app@${bondAppVersion}`, NPM_PUBLIC_REGISTRY_ARG],
|
|
775
|
+
180_000
|
|
776
|
+
);
|
|
777
|
+
if (!npmRes.ok) {
|
|
778
|
+
const fix = `npm install -g @jmanuelcorral/bond-app@${bondAppVersion} ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
779
|
+
logStep('✗', 'bond-app CLI', npmRes.stderr.trim() || npmRes.error || 'npm install failed', fix);
|
|
780
|
+
steps.push({ id: 'bond-app', ok: false, detail: 'npm install failed' });
|
|
781
|
+
return { ok: false, dryRun, steps, record: null };
|
|
782
|
+
}
|
|
783
|
+
const detail = `installed ${bondAppVersion}`;
|
|
784
|
+
logStep('✓', 'bond-app CLI', detail);
|
|
785
|
+
steps.push({ id: 'bond-app', ok: true, detail });
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// Step 4: Layout, mods, themes + optional desktop launcher
|
|
789
|
+
let wantLauncher = false;
|
|
790
|
+
if (options.noLauncher) {
|
|
791
|
+
wantLauncher = false;
|
|
792
|
+
} else if (options.yes) {
|
|
793
|
+
wantLauncher = true;
|
|
794
|
+
} else if (isTTY && !dryRun) {
|
|
795
|
+
const ans = await promptFn(' Install desktop launcher «bond»? [y/N] ');
|
|
796
|
+
wantLauncher = isYesAnswer(ans, false);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
if (dryRun) {
|
|
800
|
+
const detail = `would install layout, mods and themes into ${herdrAppHome}${wantLauncher ? ' (with launcher)' : ''}`;
|
|
801
|
+
logStep('✓', 'Layout, mods & themes', detail);
|
|
802
|
+
steps.push({ id: 'app-integration', ok: true, skipped: true, detail });
|
|
803
|
+
} else {
|
|
804
|
+
try {
|
|
805
|
+
const tokenFile = path.join(path.resolve(dataDir), 'token');
|
|
806
|
+
const res = installAppFn({
|
|
807
|
+
home: herdrAppHome,
|
|
808
|
+
api: `http://${host}:${port}/api/v1`,
|
|
809
|
+
tokenFile,
|
|
810
|
+
check: false,
|
|
811
|
+
skipVersionCheck: true,
|
|
812
|
+
force: false,
|
|
813
|
+
launcher: wantLauncher,
|
|
814
|
+
dataHome: xdgDataHome,
|
|
815
|
+
customHome: Boolean(options.home || deps.herdrAppHome || process.env.HERDR_APP_HOME),
|
|
816
|
+
platform,
|
|
817
|
+
});
|
|
818
|
+
const detail = `installed in ${res.home} (${res.mods.length} mods, ${res.themes.length} themes${res.launcher ? ', launcher' : ''})`;
|
|
819
|
+
logStep('✓', 'Layout, mods & themes', detail);
|
|
820
|
+
steps.push({ id: 'app-integration', ok: true, detail });
|
|
821
|
+
} catch (err: unknown) {
|
|
822
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
823
|
+
logStep('✗', 'Layout, mods & themes', msg);
|
|
824
|
+
steps.push({ id: 'app-integration', ok: false, detail: msg });
|
|
825
|
+
return { ok: false, dryRun, steps, record: null };
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
// Step 5: Data dir (~/.bond-app, 0700 / user-only ACL) and API token (0600 / user-only ACL)
|
|
830
|
+
const permNote = normPlatform === 'windows' ? 'user-only ACL' : '0700/0600';
|
|
831
|
+
if (dryRun) {
|
|
832
|
+
const detail = `would ensure ${dataDir} and token (${permNote})`;
|
|
833
|
+
logStep('✓', 'Data dir & token', detail);
|
|
834
|
+
steps.push({ id: 'data-dir', ok: true, skipped: true, detail });
|
|
835
|
+
} else {
|
|
836
|
+
loadConfig({ dataDir, host, port });
|
|
837
|
+
const tokenPath = path.join(dataDir, 'token');
|
|
838
|
+
if (normPlatform === 'windows') {
|
|
839
|
+
await restrictPath(dataDir, 0o700, { platform, runCommand });
|
|
840
|
+
if (fs.existsSync(tokenPath)) {
|
|
841
|
+
await restrictPath(tokenPath, 0o600, { platform, runCommand });
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
const detail = `${dataDir} and token (${permNote}) ready`;
|
|
845
|
+
logStep('✓', 'Data dir & token', detail);
|
|
846
|
+
steps.push({ id: 'data-dir', ok: true, detail });
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
// Step 6: Runner image (check only, never build — D5)
|
|
850
|
+
const imageCheck = doctorReport.checks.find((c) => c.id === 'runner-image');
|
|
851
|
+
if (imageCheck) {
|
|
852
|
+
const icon = imageCheck.status === 'ok' ? '✓' : '!';
|
|
853
|
+
const detail =
|
|
854
|
+
imageCheck.status === 'ok'
|
|
855
|
+
? imageCheck.detail
|
|
856
|
+
: `${imageCheck.detail} — build it in the herdr-app wizard`;
|
|
857
|
+
logStep(icon, 'Runner image', detail);
|
|
858
|
+
steps.push({ id: 'runner-image', ok: true, detail });
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// Step 6b: Orchestrator user service (default yes with --yes, skipped with --no-service)
|
|
862
|
+
let wantService = false;
|
|
863
|
+
if (options.noService) {
|
|
864
|
+
wantService = false;
|
|
865
|
+
} else if (options.yes) {
|
|
866
|
+
wantService = true;
|
|
867
|
+
} else if (isTTY && !dryRun) {
|
|
868
|
+
const ans = await promptFn(' Install orchestrator as a user service (auto-start on login)? [Y/n] ');
|
|
869
|
+
wantService = isYesAnswer(ans, true);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
if (dryRun) {
|
|
873
|
+
const detail = options.noService
|
|
874
|
+
? 'skipped (--no-service)'
|
|
875
|
+
: `would install orchestrator user service on port ${port}`;
|
|
876
|
+
logStep('✓', 'User service', detail);
|
|
877
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail });
|
|
878
|
+
} else if (!wantService) {
|
|
879
|
+
const detail = options.noService
|
|
880
|
+
? 'skipped (--no-service; run bond-app start in foreground)'
|
|
881
|
+
: 'skipped (run bond-app service install or bond-app start)';
|
|
882
|
+
logStep('✓', 'User service', detail);
|
|
883
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail });
|
|
884
|
+
} else {
|
|
885
|
+
const installSvcFn = deps.installService ?? installService;
|
|
886
|
+
const svcRes = await installSvcFn({
|
|
887
|
+
port,
|
|
888
|
+
host,
|
|
889
|
+
dataDir,
|
|
890
|
+
runnerImage: options.runnerImage,
|
|
891
|
+
deps: {
|
|
892
|
+
runCommand,
|
|
893
|
+
platform,
|
|
894
|
+
homeDir,
|
|
895
|
+
...deps.serviceDeps,
|
|
896
|
+
},
|
|
897
|
+
});
|
|
898
|
+
if (!svcRes.ok) {
|
|
899
|
+
logStep('!', 'User service', svcRes.detail);
|
|
900
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail: svcRes.detail });
|
|
901
|
+
} else {
|
|
902
|
+
logStep('✓', 'User service', svcRes.detail);
|
|
903
|
+
steps.push({ id: 'service', ok: true, detail: svcRes.detail });
|
|
904
|
+
|
|
905
|
+
const checkHealthFn =
|
|
906
|
+
deps.checkHealth ?? deps.doctorDeps?.checkHealth ?? (await import('../cli.ts')).checkHttpHealth;
|
|
907
|
+
const deadline = Date.now() + (deps.serviceWaitTimeoutMs ?? 10_000);
|
|
908
|
+
let apiReady = false;
|
|
909
|
+
while (Date.now() <= deadline) {
|
|
910
|
+
try {
|
|
911
|
+
const h = await checkHealthFn(host, port, 1000);
|
|
912
|
+
if (h.ok) {
|
|
913
|
+
apiReady = true;
|
|
914
|
+
break;
|
|
915
|
+
}
|
|
916
|
+
} catch {
|
|
917
|
+
// retry until timeout
|
|
918
|
+
}
|
|
919
|
+
if (Date.now() >= deadline) break;
|
|
920
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
921
|
+
}
|
|
922
|
+
if (apiReady) {
|
|
923
|
+
logStep('✓', 'Orchestrator API', `listening at http://${host}:${port}`);
|
|
924
|
+
} else {
|
|
925
|
+
logStep('!', 'Orchestrator API', `did not respond at http://${host}:${port} within timeout`);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// Step 7: Open herdr-app with the bond layout (without touching the window further)
|
|
931
|
+
if (dryRun) {
|
|
932
|
+
const detail = `would open herdr-app with layout 'bond'`;
|
|
933
|
+
logStep('✓', 'Open herdr-app', detail);
|
|
934
|
+
steps.push({ id: 'open-herdr-app', ok: true, skipped: true, detail });
|
|
935
|
+
} else if (deps.openHerdrApp) {
|
|
936
|
+
try {
|
|
937
|
+
await deps.openHerdrApp({ home: herdrAppHome, herdrAppBin: herdrAppBinPath });
|
|
938
|
+
const detail = 'opened with layout bond';
|
|
939
|
+
logStep('✓', 'Open herdr-app', detail);
|
|
940
|
+
steps.push({ id: 'open-herdr-app', ok: true, detail });
|
|
941
|
+
} catch (err: unknown) {
|
|
942
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
943
|
+
logStep('!', 'Open herdr-app', `${msg} (run 'bond-app open' later)`);
|
|
944
|
+
steps.push({ id: 'open-herdr-app', ok: true, skipped: true, detail: msg });
|
|
945
|
+
}
|
|
946
|
+
} else {
|
|
947
|
+
const { openHerdrApp } = await import('../cli.ts');
|
|
948
|
+
try {
|
|
949
|
+
await openHerdrApp({ home: herdrAppHome, herdrAppBin: herdrAppBinPath });
|
|
950
|
+
const detail = 'opened with layout bond';
|
|
951
|
+
logStep('✓', 'Open herdr-app', detail);
|
|
952
|
+
steps.push({ id: 'open-herdr-app', ok: true, detail });
|
|
953
|
+
} catch (err: unknown) {
|
|
954
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
955
|
+
logStep('!', 'Open herdr-app', `${msg} (run 'bond-app open' later)`);
|
|
956
|
+
steps.push({ id: 'open-herdr-app', ok: true, skipped: true, detail: msg });
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// Step 8: Write ~/.bond-app/setup.json (idempotent: skip rewrite if unchanged)
|
|
961
|
+
const record: SetupRecord = {
|
|
962
|
+
version: bondAppVersion,
|
|
963
|
+
at: nowFn().toISOString(),
|
|
964
|
+
herdr_app: herdrAppVersion,
|
|
965
|
+
hc: hcVersion,
|
|
966
|
+
bond: bondVersion,
|
|
967
|
+
platform: normPlatform,
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
if (dryRun) {
|
|
971
|
+
const detail = `would write ${path.join(dataDir, 'setup.json')}`;
|
|
972
|
+
logStep('✓', 'setup.json', detail);
|
|
973
|
+
steps.push({ id: 'setup-record', ok: true, skipped: true, detail });
|
|
974
|
+
} else {
|
|
975
|
+
const existing = readSetupRecord(dataDir);
|
|
976
|
+
const setupPath = path.join(dataDir, 'setup.json');
|
|
977
|
+
if (sameSetupRecord(existing, record)) {
|
|
978
|
+
logStep('✓', 'setup.json', `unchanged (${setupPath})`);
|
|
979
|
+
steps.push({ id: 'setup-record', ok: true, skipped: true, detail: 'unchanged' });
|
|
980
|
+
return { ok: true, dryRun, steps, record: existing };
|
|
981
|
+
}
|
|
982
|
+
const dataDirExisted = fs.existsSync(dataDir);
|
|
983
|
+
fs.mkdirSync(dataDir, { recursive: true, mode: 0o700 });
|
|
984
|
+
if (!dataDirExisted) {
|
|
985
|
+
await restrictPath(dataDir, 0o700, { platform, runCommand });
|
|
986
|
+
}
|
|
987
|
+
fs.writeFileSync(setupPath, JSON.stringify(record, null, 2) + '\n', { encoding: 'utf8', mode: 0o600 });
|
|
988
|
+
await restrictPath(setupPath, 0o600, { platform, runCommand });
|
|
989
|
+
logStep('✓', 'setup.json', `written (${setupPath})`);
|
|
990
|
+
steps.push({ id: 'setup-record', ok: true, detail: `written (${setupPath})` });
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
void errorLog;
|
|
994
|
+
return { ok: true, dryRun, steps, record };
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
export function warnFirstRunIfNeeded(opts: { dataDir?: string; deps?: SetupDeps } = {}): void {
|
|
998
|
+
const deps = opts.deps ?? {};
|
|
999
|
+
const homeDir = deps.homeDir ?? os.homedir();
|
|
1000
|
+
const dataDir = opts.dataDir ?? deps.dataDir ?? defaultDataDir({ platform: deps.platform, homeDir });
|
|
1001
|
+
if (readSetupRecord(dataDir)) return;
|
|
1002
|
+
const log = deps.log ?? console.log;
|
|
1003
|
+
log("bond-app: initial setup has not been run yet. Run 'bond-app setup' to configure it.");
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
export async function maybeHandleFirstRun(opts: {
|
|
1007
|
+
fromStart: boolean;
|
|
1008
|
+
dataDir?: string;
|
|
1009
|
+
deps?: SetupDeps;
|
|
1010
|
+
}): Promise<{ ranSetup: boolean; setupOk: boolean }> {
|
|
1011
|
+
const deps = opts.deps ?? {};
|
|
1012
|
+
const homeDir = deps.homeDir ?? os.homedir();
|
|
1013
|
+
const dataDir = opts.dataDir ?? deps.dataDir ?? defaultDataDir({ platform: deps.platform, homeDir });
|
|
1014
|
+
if (readSetupRecord(dataDir)) {
|
|
1015
|
+
return { ranSetup: false, setupOk: true };
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
const isTTY = deps.isTTY ?? Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
1019
|
+
const log = deps.log ?? console.log;
|
|
1020
|
+
const promptFn = deps.prompt ?? defaultPrompt;
|
|
1021
|
+
|
|
1022
|
+
if (!isTTY) {
|
|
1023
|
+
log("bond-app: initial setup has not been run yet. Run 'bond-app setup' to configure it.");
|
|
1024
|
+
return { ranSetup: false, setupOk: true };
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
const msg = opts.fromStart
|
|
1028
|
+
? "bond-app has not been set up yet. Run 'bond-app setup' first? [y/N] "
|
|
1029
|
+
: "bond-app has not been set up yet. Run 'bond-app setup' now? [y/N] ";
|
|
1030
|
+
const ans = await promptFn(msg);
|
|
1031
|
+
if (!isYesAnswer(ans, false)) {
|
|
1032
|
+
return { ranSetup: false, setupOk: true };
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
const res = await runSetup({ dataDir, deps });
|
|
1036
|
+
return { ranSetup: true, setupOk: res.ok };
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
export interface UpdateOptions {
|
|
1040
|
+
dryRun?: boolean;
|
|
1041
|
+
port?: number;
|
|
1042
|
+
host?: string;
|
|
1043
|
+
dataDir?: string;
|
|
1044
|
+
home?: string;
|
|
1045
|
+
runnerImage?: string;
|
|
1046
|
+
deps?: SetupDeps;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
export interface UpdateResult {
|
|
1050
|
+
ok: boolean;
|
|
1051
|
+
dryRun: boolean;
|
|
1052
|
+
steps: SetupStepResult[];
|
|
1053
|
+
imageOutdated: boolean;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
export async function runUpdate(options: UpdateOptions = {}): Promise<UpdateResult> {
|
|
1057
|
+
const deps = options.deps ?? {};
|
|
1058
|
+
const runCommand = deps.runCommand ?? defaultRunCommand;
|
|
1059
|
+
const platform = deps.platform ?? process.platform;
|
|
1060
|
+
const normPlatform = normalizePlatform(platform);
|
|
1061
|
+
const homeDir = deps.homeDir ?? os.homedir();
|
|
1062
|
+
const pathCtx = { platform, homeDir };
|
|
1063
|
+
const dataDir = options.dataDir ?? deps.dataDir ?? defaultDataDir(pathCtx);
|
|
1064
|
+
const herdrAppHome = options.home ?? deps.herdrAppHome ?? defaultHerdrAppHome(pathCtx);
|
|
1065
|
+
const xdgDataHome = deps.xdgDataHome ?? defaultLauncherDataHome(pathCtx);
|
|
1066
|
+
const log = deps.log ?? console.log;
|
|
1067
|
+
const installAppFn = deps.installApp ?? install;
|
|
1068
|
+
const restartSvcFn = deps.restartService ?? restartService;
|
|
1069
|
+
const dryRun = Boolean(options.dryRun);
|
|
1070
|
+
const port = options.port ?? parseInt(process.env.BOND_APP_PORT || '3000', 10);
|
|
1071
|
+
const host = options.host ?? process.env.BOND_APP_HOST ?? '127.0.0.1';
|
|
1072
|
+
|
|
1073
|
+
const steps: SetupStepResult[] = [];
|
|
1074
|
+
const logStep = (icon: '✓' | '✗' | '!', label: string, detail: string, fix?: string) => {
|
|
1075
|
+
log(` ${icon} ${label}: ${detail}`);
|
|
1076
|
+
if (fix) log(` fix: ${fix}`);
|
|
1077
|
+
};
|
|
1078
|
+
|
|
1079
|
+
log(`bond-app update (${normPlatform}${dryRun ? ', dry-run' : ''})`);
|
|
1080
|
+
|
|
1081
|
+
// 1. Update @jmanuelcorral/bond-app via npm (public registry)
|
|
1082
|
+
if (dryRun) {
|
|
1083
|
+
const detail = `would run: npm install -g @jmanuelcorral/bond-app@latest ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
1084
|
+
logStep('✓', 'bond-app', detail);
|
|
1085
|
+
steps.push({ id: 'bond-app', ok: true, skipped: true, detail });
|
|
1086
|
+
} else {
|
|
1087
|
+
const npmRes = await runCommand(
|
|
1088
|
+
'npm',
|
|
1089
|
+
['install', '-g', '@jmanuelcorral/bond-app@latest', NPM_PUBLIC_REGISTRY_ARG],
|
|
1090
|
+
180_000
|
|
1091
|
+
);
|
|
1092
|
+
if (!npmRes.ok) {
|
|
1093
|
+
const fix = `npm install -g @jmanuelcorral/bond-app@latest ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
1094
|
+
logStep('✗', 'bond-app', npmRes.stderr.trim() || npmRes.error || 'npm install failed', fix);
|
|
1095
|
+
steps.push({ id: 'bond-app', ok: false, detail: 'npm install failed' });
|
|
1096
|
+
return { ok: false, dryRun, steps, imageOutdated: false };
|
|
1097
|
+
}
|
|
1098
|
+
const verRes = await runCommand('bond-app', ['--version'], 10_000);
|
|
1099
|
+
const ver = verRes.ok ? extractSemver(verRes.stdout) : null;
|
|
1100
|
+
const detail = ver ? `updated to ${ver}` : 'updated';
|
|
1101
|
+
logStep('✓', 'bond-app', detail);
|
|
1102
|
+
steps.push({ id: 'bond-app', ok: true, detail });
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
// 2. Update @jmanuelcorral/bond via npm (public registry)
|
|
1106
|
+
if (dryRun) {
|
|
1107
|
+
const detail = `would run: npm install -g @jmanuelcorral/bond@latest ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
1108
|
+
logStep('✓', 'bond CLI', detail);
|
|
1109
|
+
steps.push({ id: 'bond', ok: true, skipped: true, detail });
|
|
1110
|
+
} else {
|
|
1111
|
+
const npmRes = await runCommand(
|
|
1112
|
+
'npm',
|
|
1113
|
+
['install', '-g', '@jmanuelcorral/bond@latest', NPM_PUBLIC_REGISTRY_ARG],
|
|
1114
|
+
180_000
|
|
1115
|
+
);
|
|
1116
|
+
if (!npmRes.ok) {
|
|
1117
|
+
const fix = `npm install -g @jmanuelcorral/bond@latest ${NPM_PUBLIC_REGISTRY_ARG}`;
|
|
1118
|
+
logStep('✗', 'bond CLI', npmRes.stderr.trim() || npmRes.error || 'npm install failed', fix);
|
|
1119
|
+
steps.push({ id: 'bond', ok: false, detail: 'npm install failed' });
|
|
1120
|
+
return { ok: false, dryRun, steps, imageOutdated: false };
|
|
1121
|
+
}
|
|
1122
|
+
const verRes = await runCommand('bond', ['--version'], 10_000);
|
|
1123
|
+
const ver = verRes.ok ? extractSemver(verRes.stdout) : null;
|
|
1124
|
+
const detail = ver ? `updated to ${ver}` : 'updated';
|
|
1125
|
+
logStep('✓', 'bond CLI', detail);
|
|
1126
|
+
steps.push({ id: 'bond', ok: true, detail });
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
// 3. Reinstall layout, mods and themes into herdr-app
|
|
1130
|
+
if (dryRun) {
|
|
1131
|
+
const detail = `would reinstall layout, mods and themes into ${herdrAppHome}`;
|
|
1132
|
+
logStep('✓', 'Layout, mods & themes', detail);
|
|
1133
|
+
steps.push({ id: 'app-integration', ok: true, skipped: true, detail });
|
|
1134
|
+
} else {
|
|
1135
|
+
try {
|
|
1136
|
+
const tokenFile = path.join(path.resolve(dataDir), 'token');
|
|
1137
|
+
const res = installAppFn({
|
|
1138
|
+
home: herdrAppHome,
|
|
1139
|
+
api: `http://${host}:${port}/api/v1`,
|
|
1140
|
+
tokenFile,
|
|
1141
|
+
check: false,
|
|
1142
|
+
skipVersionCheck: true,
|
|
1143
|
+
force: false,
|
|
1144
|
+
launcher: false,
|
|
1145
|
+
dataHome: xdgDataHome,
|
|
1146
|
+
customHome: Boolean(options.home || deps.herdrAppHome || process.env.HERDR_APP_HOME),
|
|
1147
|
+
platform,
|
|
1148
|
+
});
|
|
1149
|
+
const detail = `reinstalled in ${res.home} (${res.mods.length} mods, ${res.themes.length} themes)`;
|
|
1150
|
+
logStep('✓', 'Layout, mods & themes', detail);
|
|
1151
|
+
steps.push({ id: 'app-integration', ok: true, detail });
|
|
1152
|
+
} catch (err: unknown) {
|
|
1153
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1154
|
+
logStep('✗', 'Layout, mods & themes', msg);
|
|
1155
|
+
steps.push({ id: 'app-integration', ok: false, detail: msg });
|
|
1156
|
+
return { ok: false, dryRun, steps, imageOutdated: false };
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
// 4. Restart user service if installed
|
|
1161
|
+
const serviceDeps: ServiceDeps = {
|
|
1162
|
+
runCommand,
|
|
1163
|
+
platform,
|
|
1164
|
+
homeDir,
|
|
1165
|
+
...deps.serviceDeps,
|
|
1166
|
+
};
|
|
1167
|
+
const svcStatus = await getServiceStatus({ port, host, dataDir, deps: serviceDeps });
|
|
1168
|
+
if (svcStatus.supported && svcStatus.installed) {
|
|
1169
|
+
if (dryRun) {
|
|
1170
|
+
const detail = `would restart user service (${svcStatus.serviceName})`;
|
|
1171
|
+
logStep('✓', 'User service', detail);
|
|
1172
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail });
|
|
1173
|
+
} else {
|
|
1174
|
+
const svcRes = await restartSvcFn({
|
|
1175
|
+
port,
|
|
1176
|
+
host,
|
|
1177
|
+
dataDir,
|
|
1178
|
+
runnerImage: options.runnerImage,
|
|
1179
|
+
deps: serviceDeps,
|
|
1180
|
+
});
|
|
1181
|
+
if (svcRes.ok) {
|
|
1182
|
+
logStep('✓', 'User service', svcRes.detail);
|
|
1183
|
+
steps.push({ id: 'service', ok: true, detail: svcRes.detail });
|
|
1184
|
+
} else {
|
|
1185
|
+
logStep('!', 'User service', svcRes.detail);
|
|
1186
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail: svcRes.detail });
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
} else {
|
|
1190
|
+
const detail = 'not installed (skipped)';
|
|
1191
|
+
logStep('✓', 'User service', detail);
|
|
1192
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail });
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
// 5. Check runner image recipe without rebuilding (D5)
|
|
1196
|
+
const doctorReport = await runDoctor({
|
|
1197
|
+
port,
|
|
1198
|
+
host,
|
|
1199
|
+
dataDir,
|
|
1200
|
+
home: herdrAppHome,
|
|
1201
|
+
runnerImage: options.runnerImage,
|
|
1202
|
+
deps: {
|
|
1203
|
+
...deps.doctorDeps,
|
|
1204
|
+
runCommand,
|
|
1205
|
+
nodeVersion: deps.nodeVersion,
|
|
1206
|
+
platform,
|
|
1207
|
+
serviceDeps,
|
|
1208
|
+
},
|
|
1209
|
+
});
|
|
1210
|
+
const imageCheck = doctorReport.checks.find((c) => c.id === 'runner-image');
|
|
1211
|
+
let imageOutdated = false;
|
|
1212
|
+
if (imageCheck && imageCheck.status !== 'ok') {
|
|
1213
|
+
imageOutdated = true;
|
|
1214
|
+
logStep(
|
|
1215
|
+
'!',
|
|
1216
|
+
'Runner image',
|
|
1217
|
+
`${imageCheck.detail} — rebuild it in Settings → Image (not rebuilt automatically)`,
|
|
1218
|
+
imageCheck.fix
|
|
1219
|
+
);
|
|
1220
|
+
steps.push({ id: 'runner-image', ok: true, detail: imageCheck.detail });
|
|
1221
|
+
} else if (imageCheck) {
|
|
1222
|
+
logStep('✓', 'Runner image', imageCheck.detail);
|
|
1223
|
+
steps.push({ id: 'runner-image', ok: true, detail: imageCheck.detail });
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
return { ok: true, dryRun, steps, imageOutdated };
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
export interface UninstallOptions {
|
|
1230
|
+
purge?: boolean;
|
|
1231
|
+
yes?: boolean;
|
|
1232
|
+
dryRun?: boolean;
|
|
1233
|
+
port?: number;
|
|
1234
|
+
host?: string;
|
|
1235
|
+
dataDir?: string;
|
|
1236
|
+
home?: string;
|
|
1237
|
+
deps?: SetupDeps;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
export interface UninstallResult {
|
|
1241
|
+
ok: boolean;
|
|
1242
|
+
dryRun: boolean;
|
|
1243
|
+
purged: boolean;
|
|
1244
|
+
steps: SetupStepResult[];
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
export async function runUninstall(options: UninstallOptions = {}): Promise<UninstallResult> {
|
|
1248
|
+
const deps = options.deps ?? {};
|
|
1249
|
+
const runCommand = deps.runCommand ?? defaultRunCommand;
|
|
1250
|
+
const platform = deps.platform ?? process.platform;
|
|
1251
|
+
const normPlatform = normalizePlatform(platform);
|
|
1252
|
+
const homeDir = deps.homeDir ?? os.homedir();
|
|
1253
|
+
const pathCtx = { platform, homeDir };
|
|
1254
|
+
const dataDir = options.dataDir ?? deps.dataDir ?? defaultDataDir(pathCtx);
|
|
1255
|
+
const herdrAppHome = options.home ?? deps.herdrAppHome ?? defaultHerdrAppHome(pathCtx);
|
|
1256
|
+
const xdgDataHome = deps.xdgDataHome ?? defaultLauncherDataHome(pathCtx);
|
|
1257
|
+
const isTTY = deps.isTTY ?? Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
1258
|
+
const promptFn = deps.prompt ?? defaultPrompt;
|
|
1259
|
+
const log = deps.log ?? console.log;
|
|
1260
|
+
const removeAppFn = deps.removeApp ?? removeApp;
|
|
1261
|
+
const removeLauncherFn = deps.removeLauncher ?? removeLauncher;
|
|
1262
|
+
const uninstallSvcFn = deps.uninstallService ?? uninstallService;
|
|
1263
|
+
const dryRun = Boolean(options.dryRun);
|
|
1264
|
+
const port = options.port ?? parseInt(process.env.BOND_APP_PORT || '3000', 10);
|
|
1265
|
+
const host = options.host ?? process.env.BOND_APP_HOST ?? '127.0.0.1';
|
|
1266
|
+
|
|
1267
|
+
const steps: SetupStepResult[] = [];
|
|
1268
|
+
let allOk = true;
|
|
1269
|
+
let purged = false;
|
|
1270
|
+
const logStep = (icon: '✓' | '✗' | '!', label: string, detail: string) => {
|
|
1271
|
+
log(` ${icon} ${label}: ${detail}`);
|
|
1272
|
+
};
|
|
1273
|
+
|
|
1274
|
+
log(`bond-app uninstall (${normPlatform}${dryRun ? ', dry-run' : ''})`);
|
|
1275
|
+
|
|
1276
|
+
// 1. User service
|
|
1277
|
+
const serviceDeps: ServiceDeps = {
|
|
1278
|
+
runCommand,
|
|
1279
|
+
platform,
|
|
1280
|
+
homeDir,
|
|
1281
|
+
...deps.serviceDeps,
|
|
1282
|
+
};
|
|
1283
|
+
const svcStatus = await getServiceStatus({ port, host, dataDir, deps: serviceDeps });
|
|
1284
|
+
if (svcStatus.supported && svcStatus.installed) {
|
|
1285
|
+
if (dryRun) {
|
|
1286
|
+
const detail = `would uninstall user service (${svcStatus.serviceName})`;
|
|
1287
|
+
logStep('✓', 'User service', detail);
|
|
1288
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail });
|
|
1289
|
+
} else {
|
|
1290
|
+
const svcRes = await uninstallSvcFn({ port, host, dataDir, deps: serviceDeps });
|
|
1291
|
+
if (svcRes.ok) {
|
|
1292
|
+
logStep('✓', 'User service', svcRes.detail);
|
|
1293
|
+
steps.push({ id: 'service', ok: true, detail: svcRes.detail });
|
|
1294
|
+
} else {
|
|
1295
|
+
logStep('✗', 'User service', svcRes.detail);
|
|
1296
|
+
steps.push({ id: 'service', ok: false, detail: svcRes.detail });
|
|
1297
|
+
allOk = false;
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
} else {
|
|
1301
|
+
const detail = 'not installed (skipped)';
|
|
1302
|
+
logStep('✓', 'User service', detail);
|
|
1303
|
+
steps.push({ id: 'service', ok: true, skipped: true, detail });
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
// 2. Layout, mods and themes (only those installed by bond-app)
|
|
1307
|
+
if (dryRun) {
|
|
1308
|
+
const detail = `would remove bond-app layout, mods and themes from ${herdrAppHome}`;
|
|
1309
|
+
logStep('✓', 'Layout, mods & themes', detail);
|
|
1310
|
+
steps.push({ id: 'app-integration', ok: true, skipped: true, detail });
|
|
1311
|
+
} else {
|
|
1312
|
+
const { removed, kept } = removeAppFn(herdrAppHome);
|
|
1313
|
+
const detail = removed.length
|
|
1314
|
+
? `removed ${removed.length} item(s)${kept.length ? `; kept ${kept.length} foreign/edited` : ''}`
|
|
1315
|
+
: kept.length
|
|
1316
|
+
? `nothing removed; kept ${kept.length} foreign/edited`
|
|
1317
|
+
: 'not installed (skipped)';
|
|
1318
|
+
logStep('✓', 'Layout, mods & themes', detail);
|
|
1319
|
+
steps.push({ id: 'app-integration', ok: true, detail });
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// 3. Desktop launcher
|
|
1323
|
+
if (dryRun) {
|
|
1324
|
+
const detail = `would remove bond desktop launcher from ${xdgDataHome}`;
|
|
1325
|
+
logStep('✓', 'Desktop launcher', detail);
|
|
1326
|
+
steps.push({ id: 'launcher', ok: true, skipped: true, detail });
|
|
1327
|
+
} else {
|
|
1328
|
+
const { removed, kept } = removeLauncherFn(xdgDataHome, { platform });
|
|
1329
|
+
const detail = removed.length
|
|
1330
|
+
? `removed ${removed.length} file(s)${kept.length ? `; kept ${kept.length} foreign/edited` : ''}`
|
|
1331
|
+
: kept.length
|
|
1332
|
+
? `nothing removed; kept ${kept.length} foreign/edited`
|
|
1333
|
+
: 'not installed (skipped)';
|
|
1334
|
+
logStep('✓', 'Desktop launcher', detail);
|
|
1335
|
+
steps.push({ id: 'launcher', ok: true, detail });
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
// 4. Data directory (~/.bond-app) — only with --purge and confirmation (or --yes)
|
|
1339
|
+
if (!options.purge) {
|
|
1340
|
+
const detail = `preserved ${dataDir} (use --purge to remove database, token and credentials)`;
|
|
1341
|
+
logStep('✓', 'Data dir', detail);
|
|
1342
|
+
steps.push({ id: 'data-dir', ok: true, skipped: true, detail });
|
|
1343
|
+
} else if (dryRun) {
|
|
1344
|
+
const detail = `would remove ${dataDir}`;
|
|
1345
|
+
logStep('✓', 'Data dir', detail);
|
|
1346
|
+
steps.push({ id: 'data-dir', ok: true, skipped: true, detail });
|
|
1347
|
+
} else if (!fs.existsSync(dataDir)) {
|
|
1348
|
+
const detail = `${dataDir} does not exist (skipped)`;
|
|
1349
|
+
logStep('✓', 'Data dir', detail);
|
|
1350
|
+
steps.push({ id: 'data-dir', ok: true, skipped: true, detail });
|
|
1351
|
+
} else {
|
|
1352
|
+
let confirmed = Boolean(options.yes);
|
|
1353
|
+
if (!confirmed) {
|
|
1354
|
+
if (!isTTY) {
|
|
1355
|
+
const detail = `refusing to purge ${dataDir} without --yes in non-interactive mode`;
|
|
1356
|
+
logStep('✗', 'Data dir', detail);
|
|
1357
|
+
steps.push({ id: 'data-dir', ok: false, detail });
|
|
1358
|
+
allOk = false;
|
|
1359
|
+
} else {
|
|
1360
|
+
const ans = await promptFn(` Remove ${dataDir} (database, token and credentials)? [y/N] `);
|
|
1361
|
+
confirmed = isYesAnswer(ans, false);
|
|
1362
|
+
if (!confirmed) {
|
|
1363
|
+
const detail = `preserved ${dataDir} (cancelled)`;
|
|
1364
|
+
logStep('✓', 'Data dir', detail);
|
|
1365
|
+
steps.push({ id: 'data-dir', ok: true, skipped: true, detail });
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
if (confirmed) {
|
|
1370
|
+
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
1371
|
+
purged = true;
|
|
1372
|
+
const detail = `removed ${dataDir}`;
|
|
1373
|
+
logStep('✓', 'Data dir', detail);
|
|
1374
|
+
steps.push({ id: 'data-dir', ok: true, detail });
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
log(' Note: herdr-app, hc and bond were not uninstalled (they may be used by other projects).');
|
|
1379
|
+
return { ok: allOk, dryRun, purged, steps };
|
|
1380
|
+
}
|