@animastor/installer 0.1.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.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +66 -0
  3. package/ai/install-manifests/audio/qwen-tts.json +445 -0
  4. package/ai/install-manifests/image/qwen-image.json +465 -0
  5. package/ai/install-manifests/video/ltx-2.3.json +892 -0
  6. package/package.json +42 -0
  7. package/src/installer/cli.js +1067 -0
  8. package/src/installer/compatibility-resolver.js +1287 -0
  9. package/src/installer/download-planner.js +172 -0
  10. package/src/installer/engine/comfyui.js +508 -0
  11. package/src/installer/engine/downloader.js +514 -0
  12. package/src/installer/engine/engine.js +1180 -0
  13. package/src/installer/engine/io.js +437 -0
  14. package/src/installer/engine/logger.js +75 -0
  15. package/src/installer/engine/nodes.js +146 -0
  16. package/src/installer/engine/prereq.js +399 -0
  17. package/src/installer/engine/probe.js +349 -0
  18. package/src/installer/engine/progress.js +350 -0
  19. package/src/installer/engine/state.js +170 -0
  20. package/src/installer/engine/term.js +298 -0
  21. package/src/installer/engine/worker.js +430 -0
  22. package/src/installer/engine/workflows.js +153 -0
  23. package/src/installer/index.js +40 -0
  24. package/src/installer/install-manifest.js +376 -0
  25. package/src/installer/install-plan.js +741 -0
  26. package/src/installer/management.js +489 -0
  27. package/src/installer/platform/deployment/docker.js +78 -0
  28. package/src/installer/platform/deployment/native.js +36 -0
  29. package/src/installer/platform/index.js +165 -0
  30. package/src/installer/platform/linux.js +341 -0
  31. package/src/installer/platform/windows.js +360 -0
  32. package/src/installer/safety-rules.js +156 -0
  33. package/src/installer/setup-contract.js +1401 -0
  34. package/src/installer/uninstaller.js +572 -0
  35. package/src/installer/verification-report.js +220 -0
  36. package/src/installer/worker-bundle-source.js +64 -0
  37. package/src/installer/workflow-artifacts.js +152 -0
@@ -0,0 +1,572 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Uninstaller — safe, manifest-driven removal of Animastor installer artifacts.
5
+ *
6
+ * The ONLY source of truth for "what belongs to Animastor" is the
7
+ * installation state/manifest written by the installer
8
+ * (<root>/.animastor-installer/install-state.json → `components` registry).
9
+ * The uninstaller never guesses by file names and never removes anything
10
+ * that is not registered there:
11
+ *
12
+ * - a ComfyUI directory that existed before the installer ran is recorded
13
+ * as owned:false and is NEVER removed wholesale (the installer-owned
14
+ * venv inside it is removed precisely);
15
+ * - custom nodes / models / workflows that were already present are not
16
+ * registered as owned and are not touched;
17
+ * - a worker directory created by the installer may be removed entirely;
18
+ * a pre-existing worker directory only has the files the installer
19
+ * actually copied removed;
20
+ * - the worker .env is removed only when the installer created it (merge
21
+ * semantics: a pre-existing .env is left in place).
22
+ *
23
+ * Every deletion goes through assertDeletablePath(): absolute, normalized,
24
+ * deep enough, never a system directory, never $HOME, never an ancestor of
25
+ * the state file or the home directory.
26
+ */
27
+
28
+ const path = require('path');
29
+ const platforms = require('./platform');
30
+
31
+ /**
32
+ * System directory prefixes: nothing inside these may ever be deleted.
33
+ * (Install roots normally live under $HOME or a user-chosen data dir such as
34
+ * /opt/<name> or /data/<name>, which stay allowed.)
35
+ */
36
+ const SYSTEM_PREFIXES = Object.freeze([
37
+ '/bin', '/boot', '/dev', '/etc', '/lib', '/lib32', '/lib64', '/libx32',
38
+ '/proc', '/run', '/sbin', '/sys', '/usr', '/var',
39
+ ]);
40
+
41
+ /** Directories that may only be deleted as whole paths, never exactly. */
42
+ const PROTECTED_DIRS = Object.freeze([
43
+ '/', '/bin', '/boot', '/dev', '/etc', '/home', '/lib', '/lib32', '/lib64',
44
+ '/libx32', '/media', '/mnt', '/opt', '/proc', '/root', '/run', '/sbin',
45
+ '/srv', '/sys', '/tmp', '/usr', '/var',
46
+ ]);
47
+
48
+ /** Interactive question groups, in the order they are asked. */
49
+ const GROUPS = Object.freeze([
50
+ { key: 'comfyui', title: 'Animastor ComfyUI (incl. the installer-created venv)' },
51
+ { key: 'models', title: 'Models installed by Animastor' },
52
+ { key: 'custom_nodes', title: 'Custom Nodes installed by Animastor' },
53
+ { key: 'worker', title: 'Animastor Worker (bundle files the installer deployed + .env it created)' },
54
+ { key: 'services', title: 'Cleaner / related service artifacts (cleanup journal, registered services)' },
55
+ { key: 'config', title: 'Remaining Animastor configuration/state files (install-state.json)' },
56
+ ]);
57
+
58
+ // ---------------------------------------------------------------------------
59
+ // Path safety
60
+ // ---------------------------------------------------------------------------
61
+
62
+ /**
63
+ * Guard every deletion target.
64
+ * @returns {string} the resolved absolute path (safe to delete)
65
+ * @throws when the path is not safely deletable
66
+ */
67
+ function assertDeletablePath(p, { home = null } = {}) {
68
+ if (typeof p !== 'string' || p.length === 0) {
69
+ throw new Error(`refusing to delete: invalid path ${JSON.stringify(p)}`);
70
+ }
71
+ if (!path.isAbsolute(p)) {
72
+ throw new Error(`refusing to delete relative path: ${p}`);
73
+ }
74
+ const resolved = path.resolve(p);
75
+ if (resolved !== p && path.resolve(resolved) !== path.resolve(p)) {
76
+ throw new Error(`refusing to delete non-normalized path: ${p}`);
77
+ }
78
+ if (resolved === '/' || path.dirname(resolved) === resolved) {
79
+ throw new Error(`refusing to delete filesystem root: ${resolved}`);
80
+ }
81
+ if (PROTECTED_DIRS.includes(resolved)) {
82
+ throw new Error(`refusing to delete protected system directory: ${resolved}`);
83
+ }
84
+ for (const prefix of SYSTEM_PREFIXES) {
85
+ if (resolved === prefix || resolved.startsWith(prefix + '/')) {
86
+ throw new Error(`refusing to delete a path inside the system directory ${prefix}: ${resolved}`);
87
+ }
88
+ }
89
+ const homeResolved = home ? path.resolve(home) : null;
90
+ if (homeResolved) {
91
+ if (resolved === homeResolved) {
92
+ throw new Error(`refusing to delete the home directory: ${resolved}`);
93
+ }
94
+ if (homeResolved.startsWith(resolved + path.sep)) {
95
+ throw new Error(`refusing to delete a parent of the home directory: ${resolved}`);
96
+ }
97
+ }
98
+ return resolved;
99
+ }
100
+
101
+ // ---------------------------------------------------------------------------
102
+ // Plan construction (read-only)
103
+ // ---------------------------------------------------------------------------
104
+
105
+ function loadInstallRecord(io, statePath) {
106
+ if (!io.fs.existsSync(statePath)) return null;
107
+ let parsed;
108
+ try {
109
+ parsed = JSON.parse(io.fs.readFileSync(statePath, 'utf8'));
110
+ } catch (err) {
111
+ return { corrupt: true, error: err.message };
112
+ }
113
+ if (!parsed || parsed.state_version !== 1) return { corrupt: true, error: 'unsupported or missing state_version' };
114
+ const { normalizeState } = require('./engine/state');
115
+ return normalizeState(parsed);
116
+ }
117
+
118
+ function entryKind(io, absPath) {
119
+ try {
120
+ const st = io.fs.statSync(absPath);
121
+ return st.isDirectory ? 'dir' : 'file';
122
+ } catch (_) {
123
+ return 'missing';
124
+ }
125
+ }
126
+
127
+ function item({ label, path: p, type = 'dir', owned = true, removable = true, created = null, note = null, files = null }) {
128
+ return { label, path: p, type, owned, removable, created, note, files };
129
+ }
130
+
131
+ /**
132
+ * Build the full uninstall plan from the installation state.
133
+ * Pure/read-only: it inspects the state and disk presence but deletes nothing.
134
+ *
135
+ * @param {object} io
136
+ * @param {object} args { state, statePath, home }
137
+ * @returns {{ groups: object[], removable_count, skipped_count, has_any }}
138
+ */
139
+ function buildUninstallPlan(io, { state, statePath, home = null }) {
140
+ const comps = state.components || {};
141
+ const groups = [];
142
+ let removableCount = 0;
143
+ let skippedCount = 0;
144
+
145
+ const guard = (p) => {
146
+ try {
147
+ return { ok: true, path: assertDeletablePath(p, { home }) };
148
+ } catch (err) {
149
+ return { ok: false, error: err.message };
150
+ }
151
+ };
152
+
153
+ const addGroup = (key, items, skipped = []) => {
154
+ const removableItems = items.filter((i) => i.removable && i.path);
155
+ removableCount += removableItems.length;
156
+ skippedCount += skipped.length;
157
+ groups.push({ key, items, skipped });
158
+ };
159
+
160
+ // --- ComfyUI + venv -----------------------------------------------------
161
+ const comfyItems = [];
162
+ const comfySkipped = [];
163
+ if (comps.comfyui && comps.comfyui.path) {
164
+ const c = comps.comfyui;
165
+ if (c.owned === true) {
166
+ const g = guard(c.path);
167
+ const containsState = statePath && path.resolve(statePath).startsWith(path.resolve(c.path) + path.sep);
168
+ comfyItems.push(item({
169
+ label: `ComfyUI directory (created by the installer)${c.ref ? ` @ ${c.ref}` : ''}`,
170
+ path: g.ok ? g.path : c.path,
171
+ removable: g.ok,
172
+ note: g.ok
173
+ ? (containsState ? 'removing this directory also removes the install state/manifest inside it' : null)
174
+ : g.error,
175
+ owned: true,
176
+ }));
177
+ } else {
178
+ comfySkipped.push({
179
+ label: `ComfyUI at ${c.path}`,
180
+ reason: 'pre-existing (not created by the Animastor installer) — never removed by the uninstaller',
181
+ });
182
+ }
183
+ }
184
+ if (comps.venv && comps.venv.path && comps.venv.owned) {
185
+ const g = guard(comps.venv.path);
186
+ comfyItems.push(item({
187
+ label: `Python venv (created by the installer)${comps.venv.created === false ? ' — pre-existing venv inside the root' : ''}`,
188
+ path: g.ok ? g.path : comps.venv.path,
189
+ removable: g.ok,
190
+ created: comps.venv.created === true ? true : null,
191
+ note: g.ok ? null : g.error,
192
+ }));
193
+ }
194
+ addGroup('comfyui', comfyItems, comfySkipped);
195
+
196
+ // --- Models ---------------------------------------------------------------
197
+ const modelItems = [];
198
+ for (const e of comps.models || []) {
199
+ const kind = entryKind(io, e.path);
200
+ if (kind === 'missing') continue;
201
+ if (e.created === false && Array.isArray(e.files) && e.files.length > 0) {
202
+ // a directory that existed before the install — only registered
203
+ // downloaded files are ours
204
+ for (const f of e.files) {
205
+ const fp = path.join(e.path, f);
206
+ const g = guard(fp);
207
+ modelItems.push(item({
208
+ label: `model file ${f} (${e.id || 'model'})`,
209
+ path: g.ok ? g.path : fp,
210
+ type: 'file',
211
+ removable: g.ok,
212
+ created: false,
213
+ note: g.ok ? null : g.error,
214
+ }));
215
+ }
216
+ } else {
217
+ const g = guard(e.path);
218
+ modelItems.push(item({
219
+ label: `${e.id || 'model'} → ${e.path}`,
220
+ path: g.ok ? g.path : e.path,
221
+ type: kind === 'dir' ? 'dir' : 'file',
222
+ removable: g.ok,
223
+ created: e.created === true ? true : null,
224
+ note: g.ok ? null : g.error,
225
+ }));
226
+ }
227
+ }
228
+ addGroup('models', modelItems);
229
+
230
+ // --- Custom nodes ----------------------------------------------------------
231
+ const nodeItems = [];
232
+ for (const e of comps.custom_nodes || []) {
233
+ if (entryKind(io, e.path) === 'missing') continue;
234
+ const g = guard(e.path);
235
+ nodeItems.push(item({
236
+ label: `${e.id || 'custom node'} → ${e.path}`,
237
+ path: g.ok ? g.path : e.path,
238
+ removable: g.ok,
239
+ note: g.ok ? null : g.error,
240
+ }));
241
+ }
242
+ addGroup('custom_nodes', nodeItems);
243
+
244
+ // --- Worker ------------------------------------------------------------------
245
+ const workerItems = [];
246
+ const workerSkipped = [];
247
+ if (comps.worker && comps.worker.dir) {
248
+ const w = comps.worker;
249
+ if (w.owned === true) {
250
+ const g = guard(w.dir);
251
+ workerItems.push(item({
252
+ label: `Worker directory (created by the installer) → ${w.dir}`,
253
+ path: g.ok ? g.path : w.dir,
254
+ removable: g.ok,
255
+ note: g.ok ? null : g.error,
256
+ }));
257
+ } else {
258
+ workerSkipped.push({
259
+ label: `Worker directory ${w.dir}`,
260
+ reason: 'pre-existing — only files deployed by the installer are removable',
261
+ });
262
+ for (const f of w.files_installed || []) {
263
+ const fp = path.join(w.dir, f);
264
+ const g = guard(fp);
265
+ workerItems.push(item({
266
+ label: `worker file deployed by the installer → ${f}`,
267
+ path: g.ok ? g.path : fp,
268
+ type: 'file',
269
+ removable: g.ok,
270
+ note: g.ok ? null : g.error,
271
+ }));
272
+ }
273
+ if ((w.files_installed || []).includes('package.json')) {
274
+ const nm = path.join(w.dir, 'node_modules');
275
+ if (io.fs.existsSync(nm)) {
276
+ const g = guard(nm);
277
+ workerItems.push(item({
278
+ label: 'worker node_modules (installed for the deployed package.json)',
279
+ path: g.ok ? g.path : nm,
280
+ removable: g.ok,
281
+ note: g.ok ? null : g.error,
282
+ }));
283
+ }
284
+ }
285
+ }
286
+ if (w.env_created === true) {
287
+ const envPath = path.join(w.dir, '.env');
288
+ if (io.fs.existsSync(envPath)) {
289
+ const g = guard(envPath);
290
+ workerItems.push(item({
291
+ label: 'worker .env (created by the installer; contains the Worker Key)',
292
+ path: g.ok ? g.path : envPath,
293
+ type: 'file',
294
+ removable: g.ok,
295
+ note: g.ok ? null : g.error,
296
+ }));
297
+ }
298
+ } else if (w.env_created === false) {
299
+ workerSkipped.push({
300
+ label: `worker .env at ${path.join(w.dir, '.env')}`,
301
+ reason: 'pre-existing (the installer only merged keys into it) — left in place; remove Animastor keys manually if desired',
302
+ });
303
+ }
304
+ }
305
+ addGroup('worker', workerItems, workerSkipped);
306
+
307
+ // --- Services / cleaner artifacts ---------------------------------------------
308
+ const serviceItems = [];
309
+ if (comps.worker && comps.worker.dir && comps.worker.owned !== true) {
310
+ const journal = path.join(comps.worker.dir, 'cleanup-journal');
311
+ if (io.fs.existsSync(journal)) {
312
+ const g = guard(journal);
313
+ serviceItems.push(item({
314
+ label: `worker cleanup journal → ${journal}`,
315
+ path: g.ok ? g.path : journal,
316
+ removable: g.ok,
317
+ note: g.ok ? null : g.error,
318
+ }));
319
+ }
320
+ const envVal = readEnvValue(io, path.join(comps.worker.dir, '.env'), 'WORKER_JOURNAL_DIR');
321
+ if (envVal && io.fs.existsSync(envVal) && envVal !== journal) {
322
+ const g = guard(envVal);
323
+ serviceItems.push(item({
324
+ label: `worker cleanup journal (WORKER_JOURNAL_DIR) → ${envVal}`,
325
+ path: g.ok ? g.path : envVal,
326
+ removable: g.ok,
327
+ note: g.ok ? null : g.error,
328
+ }));
329
+ }
330
+ }
331
+ for (const e of comps.services || []) {
332
+ if (entryKind(io, e.path) === 'missing') continue;
333
+ const g = guard(e.path);
334
+ serviceItems.push(item({
335
+ label: `${e.id || 'service artifact'} → ${e.path}`,
336
+ path: g.ok ? g.path : e.path,
337
+ type: e.type || 'dir',
338
+ removable: g.ok,
339
+ note: g.ok ? null : g.error,
340
+ }));
341
+ }
342
+ addGroup('services', serviceItems);
343
+
344
+ // --- Config / state --------------------------------------------------------------
345
+ const configItems = [];
346
+ if (io.fs.existsSync(statePath)) {
347
+ configItems.push(item({
348
+ label: `installation state/manifest → ${statePath}`,
349
+ path: statePath,
350
+ type: 'file',
351
+ removable: true,
352
+ note: 'removed last; also removes the containing directory when empty',
353
+ }));
354
+ }
355
+ addGroup('config', configItems);
356
+
357
+ return {
358
+ groups,
359
+ removable_count: removableCount,
360
+ skipped_count: skippedCount,
361
+ has_any: removableCount > 0 || skippedCount > 0,
362
+ };
363
+ }
364
+
365
+ /** Read a single value from a .env file. The value is never logged. */
366
+ function readEnvValue(io, envPath, key) {
367
+ try {
368
+ const text = io.fs.readFileSync(envPath, 'utf8');
369
+ for (const line of text.split('\n')) {
370
+ const m = new RegExp(`^${key}\\s*=\\s*(.*)$`).exec(line.trim());
371
+ if (m && m[1]) {
372
+ return m[1].trim().replace(/^["']|["']$/g, '');
373
+ }
374
+ }
375
+ } catch (_) { /* no .env */ }
376
+ return null;
377
+ }
378
+
379
+ // ---------------------------------------------------------------------------
380
+ // Rendering
381
+ // ---------------------------------------------------------------------------
382
+
383
+ function renderUninstallPlan(plan, { state = null } = {}) {
384
+ const lines = [];
385
+ lines.push('Animastor Worker Uninstaller');
386
+ if (state) {
387
+ const when = state.updated || state.created || 'unknown time';
388
+ lines.push(`Recorded installation: profiles ${(state.profiles || []).join(' + ') || '—'}, device ${state.device || 'unknown'}, updated ${when}`);
389
+ }
390
+ lines.push('');
391
+ for (const group of plan.groups) {
392
+ if (group.items.length === 0 && group.skipped.length === 0) continue;
393
+ const groupDef = GROUPS.find((g) => g.key === group.key);
394
+ lines.push(`[${groupDef ? groupDef.title : group.key}]`);
395
+ for (const it of group.items) {
396
+ const size = it.removable ? '' : ' (NOT removable — see note)';
397
+ lines.push(` - ${it.label}${size}`);
398
+ if (it.note) lines.push(` note: ${it.note}`);
399
+ }
400
+ for (const s of group.skipped) {
401
+ lines.push(` - ${s.label} — KEPT: ${s.reason}`);
402
+ }
403
+ lines.push('');
404
+ }
405
+ lines.push(`Removable components: ${plan.removable_count}; pre-existing (kept): ${plan.skipped_count}`);
406
+ return lines.join('\n').trim();
407
+ }
408
+
409
+ // ---------------------------------------------------------------------------
410
+ // Execution
411
+ // ---------------------------------------------------------------------------
412
+
413
+ /**
414
+ * Find live processes whose working directory is under any of the paths
415
+ * (ComfyUI server, worker). Delegated to the platform adapter (Linux: a
416
+ * read-only /proc scan; on Windows cwd-based discovery is unsupported and
417
+ * the adapter reports no pids — running workers must be stopped first).
418
+ * @returns {number[]} pids
419
+ */
420
+ function findProcessesUsingPaths(io, paths, platformAdapter = null) {
421
+ if (!paths || paths.length === 0) return [];
422
+ return platforms.getPlatformAdapter(platformAdapter).findPidsByCwdPrefix(io, paths);
423
+ }
424
+
425
+ /**
426
+ * Execute the uninstall plan.
427
+ * @param {object} args { io, plan, answers: {comfyui,models,custom_nodes,worker,services,config},
428
+ * statePath, log, dryRun }
429
+ * @returns {{ results: object[], removed: number, kept: number, failed: number }}
430
+ */
431
+ function runUninstallation(io, { plan, answers, statePath, log = null, dryRun = false }) {
432
+ const results = [];
433
+ let removed = 0;
434
+ let kept = 0;
435
+ let failed = 0;
436
+
437
+ const say = (msg) => { if (log && log.info) log.info(msg); };
438
+
439
+ // 0. stop live processes for the paths that are about to be removed
440
+ const targetPaths = [];
441
+ for (const group of plan.groups) {
442
+ if (answers[group.key] !== true) continue;
443
+ for (const it of group.items) {
444
+ if (it.removable) targetPaths.push(it.path);
445
+ }
446
+ }
447
+ const pids = findProcessesUsingPaths(io, targetPaths);
448
+ for (const pid of pids) {
449
+ if (dryRun) {
450
+ results.push({ path: `pid ${pid}`, status: 'would-stop-process' });
451
+ } else {
452
+ const r = io.exec('kill', ['-TERM', String(pid)]);
453
+ results.push({ path: `pid ${pid}`, status: r.code === 0 ? 'stopped-process' : 'stop-failed' });
454
+ if (r.code !== 0) failed += 1;
455
+ else say(`stopped process ${pid} (SIGTERM)`);
456
+ }
457
+ }
458
+
459
+ // 1. component deletions (state file group handled last)
460
+ for (const group of plan.groups) {
461
+ if (group.key === 'config') continue;
462
+ if (answers[group.key] !== true) {
463
+ for (const it of group.items) {
464
+ results.push({ path: it.path, status: 'kept-by-user' });
465
+ kept += 1;
466
+ }
467
+ continue;
468
+ }
469
+ for (const it of group.items) {
470
+ if (!it.removable) {
471
+ results.push({ path: it.path, status: 'skipped-pre-existing', note: it.note });
472
+ kept += 1;
473
+ continue;
474
+ }
475
+ if (dryRun) {
476
+ results.push({ path: it.path, status: `would-remove-${it.type}` });
477
+ removed += 1;
478
+ continue;
479
+ }
480
+ let abs;
481
+ try {
482
+ abs = assertDeletablePath(it.path, {});
483
+ } catch (err) {
484
+ results.push({ path: it.path, status: 'failed', error: err.message });
485
+ failed += 1;
486
+ continue;
487
+ }
488
+ if (!io.fs.existsSync(abs)) {
489
+ results.push({ path: abs, status: 'missing' });
490
+ continue;
491
+ }
492
+ try {
493
+ if (it.type === 'file') {
494
+ io.fs.unlinkSync(abs);
495
+ } else {
496
+ io.fs.rmSync(abs, { recursive: true, force: true });
497
+ }
498
+ results.push({ path: abs, status: 'removed' });
499
+ removed += 1;
500
+ say(`removed: ${abs}`);
501
+ } catch (err) {
502
+ results.push({ path: abs, status: 'failed', error: err.message });
503
+ failed += 1;
504
+ }
505
+ }
506
+ }
507
+
508
+ // 2. config/state last — so a failed run keeps its record
509
+ if (answers.config === true) {
510
+ const configGroup = plan.groups.find((g) => g.key === 'config');
511
+ for (const it of configGroup ? configGroup.items : []) {
512
+ if (dryRun) {
513
+ results.push({ path: it.path, status: 'would-remove-file' });
514
+ removed += 1;
515
+ continue;
516
+ }
517
+ try {
518
+ const abs = assertDeletablePath(it.path, {});
519
+ if (io.fs.existsSync(abs)) {
520
+ io.fs.unlinkSync(abs);
521
+ results.push({ path: abs, status: 'removed' });
522
+ removed += 1;
523
+ say(`removed: ${abs}`);
524
+ } else {
525
+ results.push({ path: abs, status: 'missing' });
526
+ }
527
+ // drop the state dir when empty (best effort)
528
+ const dir = path.dirname(abs);
529
+ try { io.fs.rmdirSync(dir); } catch (_) { /* not empty or absent */ }
530
+ } catch (err) {
531
+ results.push({ path: it.path, status: 'failed', error: err.message });
532
+ failed += 1;
533
+ }
534
+ }
535
+ } else {
536
+ const configGroup = plan.groups.find((g) => g.key === 'config');
537
+ for (const it of configGroup ? configGroup.items : []) {
538
+ results.push({ path: it.path, status: 'kept-by-user' });
539
+ kept += 1;
540
+ }
541
+ }
542
+
543
+ return { results, removed, kept, failed };
544
+ }
545
+
546
+ function renderUninstallResult(outcome) {
547
+ const lines = [];
548
+ lines.push('Uninstall results:');
549
+ for (const r of outcome.results) {
550
+ lines.push(` - ${r.status}: ${r.path}${r.error ? ` (${r.error})` : ''}${r.note ? ` [${r.note}]` : ''}`);
551
+ }
552
+ lines.push('');
553
+ lines.push(`Removed: ${outcome.removed}, kept: ${outcome.kept}, failed: ${outcome.failed}`);
554
+ if (outcome.failed > 0) {
555
+ lines.push('Some components could not be removed — they are listed above. Nothing unregistered was touched.');
556
+ }
557
+ return lines.join('\n').trim();
558
+ }
559
+
560
+ module.exports = {
561
+ PROTECTED_DIRS,
562
+ SYSTEM_PREFIXES,
563
+ GROUPS,
564
+ assertDeletablePath,
565
+ loadInstallRecord,
566
+ buildUninstallPlan,
567
+ renderUninstallPlan,
568
+ findProcessesUsingPaths,
569
+ runUninstallation,
570
+ renderUninstallResult,
571
+ readEnvValue,
572
+ };