@animastor/installer 0.1.1 → 0.2.9

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.
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Auto-fix planning: everything wrong with an EXISTING worker installation
5
+ * that the installer can repair by itself, collected into one list so the
6
+ * user answers ONE question ("Fix all?") instead of reading warnings.
7
+ *
8
+ * Fixable (non-secret, installer-owned):
9
+ * - .env wiring that differs from this run: WORKER_TYPE (from the profile),
10
+ * NODE_BIN (Node.js >= 20), COMFY_URL (probed), COMFY_PORT, HUB_URL;
11
+ * - an outdated worker bundle (version differs from the bundled source);
12
+ * - bundle dependencies that are declared but not installed (npm ci).
13
+ * The Worker Key is never part of it (`doctor --new-key` asks explicitly).
14
+ */
15
+
16
+ const path = require('path');
17
+ const workerMod = require('./worker');
18
+ const probe = require('./probe');
19
+
20
+ /** Keys the installer owns in the worker .env (never secrets). */
21
+ const FIXABLE_ENV_KEYS = Object.freeze(['WORKER_TYPE', 'NODE_BIN', 'COMFY_URL', 'NOTEBOOK_PATH', 'COMFY_PORT', 'HUB_URL']);
22
+
23
+ const WHY = {
24
+ WORKER_TYPE: 'must match the profile',
25
+ NODE_BIN: 'the worker needs Node.js >= 20',
26
+ COMFY_URL: 'the address where ComfyUI actually answers',
27
+ NOTEBOOK_PATH: 'follows the probed ComfyUI address',
28
+ COMFY_PORT: 'the port of this installation\'s ComfyUI',
29
+ HUB_URL: 'the hub this installer was downloaded from',
30
+ };
31
+
32
+ /**
33
+ * Differences between the current .env values and the desired wiring.
34
+ * Pure. Missing keys are NOT fixes (they are always added).
35
+ * @returns {Array<{ id, key, from, to, title }>}
36
+ */
37
+ function planEnvFixes(current, desired) {
38
+ const out = [];
39
+ for (const key of FIXABLE_ENV_KEYS) {
40
+ if (desired[key] === undefined || desired[key] === null) continue;
41
+ if (current[key] === undefined) continue;
42
+ const from = String(current[key]);
43
+ const to = String(desired[key]);
44
+ if (from === to) continue;
45
+ out.push({ id: `env:${key}`, key, from, to, title: `${key}: ${from || '(empty)'} → ${to || '(empty)'} (${WHY[key]})` });
46
+ }
47
+ return out;
48
+ }
49
+
50
+ /**
51
+ * Collect the fix list for the installation at workerDir.
52
+ * @param {object} io
53
+ * @param {object} opts { workerDir, sourceBundleDir, desired: {KEY: value} }
54
+ * @returns {{ items: Array<{ id, title }>, additions: string[] }}
55
+ */
56
+ function planFixes(io, { workerDir, sourceBundleDir = null, sourceVersion = null, desired = {} }) {
57
+ const items = [];
58
+ const additions = [];
59
+ if (!workerDir || !io.fs.existsSync(path.join(workerDir, 'worker.cjs'))) return { items, additions };
60
+
61
+ const installed = workerMod.readPkgVersion(io, workerDir);
62
+ const source = (sourceBundleDir ? workerMod.readPkgVersion(io, sourceBundleDir) : null) || sourceVersion;
63
+ if (installed && source && installed !== source) {
64
+ items.push({ id: 'bundle', title: `worker bundle ${installed} → ${source} (worker files only; .env is kept)` });
65
+ }
66
+
67
+ const deps = Object.keys(workerMod.declaredDependencies(io, workerDir));
68
+ if (deps.length > 0 && !io.fs.existsSync(path.join(workerDir, 'node_modules'))) {
69
+ items.push({ id: 'deps', title: `install worker dependencies (${deps.join(', ')}): npm ci in ${workerDir}` });
70
+ }
71
+
72
+ if (io.fs.existsSync(path.join(workerDir, '.env'))) {
73
+ const current = workerMod.readEnvValues(io, workerDir);
74
+ for (const f of planEnvFixes(current, desired)) items.push(f);
75
+ for (const key of FIXABLE_ENV_KEYS) {
76
+ if (desired[key] !== undefined && desired[key] !== '' && current[key] === undefined) additions.push(`${key}=${desired[key]}`);
77
+ }
78
+ if (current.NODE_BIN && probe.nodeMajor(probe.nodeBinaryVersion(io, current.NODE_BIN)) < 20 && !items.some((i) => i.key === 'NODE_BIN') && desired.NODE_BIN) {
79
+ items.push({ id: 'env:NODE_BIN', key: 'NODE_BIN', from: current.NODE_BIN, to: desired.NODE_BIN, title: `NODE_BIN: ${current.NODE_BIN} is missing or older than Node.js 20 → ${desired.NODE_BIN}` });
80
+ }
81
+ }
82
+ // ONE entry per fix: two rules can describe the same key (a stale
83
+ // NODE_BIN and the generic env diff) — the list the user confirms and the
84
+ // list the engine applies must be the same, and neither may show a
85
+ // change that is not going to happen.
86
+ const seen = new Set();
87
+ const unique = items.filter((i) => {
88
+ const id = i.id || i.key;
89
+ if (seen.has(id)) return false;
90
+ seen.add(id);
91
+ return true;
92
+ });
93
+ return { items: unique, additions };
94
+ }
95
+
96
+ module.exports = { FIXABLE_ENV_KEYS, planEnvFixes, planFixes };
@@ -38,10 +38,20 @@ function createRealIo() {
38
38
  chmodSync: (p, mode) => fs.chmodSync(p, mode),
39
39
  statSync: (p) => {
40
40
  const s = fs.statSync(p);
41
- return { size: s.size, isFile: s.isFile(), isDirectory: s.isDirectory(), uid: s.uid, gid: s.gid };
41
+ return { size: s.size, isFile: s.isFile(), isDirectory: s.isDirectory(), uid: s.uid, gid: s.gid, mtimeMs: s.mtimeMs };
42
42
  },
43
43
  readdirSync: (p) => fs.readdirSync(p),
44
44
  readlinkSync: (p) => fs.readlinkSync(p),
45
+ symlinkSync: (target, p) => fs.symlinkSync(target, p),
46
+ /** lstat without following links; null when the path does not exist. */
47
+ lstatSync: (p) => {
48
+ try {
49
+ const s = fs.lstatSync(p);
50
+ return { isSymbolicLink: s.isSymbolicLink(), isFile: s.isFile(), isDirectory: s.isDirectory() };
51
+ } catch (_) {
52
+ return null;
53
+ }
54
+ },
45
55
  },
46
56
  /** Synchronous command execution (git, pip, npm, nvidia-smi…). */
47
57
  exec(command, args = [], opts = {}) {
@@ -243,6 +253,7 @@ function createRealIo() {
243
253
  function createMemoryFs(initial = {}) {
244
254
  const files = new Map(); // path -> { data: Buffer|string, mode }
245
255
  const dirs = new Set(['/']);
256
+ const links = new Map(); // path -> target (symlinks)
246
257
  for (const [p, data] of Object.entries(initial)) {
247
258
  files.set(norm(p), { data, mode: 0o644 });
248
259
  let d = path.dirname(norm(p));
@@ -288,7 +299,8 @@ function createMemoryFs(initial = {}) {
288
299
  if (opts && opts.recursive) {
289
300
  let d = target;
290
301
  const stack = [];
291
- while (d && d !== '/' && !dirs.has(d)) { stack.push(d); d = path.dirname(d); }
302
+ // guard: dirname('.') === '.' — the loop must always make progress
303
+ while (d && d !== '/' && !dirs.has(d) && d !== path.dirname(d)) { stack.push(d); d = path.dirname(d); }
292
304
  for (const dd of stack.reverse()) dirs.add(dd);
293
305
  return;
294
306
  }
@@ -327,6 +339,7 @@ function createMemoryFs(initial = {}) {
327
339
  throw Object.assign(new Error(`ENOENT: ${a}`), { code: 'ENOENT' });
328
340
  },
329
341
  unlinkSync: (p) => {
342
+ if (links.has(norm(p))) { links.delete(norm(p)); return; }
330
343
  if (!files.has(norm(p))) throw Object.assign(new Error(`ENOENT: ${p}`), { code: 'ENOENT' });
331
344
  files.delete(norm(p));
332
345
  },
@@ -387,6 +400,21 @@ function createMemoryFs(initial = {}) {
387
400
  }
388
401
  return Array.from(out).sort();
389
402
  },
403
+ symlinkSync: (target, p) => {
404
+ assertParent(p);
405
+ if (files.has(norm(p)) || links.has(norm(p))) throw Object.assign(new Error(`EEXIST: ${p}`), { code: 'EEXIST' });
406
+ links.set(norm(p), String(target));
407
+ },
408
+ readlinkSync: (p) => {
409
+ if (!links.has(norm(p))) throw Object.assign(new Error(`EINVAL: ${p}`), { code: 'EINVAL' });
410
+ return links.get(norm(p));
411
+ },
412
+ lstatSync: (p) => {
413
+ if (links.has(norm(p))) return { isSymbolicLink: true, isFile: false, isDirectory: false };
414
+ if (files.has(norm(p))) return { isSymbolicLink: false, isFile: true, isDirectory: false };
415
+ if (dirs.has(norm(p))) return { isSymbolicLink: false, isFile: false, isDirectory: true };
416
+ return null;
417
+ },
390
418
  };
391
419
  }
392
420
 
@@ -415,6 +443,9 @@ function createDryRunIo(io) {
415
443
  rmdirSync: guard('fs.rmdirSync'),
416
444
  copyFileSync: guard('fs.copyFileSync'),
417
445
  chmodSync: guard('fs.chmodSync'),
446
+ symlinkSync: guard('fs.symlinkSync'),
447
+ readlinkSync: io.fs.readlinkSync,
448
+ lstatSync: io.fs.lstatSync,
418
449
  },
419
450
  exec: guard('exec'),
420
451
  spawnDaemon: guard('spawnDaemon'),