@astryxdesign/cli 0.7.15 → 0.7.16-canary.g3aa244b

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 (163) hide show
  1. package/README.md +5 -3
  2. package/api/build/_adapter.d.mts +1 -0
  3. package/api/build/blocks/blocks.d.mts +1 -0
  4. package/api/build/blocks/blocks.mjs +18 -1
  5. package/api/component/_adapter.d.mts +1 -1
  6. package/api/component/_adapter.mjs +28 -1
  7. package/api/doctor/_adapter.d.mts +35 -0
  8. package/api/doctor/_adapter.mjs +69 -0
  9. package/api/doctor/built-checks.mjs +1 -1
  10. package/api/doctor/doctor.d.mts +18 -0
  11. package/api/doctor/doctor.doc.mjs +1 -1
  12. package/api/doctor/doctor.mjs +104 -4
  13. package/api/doctor/theme-checks.mjs +6 -10
  14. package/api/integration/add-contribution.mjs +3 -3
  15. package/api/integration/pack-check.mjs +14 -8
  16. package/api/integration/validate-integration.mjs +1 -1
  17. package/api/search/_adapter.mjs +15 -16
  18. package/api/search/_entries.d.mts +2 -1
  19. package/api/search/_entries.mjs +28 -1
  20. package/api/swizzle/_moduleGraph.mjs +1 -1
  21. package/api/theme/_adapter.d.mts +5 -2
  22. package/api/theme/_adapter.mjs +43 -12
  23. package/api/theme/_fork.mjs +1 -1
  24. package/api/theme/build/build.d.mts +116 -32
  25. package/api/theme/build/build.mjs +449 -102
  26. package/api/theme/eject/eject.mjs +22 -1
  27. package/api/theme/template/template.d.mts +19 -0
  28. package/api/theme/template/template.mjs +102 -25
  29. package/api/theme/theme.type.d.mts +5 -1
  30. package/api/theme/theme.type.mjs +2 -1
  31. package/api/theme/themeBuild.doc.mjs +3 -3
  32. package/api/theme/themeEject.doc.mjs +1 -1
  33. package/api/theme/themeTemplate.doc.mjs +9 -3
  34. package/api/theme-edit/_adapter.d.mts +32 -2
  35. package/api/theme-edit/_adapter.mjs +264 -40
  36. package/api/theme-edit/_check.d.mts +93 -4
  37. package/api/theme-edit/_check.mjs +1727 -223
  38. package/api/theme-edit/_color.d.mts +2 -8
  39. package/api/theme-edit/_color.mjs +292 -219
  40. package/api/theme-edit/_compiler.d.mts +1 -0
  41. package/api/theme-edit/_compiler.mjs +20 -1
  42. package/api/theme-edit/_context.d.mts +31 -1
  43. package/api/theme-edit/_context.mjs +445 -41
  44. package/api/theme-edit/_css.d.mts +60 -1
  45. package/api/theme-edit/_css.mjs +404 -28
  46. package/api/theme-edit/_edit.d.mts +25 -0
  47. package/api/theme-edit/_edit.mjs +1532 -149
  48. package/api/theme-edit/_how-commands.d.mts +11 -26
  49. package/api/theme-edit/_how-commands.mjs +30 -2
  50. package/api/theme-edit/_how-question.d.mts +7 -0
  51. package/api/theme-edit/_how-question.mjs +1304 -176
  52. package/api/theme-edit/_how.mjs +144 -30
  53. package/api/theme-edit/_overview.mjs +2 -0
  54. package/api/theme-edit/_palette.d.mts +38 -0
  55. package/api/theme-edit/_palette.mjs +305 -71
  56. package/api/theme-edit/_parts.d.mts +33 -0
  57. package/api/theme-edit/_parts.mjs +138 -9
  58. package/api/theme-edit/_png.d.mts +18 -0
  59. package/api/theme-edit/_png.mjs +95 -14
  60. package/api/theme-edit/_preview-app.d.mts +36 -1
  61. package/api/theme-edit/_preview-app.mjs +1415 -96
  62. package/api/theme-edit/_preview.d.mts +64 -3
  63. package/api/theme-edit/_preview.mjs +514 -31
  64. package/api/theme-edit/_project.d.mts +11 -0
  65. package/api/theme-edit/_project.mjs +46 -12
  66. package/api/theme-edit/_settings.d.mts +278 -5
  67. package/api/theme-edit/_settings.mjs +2741 -195
  68. package/api/theme-edit/_tables.d.mts +24 -0
  69. package/api/theme-edit/_tables.mjs +211 -149
  70. package/api/theme-edit/_vet.d.mts +42 -0
  71. package/api/theme-edit/_vet.mjs +144 -0
  72. package/api/theme-edit/theme-edit.type.d.mts +20 -2
  73. package/api/theme-edit/theme-edit.type.mjs +6 -2
  74. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  75. package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
  76. package/api/theme-edit/themeEditShow.doc.mjs +11 -3
  77. package/api/upgrade/registry/registry.mjs +6 -1
  78. package/assets/codemods/parse.mjs +2 -2
  79. package/assets/docs/tree/single-component.doc.mjs +11 -2
  80. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  81. package/assets/docs.generated.json +2 -2
  82. package/assets/preview-app/next.config.mjs +7 -1
  83. package/assets/preview-app/package.json +1 -0
  84. package/assets/preview-app/src/app/globals.css +1 -0
  85. package/assets/self-audit.generated.json +1 -1
  86. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
  87. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
  88. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
  89. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
  90. package/assets/templates/components.generated.json +1 -1
  91. package/assets/templates/pages/dashboard/page.tsx +1 -1
  92. package/authoring/doctypes/_schema.d.mts +52 -5
  93. package/authoring/doctypes/_schema.mjs +62 -7
  94. package/authoring/doctypes/base/type.ts +10 -0
  95. package/authoring/doctypes/component/component.doc.mjs +1 -1
  96. package/authoring/doctypes/function/function.doc.mjs +6 -0
  97. package/authoring/doctypes/function/type.ts +3 -0
  98. package/authoring/doctypes/legacy.mjs +5 -1
  99. package/authoring/index.d.ts +1 -0
  100. package/authoring/shadcn/source-variants.d.mts +1 -1
  101. package/authoring/shadcn/source-variants.mjs +32 -10
  102. package/clients/cli/build-id.generated.json +1 -1
  103. package/clients/cli/commands/doctor.doc.mjs +2 -2
  104. package/clients/cli/commands/experimental/theme.mjs +239 -45
  105. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  106. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +2 -2
  107. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +2 -2
  108. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +3 -3
  109. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  110. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  111. package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
  112. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  113. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  114. package/clients/cli/commands/experimental-theme-template.doc.mjs +2 -2
  115. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  116. package/clients/cli/commands/theme-eject.doc.mjs +1 -1
  117. package/clients/cli/commands/theme-template.doc.mjs +3 -1
  118. package/clients/cli/commands/theme.mjs +190 -46
  119. package/clients/cli/formatters/index.mjs +7 -4
  120. package/clients/cli/lib/cli-error.mjs +9 -3
  121. package/clients/cli/lib/component-format.mjs +27 -9
  122. package/clients/cli/lib/json-shim.mjs +72 -2
  123. package/clients/cli/manifest.generated.json +1 -1
  124. package/clients/cli/run.mjs +49 -0
  125. package/foundation/agent-docs/agent-docs-target.d.mts +2 -6
  126. package/foundation/agent-docs/agent-docs-target.mjs +9 -17
  127. package/foundation/agent-docs/agent-docs.mjs +3 -2
  128. package/foundation/config/config-writer.mjs +2 -2
  129. package/foundation/config/project.mjs +60 -14
  130. package/foundation/discovery/component-discovery.mjs +5 -17
  131. package/foundation/discovery/icon-set-writer.mjs +1 -1
  132. package/foundation/discovery/module-exports.mjs +1 -15
  133. package/foundation/discovery/template-adapter.mjs +44 -19
  134. package/foundation/discovery/template-source-tree.mjs +3 -16
  135. package/foundation/discovery/theme-discovery.d.mts +5 -1
  136. package/foundation/discovery/theme-discovery.mjs +61 -43
  137. package/foundation/fs/exact-codec.d.mts +35 -0
  138. package/foundation/fs/exact-codec.mjs +148 -0
  139. package/foundation/fs/gather-cache.d.mts +15 -5
  140. package/foundation/fs/gather-cache.mjs +129 -31
  141. package/foundation/fs/paths.d.mts +39 -0
  142. package/foundation/fs/paths.mjs +44 -11
  143. package/foundation/fs/read-record.mjs +11 -1
  144. package/foundation/integrations/contribution-fixes.mjs +1 -16
  145. package/foundation/integrations/may-have-integrations.mjs +3 -2
  146. package/foundation/integrations/public-imports.mjs +1 -1
  147. package/foundation/render/embedded-imports.mjs +1 -1
  148. package/foundation/render/host-exit.d.mts +8 -0
  149. package/foundation/render/host-exit.mjs +11 -0
  150. package/foundation/render/page-host.mjs +56 -10
  151. package/foundation/render/page-worker.mjs +14 -0
  152. package/foundation/render/render-page.mjs +37 -2
  153. package/foundation/response/error-codes.d.mts +2 -1
  154. package/foundation/response/error-codes.d.ts +1 -0
  155. package/foundation/response/error-codes.doc.mjs +5 -0
  156. package/foundation/response/error-codes.mjs +3 -0
  157. package/foundation/response/json.d.mts +15 -0
  158. package/foundation/response/json.mjs +52 -1
  159. package/foundation/response/response-types.doc.mjs +1 -1
  160. package/foundation/runtime/lazy-dependencies.d.mts +14 -0
  161. package/foundation/runtime/lazy-dependencies.mjs +82 -0
  162. package/foundation/xle/browser.d.mts +2 -2
  163. package/package.json +7 -6
@@ -13,9 +13,16 @@
13
13
  * gathers fresh and stores that.
14
14
  * @position Used by search's adapter (api/search/_adapter.mjs) for the
15
15
  * catalog `search` and `build` rank, and by the project
16
- * (foundation/config/project.mjs) for each integration's contribution-check
17
- * results, which `component`, `template`, `theme`, `discover` and `search`
18
- * report. Gathers are kept only once the folder exists: the first such run
16
+ * (foundation/config/project.mjs) for its integration issues: each
17
+ * integration's contribution-check results and the whole list (with the
18
+ * template and component replacement passes), which `component`,
19
+ * `template`, `theme`, `discover` and `search` report, by template
20
+ * discovery (foundation/discovery/template-adapter.mjs) for its records, by theme discovery
21
+ * (foundation/discovery/theme-discovery.mjs) for each integration's themes,
22
+ * and by `doctor` (api/doctor/_adapter.mjs) for its file-only docs checks,
23
+ * in a run that records modules and variables to its end. The commands
24
+ * whose runs keep these entries are `search`, `build`, `component`,
25
+ * `template`, `theme`, `discover`, `docs` and `doctor`. Gathers are kept only once the folder exists: the first such run
19
26
  * on a machine just makes it, so a one-off run pays nothing to store one. Only a published CLI stores anything:
20
27
  * its build id (clients/cli/build-id.generated.json, written at `prepack`)
21
28
  * names its exact code, so a checkout, whose code may change between runs
@@ -49,14 +56,17 @@ import {
49
56
  } from './read-record.mjs';
50
57
 
51
58
  /**
52
- * The kept commands whose only kept gather is their integrations' check
53
- * results: a project with no integration records none of them.
59
+ * The kept commands whose kept gathers are worth recording only where an
60
+ * integration can load (its check results, and template discovery, whose
61
+ * records hold Core's templates too but which costs little without one): a
62
+ * project with no integration records none of them.
54
63
  */
55
64
  const INTEGRATION_COMMANDS = new Set([
56
65
  'component',
57
66
  'template',
58
67
  'theme',
59
68
  'discover',
69
+ 'docs',
60
70
  ]);
61
71
 
62
72
  /** The run's commands whose gathers are kept. */
@@ -67,6 +77,9 @@ const KEPT_COMMANDS = new Set([
67
77
  'template',
68
78
  'theme',
69
79
  'discover',
80
+ 'docs',
81
+ // The project check alone (no subcommand): its docs checks are kept.
82
+ 'doctor',
70
83
  ]);
71
84
 
72
85
  /** Where `prepack` writes the id of the package's exact contents. */
@@ -91,13 +104,16 @@ const UNSET = null;
91
104
  * their states, modules it loaded with theirs, and environment variables with
92
105
  * their values. Files and folders are recorded for the whole run; modules and
93
106
  * variables only while the run is armed: from its start to the end of its
94
- * first gather, and during each later gather. Null outside a recorded run.
95
- * @type {{reads: Map<string, string>, env: Map<string, string | null>, arm: () => boolean, disarm: () => void} | null}
107
+ * first gather, and during each later gather, or, in a run armed throughout
108
+ * (`doctor`), to its end. Null outside a recorded run.
109
+ * @type {{reads: Map<string, string>, env: Map<string, string | null>, arm: () => boolean, disarm: () => void, armedThroughout: boolean, realEnv: NodeJS.ProcessEnv} | null}
96
110
  */
97
111
  let run = null;
98
112
 
99
113
  /**
100
- * The running recorded run's reads and variables, for tests: null outside one.
114
+ * The running recorded run's reads and variables, or null outside one: for
115
+ * tests, and for a caller that keeps a value only in a recorded run (doctor's
116
+ * kept checks).
101
117
  * @returns {{reads: Map<string, string>, env: Map<string, string | null>} | null}
102
118
  */
103
119
  export function recordedRun() {
@@ -225,21 +241,28 @@ export async function recordRunFor(argv, fn) {
225
241
  if (
226
242
  !command ||
227
243
  !KEPT_COMMANDS.has(command) ||
244
+ (command === 'doctor' &&
245
+ argv.filter(arg => !arg.startsWith('-')).length > 1) ||
228
246
  runsUnderPnp() ||
229
247
  currentUid() == null ||
230
248
  withoutRecording(buildId) == null ||
231
- // Last, being a few file checks: those commands keep only their
232
- // integrations' check results, so a project with none records nothing.
249
+ // Last, being a few file checks: those commands keep what is worth
250
+ // keeping only where an integration can load, so a project with none
251
+ // records nothing.
233
252
  (INTEGRATION_COMMANDS.has(command) &&
234
253
  !withoutRecording(() => mayHaveIntegrations(process.cwd())))
235
254
  ) {
236
255
  return fn();
237
256
  }
238
257
  // Gathers are kept only once the folder exists: the first recorded run on a
239
- // machine (search, build, component, template, theme or discover) just makes
258
+ // machine (search, build, component, template, theme, discover or docs) just makes
240
259
  // it, so a one-off run never pays to store one.
241
260
  const dir = cacheDirPath();
242
261
  if (!withoutRecording(() => fs.existsSync(dir))) {
262
+ // Once the per-user cache folder exists doctor uses it; it never makes
263
+ // it, so a doctor alone changes nothing on the machine (nor its own
264
+ // cache row).
265
+ if (command === 'doctor') return fn();
243
266
  try {
244
267
  withoutRecording(() => fs.mkdirSync(dir, {recursive: true, mode: 0o700}));
245
268
  } catch {
@@ -250,7 +273,10 @@ export async function recordRunFor(argv, fn) {
250
273
  // A folder the cache would refuse (not the user's own, open to others, a
251
274
  // link) leaves the run exactly as without the cache: no hook is installed.
252
275
  if (withoutRecording(gatherCacheDir) == null) return fn();
253
- return recordRun(fn);
276
+ // doctor's kept checks read docs modules that load between its gathers, so
277
+ // its run records modules and variables to the end, not only during each
278
+ // gather.
279
+ return recordRun(fn, {armedThroughout: command === 'doctor'});
254
280
  }
255
281
 
256
282
  /**
@@ -258,9 +284,12 @@ export async function recordRunFor(argv, fn) {
258
284
  * tests; `recordRunFor` decides which runs are recorded.
259
285
  * @template T
260
286
  * @param {() => Promise<T>} fn
287
+ * @param {{armedThroughout?: boolean}} [options] - `armedThroughout`: record
288
+ * modules and variables until the run ends, not only until each gather
289
+ * ends, for a run whose later gathers read modules loaded in between.
261
290
  * @returns {Promise<T>}
262
291
  */
263
- export async function recordRun(fn) {
292
+ export async function recordRun(fn, {armedThroughout = false} = {}) {
264
293
  const registerHooks = /** @type {any} */ (nodeModule).registerHooks;
265
294
  if (typeof registerHooks !== 'function' || run != null) return fn();
266
295
  /** @type {Map<string, string>} */
@@ -276,17 +305,31 @@ export async function recordRun(fn) {
276
305
  env.set(name, envState(realEnv[name]));
277
306
  }
278
307
  };
308
+ // Asking whether a variable is set, as `in` does and as `Object.keys` does
309
+ // for every name it lists, reads no value: only whether it is set counts.
310
+ // Every read of a value goes through `get` (spread, Object.entries,
311
+ // Object.assign, JSON.stringify included). Known gap: a descriptor's
312
+ // `value` (Object.getOwnPropertyDescriptor(s) on process.env) is a value
313
+ // read with no `get`, so it counts only as set. Nothing the CLI, Core, its
314
+ // dependencies, jiti's bundled babel or the integrations we test reads the
315
+ // environment that way.
316
+ /** @param {string | symbol} name */
317
+ const notePresence = name => {
318
+ if (typeof name === 'string' && !env.has(PRESENCE + name)) {
319
+ env.set(PRESENCE + name, presenceState(realEnv, name));
320
+ }
321
+ };
279
322
  const proxy = new Proxy(realEnv, {
280
323
  get(target, name) {
281
324
  noteEnv(name);
282
325
  return Reflect.get(target, name);
283
326
  },
284
327
  has(target, name) {
285
- noteEnv(name);
328
+ notePresence(name);
286
329
  return Reflect.has(target, name);
287
330
  },
288
331
  getOwnPropertyDescriptor(target, name) {
289
- noteEnv(name);
332
+ notePresence(name);
290
333
  return Reflect.getOwnPropertyDescriptor(target, name);
291
334
  },
292
335
  ownKeys(target) {
@@ -346,7 +389,7 @@ export async function recordRun(fn) {
346
389
  }
347
390
  };
348
391
  if (!arm()) return fn();
349
- run = {reads, env, arm, disarm};
392
+ run = {reads, env, arm, disarm, armedThroughout, realEnv};
350
393
  try {
351
394
  return await recordReads(reads, fn, {exact: true});
352
395
  } finally {
@@ -407,6 +450,19 @@ function lookupStamp(folder) {
407
450
  }
408
451
  }
409
452
 
453
+ /** The stamp name prefix for whether a variable is set, not its value. */
454
+ const PRESENCE = '\0set:';
455
+
456
+ /**
457
+ * Whether `name` is set in `env`, as stored.
458
+ * @param {NodeJS.ProcessEnv} env
459
+ * @param {string} name
460
+ * @returns {string | null}
461
+ */
462
+ function presenceState(env, name) {
463
+ return typeof env[name] === 'string' ? 'set' : UNSET;
464
+ }
465
+
410
466
  /**
411
467
  * How an environment variable the run read is kept: a digest of its value,
412
468
  * never the value (it can be a token), or UNSET when it was not set.
@@ -471,10 +527,13 @@ function entryKey(key, id) {
471
527
  * @returns {string | null} why not, or null when all hold
472
528
  */
473
529
  function staleReason(stamps, env) {
530
+ // The environment as it is, never through the run's recording proxy:
531
+ // checking an entry is no read the run's own gathers depend on.
532
+ const real = run?.realEnv ?? process.env;
474
533
  for (const [name, value] of env) {
475
- const current = envState(
476
- name === '\0names' ? namesOf(process.env) : process.env[name],
477
- );
534
+ const current = name.startsWith(PRESENCE)
535
+ ? presenceState(real, name.slice(PRESENCE.length))
536
+ : envState(name === '\0names' ? namesOf(real) : real[name]);
478
537
  if (current !== value) {
479
538
  return name === '\0names'
480
539
  ? 'the environment has other variables'
@@ -494,8 +553,9 @@ function staleReason(stamps, env) {
494
553
  * @param {unknown[]} key - What else decides the gather: its folder (a real
495
554
  * path) and options.
496
555
  * @param {() => Promise<T>} load
497
- * @param {{encode: (value: T) => E, decode: (stored: E) => T}} codec - A
498
- * plain-JSON form of the value and back.
556
+ * @param {{encode: (value: T) => E, decode: (stored: E) => T, valid?: (value: T) => boolean}} codec - A
557
+ * plain-JSON form of the value and back, and optionally the shape a decoded
558
+ * value must have to be used (anything else is a miss).
499
559
  * @param {(value: T) => boolean} [keep] whether a fresh gather may be
500
560
  * stored: one that depends on something its stamps cannot see (a retry
501
561
  * that waits on time) is used for this run only
@@ -509,7 +569,7 @@ export async function persistedGather(key, load, codec, keep = () => true) {
509
569
  try {
510
570
  return await gatherAt(key, load, codec, keep);
511
571
  } finally {
512
- if (--gatherDepth === 0) run?.disarm();
572
+ if (--gatherDepth === 0 && !run?.armedThroughout) run?.disarm();
513
573
  }
514
574
  }
515
575
 
@@ -519,7 +579,7 @@ export async function persistedGather(key, load, codec, keep = () => true) {
519
579
  * @template T, E
520
580
  * @param {unknown[]} key
521
581
  * @param {() => Promise<T>} load
522
- * @param {{encode: (value: T) => E, decode: (stored: E) => T}} codec
582
+ * @param {{encode: (value: T) => E, decode: (stored: E) => T, valid?: (value: T) => boolean}} codec
523
583
  * @param {(value: T) => boolean} keep
524
584
  * @returns {Promise<T>}
525
585
  */
@@ -553,9 +613,20 @@ async function gatherAt(key, load, codec, keep) {
553
613
  reason = 'an entry for other inputs';
554
614
  } else {
555
615
  const stale = staleReason(stored.stamps, stored.env);
556
- if (stale == null) {
557
- const value = codec.decode(stored.value);
616
+ const value = stale == null ? readBack(stored, codec) : undefined;
617
+ if (stale == null && value === undefined) {
618
+ // Gathered again below, and stored over the bad entry.
619
+ reason = 'a malformed value';
620
+ } else if (stale == null) {
558
621
  recordStates(stored.stamps);
622
+ // A gather around this one depends on what this one read, the
623
+ // environment included: a hit hands its variables on, as a fresh
624
+ // gather's reads would be.
625
+ if (run) {
626
+ for (const [name, value] of stored.env) {
627
+ if (!run.env.has(name)) run.env.set(name, value);
628
+ }
629
+ }
559
630
  gatherCode = cliStates(stored.stamps);
560
631
  lastOutcome = {hit: true, reason: 'unchanged'};
561
632
  try {
@@ -565,8 +636,9 @@ async function gatherAt(key, load, codec, keep) {
565
636
  // An entry that cannot be touched is evicted sooner; nothing more.
566
637
  }
567
638
  return value;
639
+ } else {
640
+ reason = stale;
568
641
  }
569
- reason = stale;
570
642
  }
571
643
  } catch (error) {
572
644
  if (/** @type {any} */ (error)?.code !== 'ENOENT')
@@ -624,7 +696,7 @@ function codeStamp(code, inputs) {
624
696
  * @template T, E
625
697
  * @param {unknown[]} key - Everything the value is computed from.
626
698
  * @param {() => T} compute
627
- * @param {{encode: (value: T) => E, decode: (stored: E) => T}} codec
699
+ * @param {{encode: (value: T) => E, decode: (stored: E) => T, valid?: (value: T) => boolean}} codec
628
700
  * @param {string[]} [inputs] - Files the computation reads, by path.
629
701
  * @returns {T}
630
702
  */
@@ -640,12 +712,13 @@ export function persistedDerived(key, compute, codec, inputs = []) {
640
712
  const stored = JSON.parse(
641
713
  withoutRecording(() => fs.readFileSync(file, 'utf-8')),
642
714
  );
643
- if (
715
+ const value =
644
716
  stored?.format === FORMAT &&
645
717
  stored.derived === true &&
646
718
  stored.key === entry
647
- ) {
648
- const value = codec.decode(stored.value);
719
+ ? readBack(stored, codec)
720
+ : undefined;
721
+ if (value !== undefined) {
649
722
  try {
650
723
  const now = new Date();
651
724
  withoutRecording(() => fs.utimesSync(file, now, now));
@@ -679,6 +752,25 @@ export function persistedDerived(key, compute, codec, inputs = []) {
679
752
  return value;
680
753
  }
681
754
 
755
+ /**
756
+ * A stored entry's value, decoded, or undefined when it has none, its decode
757
+ * throws, or the decoded value fails the codec's shape check (`valid`): such
758
+ * an entry is a miss, worked out again and stored over.
759
+ * @param {any} stored
760
+ * @param {{decode: (stored: any) => any, valid?: (value: any) => boolean}} codec
761
+ * @returns {any}
762
+ */
763
+ function readBack(stored, codec) {
764
+ if (!Object.hasOwn(stored, 'value') || stored.value == null) return undefined;
765
+ try {
766
+ const value = codec.decode(stored.value);
767
+ if (value == null) return undefined;
768
+ return !codec.valid || codec.valid(value) ? value : undefined;
769
+ } catch {
770
+ return undefined;
771
+ }
772
+ }
773
+
682
774
  /**
683
775
  * Store a fresh gather with what the run read, unless its plain form does not
684
776
  * read back as the same value. Silent on any failure.
@@ -687,7 +779,7 @@ export function persistedDerived(key, compute, codec, inputs = []) {
687
779
  * @param {string} file
688
780
  * @param {string} entry - The entry's key (entryKey).
689
781
  * @param {T} value
690
- * @param {{encode: (value: T) => E, decode: (stored: E) => T}} codec
782
+ * @param {{encode: (value: T) => E, decode: (stored: E) => T, valid?: (value: T) => boolean}} codec
691
783
  */
692
784
  function store(dir, file, entry, value, codec) {
693
785
  if (!run) return;
@@ -761,6 +853,12 @@ export function runsUnderPnp() {
761
853
  * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp)
762
854
  */
763
855
  export function gatherCacheSummary() {
856
+ // The cache's own bookkeeping, never an input of what it keeps.
857
+ return withoutRecording(summarize);
858
+ }
859
+
860
+ /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null} */
861
+ function summarize() {
764
862
  if (buildId() == null) return null;
765
863
  const dir = gatherCacheDir();
766
864
  if (dir == null) return null;
@@ -60,6 +60,20 @@ export function findCarriedCoreDir(startDir: string, { fetch }?: {
60
60
  * @returns {string | null}
61
61
  */
62
62
  export function findProjectCoreDir(startDir?: string): string | null;
63
+ /**
64
+ * `p` resolved through symlinks when it exists, so folders compare equal
65
+ * however they were reached.
66
+ *
67
+ * @param {string} p
68
+ * @returns {string}
69
+ */
70
+ export function realDir(p: string): string;
71
+ /**
72
+ * The home folder, resolved through symlinks, or null when there is none.
73
+ *
74
+ * @returns {string|null}
75
+ */
76
+ export function homeFolder(): string | null;
63
77
  /**
64
78
  * The project root: the directory of the nearest package.json at or above
65
79
  * `startDir`, or null when there is none.
@@ -68,6 +82,22 @@ export function findProjectCoreDir(startDir?: string): string | null;
68
82
  * @returns {string|null}
69
83
  */
70
84
  export function findPackageRoot(startDir: string): string | null;
85
+ /**
86
+ * A path a command writes in a project, read the same way by every theme
87
+ * command: from the working folder when given, from the project root (the
88
+ * nearest folder with a package.json, else the working folder) when not,
89
+ * and with a backslash as a folder separator on every platform.
90
+ * @param {string} cwd
91
+ * @param {string | undefined} typed the path as given, or undefined
92
+ * @param {string} fallback the path from the project root when none is given
93
+ * @returns {{projectDir: string, typed: string | undefined, relative: string}}
94
+ * `typed`: the path as given with `/`; `relative`: from the project root
95
+ */
96
+ export function projectWritePath(cwd: string, typed: string | undefined, fallback: string): {
97
+ projectDir: string;
98
+ typed: string | undefined;
99
+ relative: string;
100
+ };
71
101
  /**
72
102
  * The Astryx project `startDir` belongs to: the nearest folder at or above it
73
103
  * whose package.json declares an Astryx package (`@astryxdesign/*`, or the
@@ -89,6 +119,15 @@ export function findAstryxProject(startDir: string): string | null;
89
119
  * @returns {string}
90
120
  */
91
121
  export function findWorkspaceRoot(startDir: string): string;
122
+ /**
123
+ * The nearest directory at or above `startDir` that declares a workspace,
124
+ * by a `pnpm-workspace.yaml` or a package.json `workspaces` field; null
125
+ * outside a workspace.
126
+ *
127
+ * @param {string} startDir
128
+ * @returns {string|null}
129
+ */
130
+ export function findDeclaredWorkspaceRoot(startDir: string): string | null;
92
131
  /**
93
132
  * The nearest `node_modules/<name>` at or above `startDir`, found the way
94
133
  * Node's resolver finds a package, and the folder whose node_modules holds it.
@@ -269,7 +269,7 @@ function isCoreSource(dir) {
269
269
  * @param {string} p
270
270
  * @returns {string}
271
271
  */
272
- function realDir(p) {
272
+ export function realDir(p) {
273
273
  try {
274
274
  return fs.realpathSync(path.resolve(p));
275
275
  } catch {
@@ -282,7 +282,7 @@ function realDir(p) {
282
282
  *
283
283
  * @returns {string|null}
284
284
  */
285
- function homeFolder() {
285
+ export function homeFolder() {
286
286
  try {
287
287
  const home = os.homedir();
288
288
  return home ? realDir(home) : null;
@@ -339,6 +339,31 @@ export function findPackageRoot(startDir) {
339
339
  return null;
340
340
  }
341
341
 
342
+ /**
343
+ * A path a command writes in a project, read the same way by every theme
344
+ * command: from the working folder when given, from the project root (the
345
+ * nearest folder with a package.json, else the working folder) when not,
346
+ * and with a backslash as a folder separator on every platform.
347
+ * @param {string} cwd
348
+ * @param {string | undefined} typed the path as given, or undefined
349
+ * @param {string} fallback the path from the project root when none is given
350
+ * @returns {{projectDir: string, typed: string | undefined, relative: string}}
351
+ * `typed`: the path as given with `/`; `relative`: from the project root
352
+ */
353
+ export function projectWritePath(cwd, typed, fallback) {
354
+ const projectDir = findPackageRoot(cwd) ?? path.resolve(cwd);
355
+ const given = typed == null ? undefined : String(typed).replace(/\\/g, '/');
356
+ const absolute =
357
+ given == null ? path.join(projectDir, fallback) : path.resolve(cwd, given);
358
+ return {
359
+ projectDir,
360
+ typed: given,
361
+ relative: path.isAbsolute(given ?? '')
362
+ ? /** @type {string} */ (given)
363
+ : path.relative(projectDir, absolute) || '.',
364
+ };
365
+ }
366
+
342
367
  /**
343
368
  * The Astryx project `startDir` belongs to: the nearest folder at or above it
344
369
  * whose package.json declares an Astryx package (`@astryxdesign/*`, or the
@@ -374,8 +399,22 @@ export function findAstryxProject(startDir) {
374
399
  * @returns {string}
375
400
  */
376
401
  export function findWorkspaceRoot(startDir) {
377
- /** @type {string|null} */
378
- let nearestPackage = null;
402
+ return (
403
+ findDeclaredWorkspaceRoot(startDir) ??
404
+ findPackageRoot(startDir) ??
405
+ path.resolve(startDir)
406
+ );
407
+ }
408
+
409
+ /**
410
+ * The nearest directory at or above `startDir` that declares a workspace,
411
+ * by a `pnpm-workspace.yaml` or a package.json `workspaces` field; null
412
+ * outside a workspace.
413
+ *
414
+ * @param {string} startDir
415
+ * @returns {string|null}
416
+ */
417
+ export function findDeclaredWorkspaceRoot(startDir) {
379
418
  for (const dir of foldersUpFrom(startDir)) {
380
419
  if (
381
420
  fs.existsSync(path.join(dir, 'pnpm-workspace.yaml')) ||
@@ -383,14 +422,8 @@ export function findWorkspaceRoot(startDir) {
383
422
  ) {
384
423
  return dir;
385
424
  }
386
- if (
387
- nearestPackage == null &&
388
- fs.existsSync(path.join(dir, 'package.json'))
389
- ) {
390
- nearestPackage = dir;
391
- }
392
425
  }
393
- return nearestPackage ?? path.resolve(startDir);
426
+ return null;
394
427
  }
395
428
 
396
429
  /**
@@ -233,7 +233,17 @@ export function recordStates(states) {
233
233
  const runs = reads.getStore();
234
234
  if (!runs) return;
235
235
  for (const [file, state] of states) {
236
- for (const seen of runs) if (!seen.has(file)) seen.set(file, state);
236
+ for (const seen of runs) {
237
+ const had = seen.get(file);
238
+ // A folder this run only probed, which the kept read listed: the
239
+ // listing counts, as when this run lists it itself (see `note`).
240
+ if (
241
+ had === undefined ||
242
+ (had.startsWith('dir:') && !state.startsWith('dir:'))
243
+ ) {
244
+ seen.set(file, state);
245
+ }
246
+ }
237
247
  }
238
248
  }
239
249
 
@@ -20,7 +20,6 @@
20
20
 
21
21
  import * as fs from 'node:fs';
22
22
  import * as path from 'node:path';
23
- import {createRequire} from 'node:module';
24
23
  import {isValidSemver} from '../env/semver.mjs';
25
24
  import {
26
25
  isThemeFolder as readsAsTheme,
@@ -28,6 +27,7 @@ import {
28
27
  THEME_MODULE_EXTENSIONS,
29
28
  THEME_SLUG_RE,
30
29
  } from '../discovery/theme-discovery.mjs';
30
+ import {jscodeshift} from '../runtime/lazy-dependencies.mjs';
31
31
 
32
32
  /** @typedef {'components' | 'templates' | 'docs' | 'themes'} MetadataRoot */
33
33
 
@@ -88,24 +88,9 @@ const ROOT_HOLDS = /** @type {Record<MetadataRoot, string>} */ ({
88
88
  themes: 'themes',
89
89
  });
90
90
 
91
- const require = createRequire(import.meta.url);
92
- /** @type {typeof import('jscodeshift').default | undefined} */
93
- let jscodeshiftModule;
94
91
  /** @type {ReturnType<typeof import('jscodeshift').default['withParser']> | undefined} */
95
92
  let tsxParser;
96
93
 
97
- /**
98
- * jscodeshift, loaded at its first use: it is a large module, and a run that
99
- * checks no contribution file never needs it.
100
- * @returns {typeof import('jscodeshift').default}
101
- */
102
- function jscodeshift() {
103
- jscodeshiftModule ??= require('jscodeshift');
104
- return /** @type {typeof import('jscodeshift').default} */ (
105
- jscodeshiftModule
106
- );
107
- }
108
-
109
94
  /** jscodeshift with the TypeScript parser, made once. */
110
95
  function tsx() {
111
96
  tsxParser ??= jscodeshift().withParser('tsx');
@@ -12,8 +12,9 @@
12
12
  * so no configured, self-resolved, autolinked or computer-level integration
13
13
  * can load.
14
14
  * @position Read by foundation/fs/gather-cache.mjs: `component`, `template`,
15
- * `theme` and `discover` record a run only for their integrations' kept
16
- * check results, so a project with none skips the recorder entirely. A true
15
+ * `theme`, `discover` and `docs` record a run only for what is worth keeping where
16
+ * an integration can load (its kept check results, and template
17
+ * discovery's records), so a project with none skips the recorder entirely. A true
17
18
  * answer only means the run records, so a check that says true too often
18
19
  * costs time, never an answer.
19
20
  */
@@ -30,7 +30,7 @@
30
30
 
31
31
  import * as fs from 'node:fs';
32
32
  import * as path from 'node:path';
33
- import {parse} from '@babel/parser';
33
+ import {babelParse as parse} from '../runtime/lazy-dependencies.mjs';
34
34
  import {findProjectCoreDir} from '../fs/paths.mjs';
35
35
  import {normalizeProviderId} from '../identity/provider-identity.mjs';
36
36
  import {
@@ -15,7 +15,7 @@
15
15
 
16
16
  import * as fs from 'node:fs';
17
17
  import * as path from 'node:path';
18
- import {parse} from '@babel/parser';
18
+ import {babelParse as parse} from '../runtime/lazy-dependencies.mjs';
19
19
  import {PAGE_EXTENSIONS} from './embedded-files.mjs';
20
20
  import postcss from 'postcss';
21
21
  import {matchPath, readPathsIn} from './ts-paths.mjs';
@@ -0,0 +1,8 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ export namespace HOST_EXIT {
5
+ let disconnected: number;
6
+ let timeout: number;
7
+ let noChannel: number;
8
+ }
@@ -0,0 +1,11 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The exit codes of the render's host process (page-host.mjs) for each
5
+ * way it ends without sending an answer. render-page.mjs reads them to say
6
+ * why.
7
+ * @position foundation/render — imported by page-host.mjs and render-page.mjs
8
+ */
9
+
10
+ /** Exit codes for each way the host ends without sending an answer. */
11
+ export const HOST_EXIT = {disconnected: 70, timeout: 71, noChannel: 72};