doctor.mjs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env node
  2. /**
  3. * Preflight. Run this BEFORE the interview, not after the first failure.
  4. *
  5. * node scripts/doctor.mjs check everything
  6. * node scripts/doctor.mjs --probe also spend one API call to read the balance
  7. *
  8. * Every check that can fail deep inside a build with a misleading message is
  9. * checked here with an honest one. The three that actually bite:
  10. *
  11. * - a STRIPPED ffmpeg on PATH. It carries ~50 filters and silently lacks
  12. * scale, fps, psnr and the webp muxer, then fails with "No option name
  13. * near ..." or "Unable to choose an output format", both of which read as
  14. * a mistake in your command rather than a missing feature.
  15. * - no KIE_AI_API_KEY, which only matters if you are generating assets.
  16. * - playwright-core resolving from the wrong directory. It is required from
  17. * the BUILD folder, not from the skill.
  18. */
  19. import fs from "node:fs";
  20. import path from "node:path";
  21. import { execFileSync } from "node:child_process";
  22. import { createRequire } from "node:module";
  23. import { fileURLToPath } from "node:url";
  24. import { paths } from "./workspace.mjs";
  25. const HERE = path.dirname(fileURLToPath(import.meta.url));
  26. const rows = [];
  27. const add = (sev, name, ok, detail, fix) => rows.push({ sev, name, ok, detail, fix });
  28. const run = (cmd, args) => {
  29. try {
  30. return execFileSync(cmd, args, { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] });
  31. } catch {
  32. return null;
  33. }
  34. };
  35. // ---------------------------------------------------------------- node ----
  36. const major = Number(process.versions.node.split(".")[0]);
  37. add("required", "node", major >= 18, `v${process.versions.node}`,
  38. "Install Node 18 or newer.");
  39. // -------------------------------------------------------------- ffmpeg ----
  40. function globWinGet() {
  41. const home = process.env.USERPROFILE || process.env.HOME || "";
  42. const base = path.join(home, "AppData/Local/Microsoft/WinGet/Packages");
  43. if (!fs.existsSync(base)) return [];
  44. const out = [];
  45. for (const d of fs.readdirSync(base)) {
  46. if (!/^Gyan\.FFmpeg/i.test(d)) continue;
  47. const inner = path.join(base, d);
  48. for (const e of fs.readdirSync(inner)) {
  49. const p = path.join(inner, e, "bin/ffmpeg.exe");
  50. if (fs.existsSync(p)) out.push(p);
  51. }
  52. }
  53. return out;
  54. }
  55. const candidates = [
  56. process.env.SCROLLCRAFT_FFMPEG,
  57. "ffmpeg",
  58. ...globWinGet(),
  59. "/usr/local/bin/ffmpeg",
  60. "/opt/homebrew/bin/ffmpeg",
  61. "/usr/bin/ffmpeg",
  62. "/snap/bin/ffmpeg",
  63. ].filter(Boolean);
  64. let ffmpeg = null, filterCount = 0;
  65. for (const c of candidates) {
  66. const out = run(c, ["-hide_banner", "-filters"]);
  67. if (!out) continue;
  68. const n = out.split("\n").length;
  69. if (n > filterCount) { filterCount = n; ffmpeg = c; }
  70. if (n > 200) break;
  71. }
  72. add("required", "ffmpeg (full build)", filterCount > 200,
  73. ffmpeg ? `${ffmpeg} (${filterCount} filters)` : "not found",
  74. "A stripped ffmpeg lacks scale/fps/psnr and the webp muxer. Install a full build (Windows: winget install Gyan.FFmpeg) or set SCROLLCRAFT_FFMPEG to one.");
  75. if (ffmpeg && filterCount > 200) {
  76. const enc = run(ffmpeg, ["-hide_banner", "-encoders"]) || "";
  77. add("optional", " └ libwebp encoder", /libwebp/.test(enc),
  78. /libwebp/.test(enc) ? "present" : "missing",
  79. "Posters fall back to JPEG. Not fatal, just heavier.");
  80. }
  81. // ---------------------------------------------------------- playwright ----
  82. let pw = false, pwWhere = "";
  83. try {
  84. createRequire(path.join(process.cwd(), "package.json"))("playwright-core");
  85. pw = true; pwWhere = "resolves from cwd";
  86. } catch {
  87. try {
  88. createRequire(path.join(HERE, "package.json"))("playwright-core");
  89. pw = true; pwWhere = "resolves from the skill, but NOT from cwd";
  90. } catch { pwWhere = "not installed"; }
  91. }
  92. add("verify", "playwright-core", pw, pwWhere,
  93. "Run `npm i playwright-core` inside the build folder. Only needed for the verification pass.");
  94. const chrome = [
  95. process.env.SCROLLCRAFT_CHROME,
  96. // Windows
  97. "C:/Program Files/Google/Chrome/Application/chrome.exe",
  98. "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
  99. "C:/Program Files/Microsoft/Edge/Application/msedge.exe",
  100. // macOS
  101. "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
  102. "/Applications/Chromium.app/Contents/MacOS/Chromium",
  103. "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
  104. // Linux
  105. "/usr/bin/google-chrome",
  106. "/usr/bin/google-chrome-stable",
  107. "/usr/bin/chromium",
  108. "/usr/bin/chromium-browser",
  109. "/snap/bin/chromium",
  110. ].find((p) => p && fs.existsSync(p));
  111. add("verify", "Chrome", Boolean(chrome), chrome || "not found",
  112. "Install Chrome, or set SCROLLCRAFT_CHROME to an executable.");
  113. // ------------------------------------------------------------- kie key ----
  114. function findKey() {
  115. if (process.env.KIE_AI_API_KEY) return "env";
  116. let dir = process.cwd();
  117. for (let i = 0; i < 8; i++) {
  118. const p = path.join(dir, ".env");
  119. if (fs.existsSync(p) && /^\s*KIE_AI_API_KEY\s*=\s*\S+/m.test(fs.readFileSync(p, "utf8"))) return p;
  120. const up = path.dirname(dir);
  121. if (up === dir) break;
  122. dir = up;
  123. }
  124. return null;
  125. }
  126. const keyWhere = findKey();
  127. add("optional", "KIE_AI_API_KEY", Boolean(keyWhere), keyWhere || "not set",
  128. "Only needed to GENERATE imagery. Building from your own photos and footage needs no key and no spend. Copy .env.example to .env to set one.");
  129. // ----------------------------------------------------------- workspace ----
  130. let ws = null;
  131. try {
  132. ws = paths();
  133. add("required", "workspace", true, `${ws.workspace}\n via ${ws.via}`, "");
  134. add("optional", " └ registry", fs.existsSync(ws.fingerprints),
  135. fs.existsSync(ws.fingerprints) ? "present" : "not created yet",
  136. "Run `node scripts/workspace.mjs --ensure` to create it.");
  137. } catch (e) {
  138. add("required", "workspace", false, e.message, "Fix or delete the offending .scrollcraft.json.");
  139. }
  140. // -------------------------------------------------------------- report ----
  141. const mark = (r) => (r.ok ? "\u001b[32m ok \u001b[0m" : r.sev === "required" ? "\u001b[31mFAIL\u001b[0m" : "\u001b[33mwarn\u001b[0m");
  142. console.log("\nscrollcraft preflight\n");
  143. for (const r of rows) {
  144. console.log(` [${mark(r)}] ${r.name.padEnd(22)} ${r.detail}`);
  145. if (!r.ok && r.fix) console.log(` ${"\u001b[2m"}${r.fix}${"\u001b[0m"}`);
  146. }
  147. const hardFails = rows.filter((r) => !r.ok && r.sev === "required");
  148. const softFails = rows.filter((r) => !r.ok && r.sev !== "required");
  149. console.log("");
  150. if (hardFails.length) {
  151. console.log(`\u001b[31m${hardFails.length} required check(s) failed. Fix these before building.\u001b[0m\n`);
  152. process.exit(1);
  153. }
  154. console.log(softFails.length
  155. ? `\u001b[33mReady, with ${softFails.length} optional item(s) missing (see above).\u001b[0m\n`
  156. : "\u001b[32mReady.\u001b[0m\n");
  157. if (process.argv.includes("--probe") && keyWhere) {
  158. const { execFileSync: x } = await import("node:child_process");
  159. try {
  160. console.log("credit: " + x(process.execPath, [path.join(HERE, "kie.mjs"), "probe"], { encoding: "utf8" }).trim().replace(/^credit:\s*/, ""));
  161. } catch (e) { console.log("balance probe failed: " + e.message); }
  162. }