workspace.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env node
  2. /**
  3. * Resolve the scrollcraft workspace: the one directory that holds this user's
  4. * builds and their fingerprint registry.
  5. *
  6. * The skill used to hardcode one author's repo layout, which meant the
  7. * fingerprint gate dead-ended on every other machine. Nothing is hardcoded now.
  8. * Resolution order, first hit wins:
  9. *
  10. * 1. SCROLLCRAFT_HOME env var, absolute or relative to cwd
  11. * 2. .scrollcraft.json nearest one walking up from cwd,
  12. * { "workspace": "some/path" } relative to
  13. * the file that declares it
  14. * 3. <project root>/scrollcraft project root = nearest ancestor with .git,
  15. * otherwise cwd
  16. *
  17. * USAGE
  18. * node scripts/workspace.mjs print the workspace path
  19. * node scripts/workspace.mjs --json print every resolved path
  20. * node scripts/workspace.mjs --ensure create it and seed the registry
  21. */
  22. import fs from "node:fs";
  23. import path from "node:path";
  24. import { fileURLToPath } from "node:url";
  25. const HERE = path.dirname(fileURLToPath(import.meta.url));
  26. const SKILL = path.resolve(HERE, "..");
  27. function findUp(startDir, predicate) {
  28. let dir = path.resolve(startDir);
  29. for (let i = 0; i < 12; i++) {
  30. const hit = predicate(dir);
  31. if (hit) return { dir, hit };
  32. const up = path.dirname(dir);
  33. if (up === dir) break;
  34. dir = up;
  35. }
  36. return null;
  37. }
  38. export function resolveWorkspace(cwd = process.cwd()) {
  39. if (process.env.SCROLLCRAFT_HOME) {
  40. return { root: path.resolve(cwd, process.env.SCROLLCRAFT_HOME), via: "SCROLLCRAFT_HOME" };
  41. }
  42. const cfg = findUp(cwd, (d) => {
  43. const p = path.join(d, ".scrollcraft.json");
  44. return fs.existsSync(p) ? p : null;
  45. });
  46. if (cfg) {
  47. let parsed = {};
  48. try {
  49. parsed = JSON.parse(fs.readFileSync(cfg.hit, "utf8"));
  50. } catch (e) {
  51. throw new Error(`.scrollcraft.json is not valid JSON: ${cfg.hit}\n ${e.message}`);
  52. }
  53. if (parsed.workspace) {
  54. return { root: path.resolve(cfg.dir, parsed.workspace), via: cfg.hit };
  55. }
  56. }
  57. const git = findUp(cwd, (d) => (fs.existsSync(path.join(d, ".git")) ? d : null));
  58. const base = git ? git.hit : path.resolve(cwd);
  59. return { root: path.join(base, "scrollcraft"), via: git ? "project root (.git)" : "cwd" };
  60. }
  61. export function paths(cwd = process.cwd()) {
  62. const { root, via } = resolveWorkspace(cwd);
  63. return {
  64. via,
  65. workspace: root,
  66. builds: path.join(root, "builds"),
  67. lab: path.join(root, "lab"),
  68. fingerprints: path.join(root, "FINGERPRINTS.md"),
  69. };
  70. }
  71. export function ensure(cwd = process.cwd()) {
  72. const p = paths(cwd);
  73. fs.mkdirSync(p.builds, { recursive: true });
  74. fs.mkdirSync(p.lab, { recursive: true });
  75. let seeded = false;
  76. if (!fs.existsSync(p.fingerprints)) {
  77. const seed = path.join(SKILL, "templates", "FINGERPRINTS.md");
  78. fs.copyFileSync(seed, p.fingerprints);
  79. seeded = true;
  80. }
  81. return { ...p, seeded };
  82. }
  83. // ------------------------------------------------------------------- cli ----
  84. if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url))) {
  85. const argv = process.argv.slice(2);
  86. const p = argv.includes("--ensure") ? ensure() : paths();
  87. if (argv.includes("--json")) {
  88. console.log(JSON.stringify(p, null, 2));
  89. } else if (argv.includes("--ensure")) {
  90. console.log(p.workspace);
  91. console.error(` builds ${p.builds}`);
  92. console.error(` registry ${p.fingerprints}${p.seeded ? " (seeded, empty)" : " (already present)"}`);
  93. console.error(` resolved via ${p.via}`);
  94. } else {
  95. console.log(p.workspace);
  96. }
  97. }