@astryxdesign/cli 0.7.16 → 0.7.17-canary.gb36275e

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 (239) hide show
  1. package/README.md +13 -23
  2. package/api/build/_adapter.d.mts +5 -0
  3. package/api/build/_adapter.mjs +49 -34
  4. package/api/build/blocks/blocks.d.mts +7 -5
  5. package/api/build/blocks/blocks.mjs +113 -26
  6. package/api/build/build.doc.mjs +2 -1
  7. package/api/build/kit/rank.mjs +59 -12
  8. package/api/build/kit/weights.d.mts +16 -3
  9. package/api/build/kit/weights.json +1 -1
  10. package/api/build/kit/weights.mjs +238 -31
  11. package/api/build/kit/word-model.d.mts +23 -0
  12. package/api/build/kit/word-model.mjs +46 -0
  13. package/api/build/kit/words.d.mts +42 -0
  14. package/api/build/kit/words.mjs +82 -1
  15. package/api/component/__fixtures__/resolutions/modules.txt +4 -5
  16. package/api/component/list/list.d.mts +1 -1
  17. package/api/component/list/list.mjs +46 -5
  18. package/api/discover/_adapter.d.mts +1 -1
  19. package/api/docs/_adapter.d.mts +26 -2
  20. package/api/docs/_adapter.mjs +217 -5
  21. package/api/docs/docs.d.mts +3 -0
  22. package/api/docs/docs.doc.mjs +7 -1
  23. package/api/docs/docs.mjs +12 -0
  24. package/api/docs/docs.type.d.mts +5 -0
  25. package/api/docs/docs.type.mjs +2 -0
  26. package/api/docs/node/node.mjs +1 -1
  27. package/api/doctor/_adapter.d.mts +27 -5
  28. package/api/doctor/_adapter.mjs +77 -6
  29. package/api/doctor/built-checks.mjs +38 -13
  30. package/api/doctor/doctor.d.mts +4 -3
  31. package/api/doctor/doctor.mjs +14 -7
  32. package/api/doctor/theme-checks.mjs +121 -17
  33. package/api/render/_adapter.mjs +531 -192
  34. package/api/render/render.doc.mjs +11 -3
  35. package/api/render/render.type.d.mts +30 -0
  36. package/api/render/render.type.mjs +19 -0
  37. package/api/search/ORDERING.md +1 -1
  38. package/api/search/_candidate-score.mjs +51 -32
  39. package/api/search/_fit-order.mjs +3 -3
  40. package/api/search/_result.d.mts +5 -5
  41. package/api/search/_score.d.mts +6 -1
  42. package/api/search/_score.mjs +15 -3
  43. package/api/search/search.d.mts +1 -1
  44. package/api/theme/_adapter.mjs +10 -0
  45. package/api/theme/build/build.d.mts +47 -0
  46. package/api/theme/build/build.mjs +147 -25
  47. package/api/theme/theme.type.d.mts +10 -0
  48. package/api/theme/theme.type.mjs +7 -1
  49. package/api/theme-edit/_adapter.d.mts +48 -0
  50. package/api/theme-edit/_adapter.mjs +675 -42
  51. package/api/theme-edit/_check.d.mts +28 -22
  52. package/api/theme-edit/_check.mjs +1481 -252
  53. package/api/theme-edit/_color.d.mts +9 -0
  54. package/api/theme-edit/_color.mjs +41 -1
  55. package/api/theme-edit/_compiler.mjs +9 -0
  56. package/api/theme-edit/_context.mjs +60 -3
  57. package/api/theme-edit/_css.mjs +21 -1
  58. package/api/theme-edit/_edit.d.mts +22 -0
  59. package/api/theme-edit/_edit.mjs +1196 -88
  60. package/api/theme-edit/_how-commands.d.mts +57 -0
  61. package/api/theme-edit/_how-commands.mjs +536 -1
  62. package/api/theme-edit/_how-question.d.mts +65 -0
  63. package/api/theme-edit/_how-question.mjs +4401 -225
  64. package/api/theme-edit/_how.mjs +104 -4
  65. package/api/theme-edit/_marks.d.mts +31 -5
  66. package/api/theme-edit/_marks.mjs +57 -5
  67. package/api/theme-edit/_normal.d.mts +153 -0
  68. package/api/theme-edit/_normal.mjs +661 -0
  69. package/api/theme-edit/_palette.mjs +10 -4
  70. package/api/theme-edit/_parts.d.mts +8 -0
  71. package/api/theme-edit/_parts.mjs +103 -2
  72. package/api/theme-edit/_preview-app.d.mts +7 -1
  73. package/api/theme-edit/_preview-app.mjs +205 -17
  74. package/api/theme-edit/_preview.d.mts +64 -8
  75. package/api/theme-edit/_preview.mjs +447 -89
  76. package/api/theme-edit/_settings.d.mts +176 -5
  77. package/api/theme-edit/_settings.mjs +1936 -128
  78. package/api/theme-edit/_tables.d.mts +6 -0
  79. package/api/theme-edit/_tables.mjs +32 -1
  80. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  81. package/api/theme-edit/theme-edit.type.mjs +4 -1
  82. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  83. package/assets/codemods/run-codemod.mjs +73 -6
  84. package/assets/codemods/transforms/next/index.mjs +11 -1
  85. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  86. package/assets/docs/author-a-theme.doc.mjs +1 -0
  87. package/assets/docs/theme.doc.dense.mjs +2 -2
  88. package/assets/docs/theme.doc.mjs +1 -1
  89. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  90. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  91. package/assets/docs/tree/render-pages.doc.mjs +43 -2
  92. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  93. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  94. package/assets/docs/use-a-theme.doc.mjs +1 -1
  95. package/assets/docs.generated.json +2 -2
  96. package/assets/self-audit.generated.json +1 -1
  97. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  98. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  99. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  100. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  101. package/assets/templates/components.generated.json +1 -1
  102. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  103. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  104. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  105. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  106. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  107. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  108. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  109. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  110. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  111. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  112. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  113. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  114. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  115. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  116. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  117. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  118. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  119. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  120. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  121. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  122. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  123. package/assets/templates/pages/ide/page.tsx +24 -2
  124. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  125. package/assets/templates/pages/library/template.doc.mjs +4 -1
  126. package/assets/templates/pages/login/template.doc.mjs +2 -1
  127. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  128. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  129. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  130. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  131. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  132. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  133. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  134. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  135. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  136. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  137. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  138. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  139. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  140. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  141. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  142. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  143. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  144. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  145. package/authoring/debug/parse.d.mts +3 -3
  146. package/authoring/doctypes/_schema.d.mts +3 -3
  147. package/authoring/doctypes/command/command.doc.mjs +1 -1
  148. package/authoring/doctypes/template/template.doc.mjs +1 -1
  149. package/authoring/doctypes/template/type.ts +9 -0
  150. package/clients/cli/bin/astryx.mjs +6 -9
  151. package/clients/cli/build-id.generated.json +1 -1
  152. package/clients/cli/commands/component.doc.mjs +1 -1
  153. package/clients/cli/commands/component.mjs +20 -2
  154. package/clients/cli/commands/docs.doc.mjs +7 -1
  155. package/clients/cli/commands/docs.mjs +3 -1
  156. package/clients/cli/commands/doctor.mjs +22 -14
  157. package/clients/cli/commands/experimental/render.mjs +32 -1
  158. package/clients/cli/commands/experimental/theme.mjs +65 -14
  159. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  160. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  161. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  162. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  163. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  164. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  165. package/clients/cli/commands/search.doc.mjs +2 -1
  166. package/clients/cli/commands/search.mjs +6 -1
  167. package/clients/cli/commands/template.mjs +4 -2
  168. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  169. package/clients/cli/commands/theme.doc.mjs +1 -1
  170. package/clients/cli/commands/theme.mjs +153 -5
  171. package/clients/cli/commands/upgrade.mjs +42 -7
  172. package/clients/cli/index.mjs +1 -1
  173. package/clients/cli/lib/component-format.mjs +205 -98
  174. package/clients/cli/lib/hook-format.mjs +9 -19
  175. package/clients/cli/manifest.generated.json +1 -1
  176. package/foundation/agent-docs/agent-docs.mjs +1 -1
  177. package/foundation/config/theme-state.d.mts +13 -1
  178. package/foundation/config/theme-state.mjs +47 -7
  179. package/foundation/discovery/component-discovery.d.mts +11 -0
  180. package/foundation/discovery/component-discovery.mjs +27 -2
  181. package/foundation/discovery/component-replacement.mjs +130 -1
  182. package/foundation/discovery/entry-name-files.d.mts +43 -0
  183. package/foundation/discovery/entry-name-files.mjs +264 -0
  184. package/foundation/discovery/hook-discovery.d.mts +8 -2
  185. package/foundation/discovery/hook-discovery.mjs +20 -7
  186. package/foundation/discovery/package-exports.d.mts +10 -0
  187. package/foundation/discovery/package-exports.mjs +56 -3
  188. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  189. package/foundation/discovery/runner-allowlist.mjs +2 -0
  190. package/foundation/discovery/template-adapter.d.mts +52 -0
  191. package/foundation/discovery/template-adapter.mjs +173 -10
  192. package/foundation/discovery/theme-imports.d.mts +14 -0
  193. package/foundation/discovery/theme-imports.mjs +43 -1
  194. package/foundation/doc-compiler/import.mjs +7 -1
  195. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  196. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  197. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  198. package/foundation/fs/cache-eviction.d.mts +21 -0
  199. package/foundation/fs/cache-eviction.mjs +73 -0
  200. package/foundation/fs/gather-cache.d.mts +25 -4
  201. package/foundation/fs/gather-cache.mjs +163 -53
  202. package/foundation/fs/jiti-cache.d.mts +19 -1
  203. package/foundation/fs/jiti-cache.mjs +130 -6
  204. package/foundation/fs/kept-tree.d.mts +60 -0
  205. package/foundation/fs/kept-tree.mjs +154 -0
  206. package/foundation/fs/module-loader.d.mts +18 -2
  207. package/foundation/fs/module-loader.mjs +92 -20
  208. package/foundation/identity/provider-identity.d.mts +6 -0
  209. package/foundation/identity/provider-identity.mjs +10 -1
  210. package/foundation/render/embedded-imports.d.mts +5 -4
  211. package/foundation/render/embedded-imports.mjs +7 -16
  212. package/foundation/render/file-header.d.mts +66 -0
  213. package/foundation/render/file-header.mjs +118 -0
  214. package/foundation/render/host-answer.d.mts +18 -0
  215. package/foundation/render/host-answer.mjs +46 -0
  216. package/foundation/render/host-exit.d.mts +1 -0
  217. package/foundation/render/host-exit.mjs +6 -1
  218. package/foundation/render/html-reader.d.mts +55 -2
  219. package/foundation/render/html-reader.mjs +145 -26
  220. package/foundation/render/markup-edits.d.mts +57 -0
  221. package/foundation/render/markup-edits.mjs +182 -0
  222. package/foundation/render/page-host.mjs +29 -12
  223. package/foundation/render/page-worker.mjs +83 -13
  224. package/foundation/render/render-page.d.mts +34 -2
  225. package/foundation/render/render-page.mjs +210 -29
  226. package/foundation/render/source-block.d.mts +73 -6
  227. package/foundation/render/source-block.mjs +170 -19
  228. package/foundation/render/ts-paths.d.mts +53 -6
  229. package/foundation/render/ts-paths.mjs +286 -41
  230. package/foundation/response/error-codes.d.mts +3 -1
  231. package/foundation/response/error-codes.d.ts +1 -0
  232. package/foundation/response/error-codes.doc.mjs +12 -1
  233. package/foundation/response/error-codes.mjs +10 -0
  234. package/foundation/text/string-utils.d.mts +3 -2
  235. package/foundation/text/string-utils.mjs +16 -3
  236. package/foundation/text/suggest-order.d.mts +9 -0
  237. package/foundation/text/suggest-order.mjs +13 -0
  238. package/host/index.mjs +8 -6
  239. package/package.json +6 -6
@@ -17,13 +17,23 @@
17
17
  * integration's contribution-check results and the whole list (with the
18
18
  * template and component replacement passes), which `component`,
19
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
26
- * on a machine just makes it, so a one-off run pays nothing to store one. Only a published CLI stores anything:
20
+ * discovery (foundation/discovery/template-adapter.mjs) for its records, by
21
+ * the component list (api/component/list/list.mjs) and component
22
+ * replacement (foundation/discovery/component-replacement.mjs), by theme
23
+ * discovery (foundation/discovery/theme-discovery.mjs) for each
24
+ * integration's themes,
25
+ * by `doctor` (api/doctor/_adapter.mjs) for its file-only docs checks and
26
+ * its theme-wiring scan,
27
+ * in a run that records modules and variables to its end, and, through
28
+ * `persistedSummary`, by kept-tree.mjs for the docs not-found hint's
29
+ * summaries of folder trees, each kept while every path it stamped is
30
+ * unchanged, in any command. The commands whose runs keep the other
31
+ * entries are `search`, `build`, `component`, `template`, `theme`,
32
+ * `discover`, `docs` and `doctor`. The first such run on a machine makes the
33
+ * folder and keeps its gathers at once, so the next run (most often the same
34
+ * session's next command) uses them; doctor never makes it. A run through
35
+ * the host entry keeps them too, keyed on what the host passed
36
+ * (hostInputs). Only a published CLI stores anything:
27
37
  * its build id (clients/cli/build-id.generated.json, written at `prepack`)
28
38
  * names its exact code, so a checkout, whose code may change between runs
29
39
  * under the same version, always gathers.
@@ -31,7 +41,7 @@
31
41
  * The folder is `catalogs` in the user's cache folder (userCacheDir):
32
42
  * created 0700, used only while it is a real folder owned by the user and
33
43
  * closed to others (isPrivateDir; never on a system with no user ids), each file written 0600 by a rename, oldest first evicted past
34
- * CACHE_LIMIT_BYTES. Any failure to read, check, parse or write is silent
44
+ * CACHE_LIMIT_BYTES (cache-eviction.mjs). Any failure to read, check, parse or write is silent
35
45
  * and the run gathers fresh, as it would with no cache.
36
46
  */
37
47
 
@@ -42,11 +52,14 @@ import * as os from 'node:os';
42
52
  import * as path from 'node:path';
43
53
  import {fileURLToPath} from 'node:url';
44
54
  import {isDeepStrictEqual} from 'node:util';
45
- import {CLI_ROOT} from './paths.mjs';
55
+ import {CLI_ROOT, realDir} from './paths.mjs';
46
56
  import {mayHaveIntegrations} from '../integrations/may-have-integrations.mjs';
47
57
  import {currentUid, isPrivateDir} from '../env/private-dir.mjs';
48
58
  import {userCacheDir} from '../env/user-dirs.mjs';
49
59
  import {jitiCacheDir} from './jiti-cache.mjs';
60
+ import {hostedRun} from '../runtime/package-root.mjs';
61
+ import {cliDoor} from '../env/cli-command.mjs';
62
+ import {CACHE_LIMIT_BYTES, evictOldest} from './cache-eviction.mjs';
50
63
  import {
51
64
  fileStamp,
52
65
  recordReads,
@@ -78,7 +91,8 @@ const KEPT_COMMANDS = new Set([
78
91
  'theme',
79
92
  'discover',
80
93
  'docs',
81
- // The project check alone (no subcommand): its docs checks are kept.
94
+ // The project check alone (no subcommand): its docs checks and theme-wiring
95
+ // scan are kept.
82
96
  'doctor',
83
97
  ]);
84
98
 
@@ -90,8 +104,12 @@ export const BUILD_ID_FILE = path.join(
90
104
  'build-id.generated.json',
91
105
  );
92
106
 
93
- /** The most the folder holds; the oldest entries go first past it. */
94
- export const CACHE_LIMIT_BYTES = 64 * 1024 * 1024;
107
+ /**
108
+ * Whether a file in the folder is a kept entry, which eviction counts.
109
+ * @param {string} name
110
+ * @returns {boolean}
111
+ */
112
+ const isEntry = name => name.startsWith('gather-');
95
113
 
96
114
  /** The stored format; a different one is a miss. */
97
115
  const FORMAT = 1;
@@ -254,9 +272,8 @@ export async function recordRunFor(argv, fn) {
254
272
  ) {
255
273
  return fn();
256
274
  }
257
- // Gathers are kept only once the folder exists: the first recorded run on a
258
- // machine (search, build, component, template, theme, discover or docs) just makes
259
- // it, so a one-off run never pays to store one.
275
+ // The first recorded run on a machine (search, build, component, template,
276
+ // theme, discover or docs) makes the folder and keeps its gathers at once.
260
277
  const dir = cacheDirPath();
261
278
  if (!withoutRecording(() => fs.existsSync(dir))) {
262
279
  // Once the per-user cache folder exists doctor uses it; it never makes
@@ -267,8 +284,8 @@ export async function recordRunFor(argv, fn) {
267
284
  withoutRecording(() => fs.mkdirSync(dir, {recursive: true, mode: 0o700}));
268
285
  } catch {
269
286
  // A folder that cannot be made keeps no catalogs; nothing more.
287
+ return fn();
270
288
  }
271
- return fn();
272
289
  }
273
290
  // A folder the cache would refuse (not the user's own, open to others, a
274
291
  // link) leaves the run exactly as without the cache: no hook is installed.
@@ -516,9 +533,29 @@ function entryKey(key, id) {
516
533
  process.platform,
517
534
  process.arch,
518
535
  ...key,
536
+ hostInputs(),
519
537
  ]);
520
538
  }
521
539
 
540
+ /**
541
+ * What a host passed that decides a gather, or null outside a hosted run: its
542
+ * package sets, by real path and in its order, the integrations it approves,
543
+ * and the command it runs the CLI as, which kept findings print. A run reads
544
+ * only some of a package set's files, so their stamps cannot tell a hosted
545
+ * run from a run of the bin in the same folder, and no file holds the
546
+ * command. The run's folder is in each gather's own key.
547
+ * @returns {{roots: string[], integrations: string[], door: string | null} | null}
548
+ */
549
+ function hostInputs() {
550
+ const hosted = hostedRun();
551
+ if (hosted == null) return null;
552
+ return {
553
+ roots: hosted.roots.map(root => withoutRecording(() => realDir(root))),
554
+ integrations: [...hosted.integrations],
555
+ door: cliDoor() ?? null,
556
+ };
557
+ }
558
+
522
559
  /**
523
560
  * Whether every stored state still holds: files, folders and modules, and the
524
561
  * environment.
@@ -744,7 +781,7 @@ export function persistedDerived(key, compute, codec, inputs = []) {
744
781
  const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
745
782
  fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
746
783
  fs.renameSync(temp, file);
747
- evict(dir);
784
+ evictOldest(dir, isEntry);
748
785
  } catch {
749
786
  // A value that cannot be stored is computed again next time.
750
787
  }
@@ -752,6 +789,102 @@ export function persistedDerived(key, compute, codec, inputs = []) {
752
789
  return value;
753
790
  }
754
791
 
792
+ /**
793
+ * How old every stamped time must be before a summary is stored. A file
794
+ * system can store times as coarsely as 2 s, so a change within one tick of
795
+ * the last can leave a stamp as it was.
796
+ */
797
+ const SUMMARY_SETTLE_MS = 2000;
798
+
799
+ /**
800
+ * Whether every stamp in `stamps` was taken longer than
801
+ * {@link SUMMARY_SETTLE_MS} ago, by its modification and change times.
802
+ * @param {[string, string][]} stamps
803
+ * @returns {boolean}
804
+ */
805
+ function settled(stamps) {
806
+ const limit = Date.now() - SUMMARY_SETTLE_MS;
807
+ return stamps.every(([, stamp]) => {
808
+ if (stamp === '-') return true;
809
+ const [, mtime, ctime] = stamp.split(':').map(Number);
810
+ return mtime < limit && ctime < limit;
811
+ });
812
+ }
813
+
814
+ /**
815
+ * A summary of files and folders, made at once, kept as an entry beside the
816
+ * gathers, in or out of a recorded run: the stored value while every path
817
+ * `make` stamped has the stamp it was stored with, else the one `make` gives
818
+ * now, which is stored unless a stamp is younger than
819
+ * {@link SUMMARY_SETTLE_MS}. The folder, the build id, the Yarn Plug'n'Play
820
+ * rule, the 0600 rename write and the eviction are those of a kept gather;
821
+ * anywhere a gather is not kept, or on any read or write error, `make` runs
822
+ * as before.
823
+ * @template T
824
+ * @param {unknown[]} key - Everything else the summary depends on.
825
+ * @param {() => {value: T, stamps: [string, string][]}} make - The summary,
826
+ * with the stamp ({@link fileStamp}) of each path it depends on, each taken
827
+ * before the path was read.
828
+ * @returns {T}
829
+ */
830
+ export function persistedSummary(key, make) {
831
+ const id = withoutRecording(buildId);
832
+ const dir =
833
+ id == null || runsUnderPnp() ? null : withoutRecording(gatherCacheDir);
834
+ if (id == null || dir == null) return make().value;
835
+ const summaryKey = ['summary', ...key];
836
+ const file = entryFile(dir, summaryKey, id);
837
+ const entry = entryKey(summaryKey, id);
838
+ try {
839
+ const stored = JSON.parse(
840
+ withoutRecording(() => fs.readFileSync(file, 'utf-8')),
841
+ );
842
+ if (
843
+ stored?.format === FORMAT &&
844
+ stored.summary === true &&
845
+ stored.key === entry &&
846
+ Object.hasOwn(stored, 'value') &&
847
+ Array.isArray(stored.stamps) &&
848
+ stored.stamps.every(
849
+ (/** @type {[string, string]} */ [at, stamp]) =>
850
+ typeof at === 'string' && fileStamp(at) === stamp,
851
+ )
852
+ ) {
853
+ try {
854
+ const now = new Date();
855
+ withoutRecording(() => fs.utimesSync(file, now, now));
856
+ } catch {
857
+ // An entry that cannot be touched is evicted sooner; nothing more.
858
+ }
859
+ return stored.value;
860
+ }
861
+ } catch {
862
+ // No usable entry: made below.
863
+ }
864
+ const {value, stamps} = make();
865
+ if (settled(stamps)) {
866
+ withoutRecording(() => {
867
+ try {
868
+ const text = JSON.stringify({
869
+ format: FORMAT,
870
+ summary: true,
871
+ key: entry,
872
+ stamps,
873
+ value,
874
+ });
875
+ if (!isDeepStrictEqual(JSON.parse(text).value, value)) return;
876
+ const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
877
+ fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
878
+ fs.renameSync(temp, file);
879
+ evictOldest(dir, isEntry);
880
+ } catch {
881
+ // A summary that cannot be stored is made again next time.
882
+ }
883
+ });
884
+ }
885
+ return value;
886
+ }
887
+
755
888
  /**
756
889
  * A stored entry's value, decoded, or undefined when it has none, its decode
757
890
  * throws, or the decoded value fails the codec's shape check (`valid`): such
@@ -798,43 +931,12 @@ function store(dir, file, entry, value, codec) {
798
931
  const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
799
932
  fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
800
933
  fs.renameSync(temp, file);
801
- evict(dir);
934
+ evictOldest(dir, isEntry);
802
935
  } catch {
803
936
  // A gather that cannot be stored is gathered again next time.
804
937
  }
805
938
  }
806
939
 
807
- /**
808
- * Delete the oldest entries until the folder holds at most CACHE_LIMIT_BYTES.
809
- * @param {string} dir
810
- */
811
- function evict(dir) {
812
- const entries = fs
813
- .readdirSync(dir)
814
- .filter(name => name.startsWith('gather-'))
815
- .map(name => {
816
- const entry = path.join(dir, name);
817
- try {
818
- const {size, mtimeMs} = fs.statSync(entry);
819
- return {entry, size, mtimeMs};
820
- } catch {
821
- return null;
822
- }
823
- })
824
- .filter(e => e != null)
825
- .sort((a, b) => a.mtimeMs - b.mtimeMs);
826
- let total = entries.reduce((sum, e) => sum + e.size, 0);
827
- for (const {entry, size} of entries) {
828
- if (total <= CACHE_LIMIT_BYTES) break;
829
- try {
830
- fs.unlinkSync(entry);
831
- total -= size;
832
- } catch {
833
- // Another run removed it.
834
- }
835
- }
836
- }
837
-
838
940
  /**
839
941
  * Whether this run is under Yarn Plug'n'Play's loader (`yarn astryx` in a PnP
840
942
  * project): Node's module hooks hand PnP's resolver options it refuses, so
@@ -849,15 +951,16 @@ export function runsUnderPnp() {
849
951
  /**
850
952
  * The cache folder and how much it holds, for `doctor`: null when the CLI
851
953
  * keeps no gathers (a checkout) or has no usable folder.
852
- * @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null}
853
- * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp)
954
+ * @returns {{dir: string, bytes: number, entries: number, pnp: boolean, limitBytes: number, jitiDir: string} | null}
955
+ * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp);
956
+ * `limitBytes`: the most the folder, and jiti's folder `jitiDir`, holds
854
957
  */
855
958
  export function gatherCacheSummary() {
856
959
  // The cache's own bookkeeping, never an input of what it keeps.
857
960
  return withoutRecording(summarize);
858
961
  }
859
962
 
860
- /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null} */
963
+ /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean, limitBytes: number, jitiDir: string} | null} */
861
964
  function summarize() {
862
965
  if (buildId() == null) return null;
863
966
  const dir = gatherCacheDir();
@@ -877,5 +980,12 @@ function summarize() {
877
980
  } catch {
878
981
  return null;
879
982
  }
880
- return {dir, bytes, entries, pnp: runsUnderPnp()};
983
+ return {
984
+ dir,
985
+ bytes,
986
+ entries,
987
+ pnp: runsUnderPnp(),
988
+ limitBytes: CACHE_LIMIT_BYTES,
989
+ jitiDir: jitiCacheDir(),
990
+ };
881
991
  }
@@ -9,7 +9,8 @@ export function jitiCacheDir(): string;
9
9
  /**
10
10
  * The `fsCache` option for a jiti loader: the per-user folder, made (0700)
11
11
  * on first use and used at once, while it is a real folder the user owns and
12
- * no one else can reach (the kept catalogs' check). A folder that fails the
12
+ * no one else can reach (the kept catalogs' check), kept within its size
13
+ * limit (keepWithinLimit). A folder that fails the
13
14
  * check, or cannot be made, is not used and nothing is cached on disk. Where
14
15
  * the system has no user ids (Windows) ownership cannot be checked, and jiti
15
16
  * keeps its default, the user's own temp folder there. Worked out once per
@@ -19,3 +20,20 @@ export function jitiCacheDir(): string;
19
20
  export function jitiCacheOptions(): {
20
21
  fsCache?: string | false;
21
22
  };
23
+ /**
24
+ * Whether a file in the folder is a copy jiti wrote, the only files the size
25
+ * limit removes.
26
+ * @param {string} name
27
+ * @returns {boolean}
28
+ */
29
+ export function isTranspiledCopy(name: string): boolean;
30
+ /**
31
+ * Keep the folder within `limit` bytes, least recently used copies first,
32
+ * never one used within RECENT_USE_MS. The folder's time moves only when a
33
+ * file is made or removed, which is the only way the folder grows (jiti
34
+ * writes an edited source's code over its old file), so a run whose folder
35
+ * time matches the last check reads no more than that.
36
+ * @param {string} dir
37
+ * @param {number} [limit]
38
+ */
39
+ export function keepWithinLimit(dir: string, limit?: number): void;
@@ -10,22 +10,30 @@
10
10
  * own and no one else can reach it; `{fsCache: false}` (transpiled in
11
11
  * memory only) when it is not; `{}` (jiti's own default) where the system
12
12
  * has no user ids (Windows), whose temp folder is the user's own.
13
- * @position Read by module-loader.mjs, doc-compiler/import.mjs and
14
- * gather-cache.mjs. jiti's default is a `jiti` folder in the system temp
13
+ * @position Read by module-loader.mjs, doc-compiler/import.mjs,
14
+ * theme build (api/theme/build/build.mjs) and gather-cache.mjs. jiti's default is a `jiti` folder in the system temp
15
15
  * folder, which on Linux every account on a machine shares, and which the
16
16
  * system empties from time to time: another account could leave transpiled
17
17
  * code there under the name jiti looks for,
18
18
  * and after each emptying every TypeScript file is transpiled again. jiti
19
19
  * names each cached file for its source's path and checks a hash of its
20
20
  * source content before using it, so an edited, upgraded or downgraded
21
- * file is transpiled again wherever the cache is. A set JITI_FS_CACHE is
22
- * jiti's own setting and left to jiti.
21
+ * file is transpiled again wherever the cache is. Each new source path adds
22
+ * a file, so the folder is kept within CACHE_LIMIT_BYTES (cache-eviction.mjs):
23
+ * a run that finds it grown since the last check removes its least recently
24
+ * used copies (isTranspiledCopy), and nothing else, never one written in
25
+ * the last day or read in the last day where the file system records reads.
26
+ * A copy that is gone or cut short is a miss, and jiti transpiles the
27
+ * source again.
28
+ * A set JITI_FS_CACHE is jiti's own setting and left to jiti.
23
29
  */
24
30
 
25
31
  import * as fs from 'node:fs';
26
32
  import * as path from 'node:path';
27
33
  import {currentUid, isPrivateDir} from '../env/private-dir.mjs';
28
34
  import {userCacheDir} from '../env/user-dirs.mjs';
35
+ import {CACHE_LIMIT_BYTES, evictOldest} from './cache-eviction.mjs';
36
+ import {withoutRecording} from './read-record.mjs';
29
37
 
30
38
  /** @type {{fsCache?: string | false} | undefined} */
31
39
  let options;
@@ -41,7 +49,8 @@ export function jitiCacheDir() {
41
49
  /**
42
50
  * The `fsCache` option for a jiti loader: the per-user folder, made (0700)
43
51
  * on first use and used at once, while it is a real folder the user owns and
44
- * no one else can reach (the kept catalogs' check). A folder that fails the
52
+ * no one else can reach (the kept catalogs' check), kept within its size
53
+ * limit (keepWithinLimit). A folder that fails the
45
54
  * check, or cannot be made, is not used and nothing is cached on disk. Where
46
55
  * the system has no user ids (Windows) ownership cannot be checked, and jiti
47
56
  * keeps its default, the user's own temp folder there. Worked out once per
@@ -58,9 +67,124 @@ export function jitiCacheOptions() {
58
67
  try {
59
68
  const dir = jitiCacheDir();
60
69
  if (!fs.existsSync(dir)) fs.mkdirSync(dir, {recursive: true, mode: 0o700});
61
- if (isPrivateDir(dir)) options = {fsCache: dir};
70
+ if (isPrivateDir(dir)) {
71
+ options = {fsCache: dir};
72
+ // The cache's own upkeep, never an input of what a kept gather holds.
73
+ withoutRecording(() => keepWithinLimit(dir));
74
+ }
62
75
  } catch {
63
76
  // No disk cache: every TypeScript file is transpiled in memory.
64
77
  }
65
78
  return options;
66
79
  }
80
+
81
+ /**
82
+ * The file in the folder that holds the folder's time at the last check. Its
83
+ * name has no hash, so it is never taken for a copy (isTranspiledCopy).
84
+ */
85
+ const CHECKED = '.checked';
86
+
87
+ /** A day, in milliseconds. */
88
+ const DAY_MS = 24 * 60 * 60 * 1000;
89
+
90
+ /**
91
+ * How long a copy is kept after its last use, whatever the folder holds.
92
+ * jiti checks that a copy exists and then reads it, with no fallback if the
93
+ * copy is gone in between, so the size limit never removes a copy a run may
94
+ * be reading: one written in the last day, or read in the last day where the
95
+ * file system records reads. Most systems record a read time at most once a
96
+ * day, so a read in the last day leaves a read time in the last two days.
97
+ */
98
+ const RECENT_USE_MS = 2 * DAY_MS;
99
+
100
+ /**
101
+ * When a copy was last used: its last write (jiti writes a copy when it
102
+ * transpiles the source), or its last read where the file system records one.
103
+ * @param {fs.Stats} stat
104
+ * @returns {number}
105
+ */
106
+ const lastUse = stat => Math.max(stat.mtimeMs, stat.atimeMs);
107
+
108
+ /**
109
+ * How jiti names a transpiled copy: the source's folder and stem (a dot
110
+ * folder keeps its dot), a hash of the source's path, and `.cjs` or `.mjs`
111
+ * (with `x` for JSX).
112
+ */
113
+ const TRANSPILED_COPY = /\.[0-9a-f]{8}\.[cm]jsx?$/;
114
+
115
+ /**
116
+ * Whether a file in the folder is a copy jiti wrote, the only files the size
117
+ * limit removes.
118
+ * @param {string} name
119
+ * @returns {boolean}
120
+ */
121
+ export function isTranspiledCopy(name) {
122
+ return TRANSPILED_COPY.test(name);
123
+ }
124
+
125
+ /**
126
+ * The folder's time at the last check, or null when there is no record: a
127
+ * record that is not a regular file is never read or written (links in the
128
+ * folder are never followed).
129
+ * @param {string} checked
130
+ * @returns {string | null}
131
+ */
132
+ function lastCheck(checked) {
133
+ try {
134
+ if (!fs.lstatSync(checked).isFile()) return null;
135
+ return fs.readFileSync(checked, 'utf8');
136
+ } catch {
137
+ return null;
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Record the folder's time. The record is made once, empty, which moves the
143
+ * folder's time; after that it is written in place, which does not, so the
144
+ * time it holds is the time a later run reads. Making it never replaces
145
+ * anything at its name, and writing it never follows a link.
146
+ * @param {string} dir
147
+ * @param {string} checked
148
+ */
149
+ function recordCheck(dir, checked) {
150
+ let stat = null;
151
+ try {
152
+ stat = fs.lstatSync(checked);
153
+ } catch {
154
+ fs.writeFileSync(checked, '', {flag: 'wx', mode: 0o600});
155
+ }
156
+ if (stat != null && !stat.isFile()) return;
157
+ const fd = fs.openSync(
158
+ checked,
159
+ fs.constants.O_WRONLY | fs.constants.O_TRUNC | fs.constants.O_NOFOLLOW,
160
+ );
161
+ try {
162
+ fs.writeSync(fd, String(fs.statSync(dir).mtimeMs));
163
+ } finally {
164
+ fs.closeSync(fd);
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Keep the folder within `limit` bytes, least recently used copies first,
170
+ * never one used within RECENT_USE_MS. The folder's time moves only when a
171
+ * file is made or removed, which is the only way the folder grows (jiti
172
+ * writes an edited source's code over its old file), so a run whose folder
173
+ * time matches the last check reads no more than that.
174
+ * @param {string} dir
175
+ * @param {number} [limit]
176
+ */
177
+ export function keepWithinLimit(dir, limit = CACHE_LIMIT_BYTES) {
178
+ const checked = path.join(dir, CHECKED);
179
+ try {
180
+ if (lastCheck(checked) === String(fs.statSync(dir).mtimeMs)) return;
181
+ evictOldest(dir, isTranspiledCopy, {
182
+ limit,
183
+ keepUsedSince: Date.now() - RECENT_USE_MS,
184
+ lastUse,
185
+ });
186
+ recordCheck(dir, checked);
187
+ } catch {
188
+ // A folder that cannot be checked is checked again by a later run.
189
+ }
190
+ }
@@ -0,0 +1,60 @@
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
+ /**
5
+ * Names as one string a lookup can search without parsing a list: each name
6
+ * on its own line. A list when a name holds a line break.
7
+ * @param {Iterable<string>} names
8
+ * @returns {string | string[]}
9
+ */
10
+ export function nameLines(names: Iterable<string>): string | string[];
11
+ /**
12
+ * Whether `lines` ({@link nameLines}) holds `name`.
13
+ * @param {string | string[]} lines
14
+ * @param {string} name
15
+ * @returns {boolean}
16
+ */
17
+ export function hasNameLine(lines: string | string[], name: string): boolean;
18
+ /**
19
+ * One root's tree as a summary reads it: its folders, and `readText`, which
20
+ * reads a file by its path from the root and stamps it.
21
+ * @typedef {{root: string, folders: TreeFolder[], readText: (rel: string) => string}} Tree
22
+ */
23
+ /**
24
+ * The summary `summarize` makes of the trees under `roots`, or the one a run
25
+ * stored for the same roots, kind and code while every folder it listed and
26
+ * every file it read is unchanged. Any failure to read or store a summary is
27
+ * silent, and the summary is made again.
28
+ * @template T
29
+ * @param {{kind: string, roots: string[], code: string[], skip?: (name: string) => boolean}} options
30
+ * `code`: the files whose code makes the summary; `skip`: folder names the
31
+ * walk does not enter (it is part of `kind`)
32
+ * @param {(trees: Tree[]) => T} summarize gets one tree per root, in order
33
+ * @returns {T}
34
+ */
35
+ export function keptTreeSummary<T>({ kind, roots, code, skip }: {
36
+ kind: string;
37
+ roots: string[];
38
+ code: string[];
39
+ skip?: (name: string) => boolean;
40
+ }, summarize: (trees: Tree[]) => T): T;
41
+ /**
42
+ * One folder of the tree: its path from the root (`''` for the root) and its
43
+ * entries, in the order the disk lists them.
44
+ */
45
+ export type TreeFolder = {
46
+ rel: string;
47
+ entries: Array<{
48
+ name: string;
49
+ isDir: boolean;
50
+ }>;
51
+ };
52
+ /**
53
+ * One root's tree as a summary reads it: its folders, and `readText`, which
54
+ * reads a file by its path from the root and stamps it.
55
+ */
56
+ export type Tree = {
57
+ root: string;
58
+ folders: TreeFolder[];
59
+ readText: (rel: string) => string;
60
+ };