@astryxdesign/cli 0.7.16 → 0.7.17-canary.g16fe6a8

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 (380) hide show
  1. package/README.md +76 -86
  2. package/api/build/_adapter.d.mts +12 -0
  3. package/api/build/_adapter.mjs +51 -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 +4 -3
  7. package/api/build/build.type.d.mts +2 -2
  8. package/api/build/build.type.mjs +3 -3
  9. package/api/build/kit/kind.d.mts +2 -1
  10. package/api/build/kit/kind.mjs +199 -10
  11. package/api/build/kit/kit.mjs +6 -4
  12. package/api/build/kit/rank.d.mts +4 -2
  13. package/api/build/kit/rank.mjs +125 -24
  14. package/api/build/kit/start.d.mts +5 -4
  15. package/api/build/kit/start.mjs +12 -8
  16. package/api/build/kit/weights.d.mts +39 -3
  17. package/api/build/kit/weights.json +1 -1
  18. package/api/build/kit/weights.mjs +340 -43
  19. package/api/build/kit/word-model.d.mts +23 -0
  20. package/api/build/kit/word-model.mjs +46 -0
  21. package/api/build/kit/words.d.mts +48 -0
  22. package/api/build/kit/words.mjs +90 -1
  23. package/api/component/__fixtures__/resolutions/modules.txt +4 -5
  24. package/api/component/list/list.d.mts +1 -1
  25. package/api/component/list/list.mjs +48 -6
  26. package/api/discover/_adapter.d.mts +1 -1
  27. package/api/discover/search/search.mjs +3 -2
  28. package/api/docs/_adapter.d.mts +28 -2
  29. package/api/docs/_adapter.mjs +249 -10
  30. package/api/docs/docs.d.mts +3 -0
  31. package/api/docs/docs.doc.mjs +7 -1
  32. package/api/docs/docs.mjs +12 -0
  33. package/api/docs/docs.type.d.mts +5 -0
  34. package/api/docs/docs.type.mjs +2 -0
  35. package/api/docs/node/node.mjs +1 -1
  36. package/api/doctor/_adapter.d.mts +27 -5
  37. package/api/doctor/_adapter.mjs +83 -10
  38. package/api/doctor/built-checks.mjs +40 -14
  39. package/api/doctor/doctor.d.mts +4 -3
  40. package/api/doctor/doctor.mjs +14 -7
  41. package/api/doctor/theme-checks.mjs +121 -17
  42. package/api/icon/_records.mjs +2 -1
  43. package/api/index.d.mts +2 -0
  44. package/api/index.mjs +2 -0
  45. package/api/integration/_adapter.d.mts +44 -0
  46. package/api/integration/_adapter.mjs +77 -1
  47. package/api/integration/authoring-checks.mjs +6 -6
  48. package/api/integration/example-scope.d.mts +14 -0
  49. package/api/integration/example-scope.mjs +44 -0
  50. package/api/integration/example-scope.type.d.mts +29 -0
  51. package/api/integration/example-scope.type.mjs +25 -0
  52. package/api/integration/integrationExampleScope.doc.d.mts +11 -0
  53. package/api/integration/integrationExampleScope.doc.mjs +59 -0
  54. package/api/integration/validate-integration.mjs +19 -1
  55. package/api/integration/validate-integration.type.d.mts +9 -0
  56. package/api/integration/validate-integration.type.mjs +1 -1
  57. package/api/integration/validateIntegration.doc.mjs +1 -1
  58. package/api/render/_adapter.mjs +591 -195
  59. package/api/render/render.doc.mjs +11 -3
  60. package/api/render/render.type.d.mts +52 -0
  61. package/api/render/render.type.mjs +31 -1
  62. package/api/search/ORDERING.md +1 -1
  63. package/api/search/_adapter.d.mts +9 -0
  64. package/api/search/_adapter.mjs +85 -0
  65. package/api/search/_candidate-score.mjs +51 -32
  66. package/api/search/_fit-order.mjs +3 -3
  67. package/api/search/_gather-components.d.mts +3 -2
  68. package/api/search/_gather-components.mjs +12 -3
  69. package/api/search/_lexical.d.mts +5 -0
  70. package/api/search/_lexical.mjs +8 -2
  71. package/api/search/_result.d.mts +11 -9
  72. package/api/search/_result.mjs +4 -4
  73. package/api/search/_score.d.mts +6 -1
  74. package/api/search/_score.mjs +15 -3
  75. package/api/search/search-index.mjs +7 -1
  76. package/api/search/search.d.mts +1 -1
  77. package/api/theme/_adapter.mjs +12 -1
  78. package/api/theme/build/build.d.mts +132 -1
  79. package/api/theme/build/build.mjs +482 -157
  80. package/api/theme/build/core-interception.d.mts +10 -0
  81. package/api/theme/build/core-interception.mjs +10 -0
  82. package/api/theme/build/font-warning.d.mts +44 -5
  83. package/api/theme/build/font-warning.mjs +118 -10
  84. package/api/theme/build/icon-imports.mjs +51 -9
  85. package/api/theme/theme.type.d.mts +10 -0
  86. package/api/theme/theme.type.mjs +7 -1
  87. package/api/theme-edit/_adapter.d.mts +115 -4
  88. package/api/theme-edit/_adapter.mjs +997 -83
  89. package/api/theme-edit/_catalog.d.mts +20 -2
  90. package/api/theme-edit/_catalog.mjs +50 -10
  91. package/api/theme-edit/_check.d.mts +75 -24
  92. package/api/theme-edit/_check.mjs +2672 -422
  93. package/api/theme-edit/_color.d.mts +10 -0
  94. package/api/theme-edit/_color.mjs +54 -1
  95. package/api/theme-edit/_compiler.d.mts +1 -0
  96. package/api/theme-edit/_compiler.mjs +133 -15
  97. package/api/theme-edit/_context.d.mts +14 -3
  98. package/api/theme-edit/_context.mjs +225 -65
  99. package/api/theme-edit/_css.d.mts +14 -0
  100. package/api/theme-edit/_css.mjs +46 -1
  101. package/api/theme-edit/_edit.d.mts +54 -4
  102. package/api/theme-edit/_edit.mjs +2966 -286
  103. package/api/theme-edit/_how-commands.d.mts +72 -1
  104. package/api/theme-edit/_how-commands.mjs +770 -9
  105. package/api/theme-edit/_how-question.d.mts +82 -5
  106. package/api/theme-edit/_how-question.mjs +6983 -701
  107. package/api/theme-edit/_how.d.mts +2 -1
  108. package/api/theme-edit/_how.mjs +237 -41
  109. package/api/theme-edit/_importers.d.mts +38 -0
  110. package/api/theme-edit/_importers.mjs +188 -0
  111. package/api/theme-edit/_marks.d.mts +31 -5
  112. package/api/theme-edit/_marks.mjs +57 -5
  113. package/api/theme-edit/_name-rule.d.mts +11 -0
  114. package/api/theme-edit/_name-rule.mjs +12 -0
  115. package/api/theme-edit/_normal.d.mts +303 -0
  116. package/api/theme-edit/_normal.mjs +1517 -0
  117. package/api/theme-edit/_palette.d.mts +12 -0
  118. package/api/theme-edit/_palette.mjs +37 -4
  119. package/api/theme-edit/_part-words.d.mts +31 -0
  120. package/api/theme-edit/_part-words.mjs +126 -0
  121. package/api/theme-edit/_parts.d.mts +28 -2
  122. package/api/theme-edit/_parts.mjs +267 -31
  123. package/api/theme-edit/_preview-app.d.mts +7 -13
  124. package/api/theme-edit/_preview-app.mjs +814 -406
  125. package/api/theme-edit/_preview.d.mts +116 -16
  126. package/api/theme-edit/_preview.mjs +649 -152
  127. package/api/theme-edit/_project.d.mts +22 -1
  128. package/api/theme-edit/_project.mjs +83 -3
  129. package/api/theme-edit/_settings.d.mts +467 -42
  130. package/api/theme-edit/_settings.mjs +3527 -468
  131. package/api/theme-edit/_tables.d.mts +21 -0
  132. package/api/theme-edit/_tables.mjs +97 -3
  133. package/api/theme-edit/_vet.d.mts +31 -0
  134. package/api/theme-edit/_vet.mjs +114 -8
  135. package/api/theme-edit/theme-edit.type.d.mts +16 -2
  136. package/api/theme-edit/theme-edit.type.mjs +6 -3
  137. package/api/theme-edit/themeEditHow.doc.mjs +1 -1
  138. package/api/theme-edit/themeEditNew.doc.d.mts +0 -1
  139. package/api/theme-edit/themeEditNew.doc.mjs +7 -4
  140. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  141. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -2
  142. package/api/upgrade/run/run.mjs +4 -3
  143. package/assets/codemods/integration-discovery.mjs +2 -1
  144. package/assets/codemods/run-codemod.mjs +87 -7
  145. package/assets/codemods/transforms/next/index.mjs +11 -1
  146. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  147. package/assets/component-list.generated.json +1 -0
  148. package/assets/docs/author-a-theme.doc.mjs +1 -0
  149. package/assets/docs/theme.doc.dense.mjs +2 -2
  150. package/assets/docs/theme.doc.mjs +1 -1
  151. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  152. package/assets/docs/tree/checks.doc.mjs +1 -1
  153. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  154. package/assets/docs/tree/render-pages.doc.mjs +44 -3
  155. package/assets/docs/tree/share-a-page.doc.mjs +157 -0
  156. package/assets/docs/tree/single-component.doc.mjs +4 -0
  157. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  158. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  159. package/assets/docs/use-a-theme.doc.mjs +1 -1
  160. package/assets/docs.generated.json +2 -2
  161. package/assets/search-catalog.generated.json +2 -0
  162. package/assets/self-audit.generated.json +1 -1
  163. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  164. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  165. package/assets/templates/blocks/components/Hooks/useContainerRevealHookUsage.tsx +2 -2
  166. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  167. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  168. package/assets/templates/components.generated.json +1 -1
  169. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  170. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +5 -1
  171. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  172. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  173. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  174. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  175. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  176. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  177. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  178. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  179. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  180. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  181. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  182. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  183. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  184. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  185. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  186. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  187. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  188. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  189. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  190. package/assets/templates/pages/ide/page.tsx +24 -2
  191. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  192. package/assets/templates/pages/library/template.doc.mjs +4 -1
  193. package/assets/templates/pages/login/template.doc.mjs +2 -1
  194. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  195. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  196. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  197. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  198. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  199. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  200. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  201. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  202. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  203. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  204. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  205. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  206. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  207. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  208. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  209. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  210. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  211. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  212. package/assets/theme.template.ts +6 -0
  213. package/authoring/debug/parse.d.mts +3 -3
  214. package/authoring/doctypes/_schema.d.mts +3 -3
  215. package/authoring/doctypes/command/command.doc.mjs +1 -1
  216. package/authoring/doctypes/component/categories.d.mts +37 -0
  217. package/authoring/doctypes/component/categories.mjs +54 -0
  218. package/authoring/doctypes/template/template.doc.mjs +1 -1
  219. package/authoring/doctypes/template/type.ts +9 -0
  220. package/authoring/example/example.doc.d.mts +12 -0
  221. package/authoring/example/example.doc.mjs +69 -0
  222. package/authoring/example/preview.d.mts +92 -0
  223. package/authoring/example/preview.mjs +994 -0
  224. package/authoring/example/type.ts +102 -0
  225. package/authoring/index.d.mts +1 -0
  226. package/authoring/index.d.ts +12 -0
  227. package/authoring/index.mjs +3 -1
  228. package/clients/cli/bin/astryx.mjs +6 -9
  229. package/clients/cli/build-id.generated.json +1 -1
  230. package/clients/cli/commands/component.doc.mjs +1 -1
  231. package/clients/cli/commands/component.mjs +22 -3
  232. package/clients/cli/commands/docs.doc.mjs +7 -1
  233. package/clients/cli/commands/docs.mjs +3 -1
  234. package/clients/cli/commands/doctor.mjs +32 -14
  235. package/clients/cli/commands/experimental/render.mjs +42 -1
  236. package/clients/cli/commands/experimental/theme.mjs +573 -119
  237. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  238. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  239. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +87 -1
  240. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +10 -3
  241. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -1
  242. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +2 -2
  243. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  244. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  245. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  246. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
  247. package/clients/cli/commands/experimental-theme-preview.doc.mjs +2 -1
  248. package/clients/cli/commands/experimental-theme.doc.mjs +5 -1
  249. package/clients/cli/commands/search.doc.mjs +2 -1
  250. package/clients/cli/commands/search.mjs +6 -1
  251. package/clients/cli/commands/template.mjs +4 -2
  252. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  253. package/clients/cli/commands/theme.doc.mjs +1 -1
  254. package/clients/cli/commands/theme.mjs +718 -438
  255. package/clients/cli/commands/upgrade.mjs +42 -7
  256. package/clients/cli/index.mjs +1 -1
  257. package/clients/cli/lib/cli-error.mjs +2 -1
  258. package/clients/cli/lib/component-format.mjs +205 -98
  259. package/clients/cli/lib/hook-format.mjs +9 -19
  260. package/clients/cli/lib/json-shim.mjs +132 -24
  261. package/clients/cli/lib/manifest.mjs +3 -2
  262. package/clients/cli/manifest.generated.json +1 -1
  263. package/foundation/agent-docs/agent-docs.mjs +1 -1
  264. package/foundation/config/theme-state.d.mts +13 -1
  265. package/foundation/config/theme-state.mjs +47 -7
  266. package/foundation/discovery/authoring-self-docs.mjs +1 -0
  267. package/foundation/discovery/component-discovery.d.mts +11 -0
  268. package/foundation/discovery/component-discovery.mjs +56 -8
  269. package/foundation/discovery/component-replacement.mjs +130 -1
  270. package/foundation/discovery/copied-passages.mjs +2 -1
  271. package/foundation/discovery/core-component-docs.d.mts +29 -0
  272. package/foundation/discovery/core-component-docs.mjs +83 -0
  273. package/foundation/discovery/doc-types.mjs +4 -3
  274. package/foundation/discovery/entry-name-files.d.mts +43 -0
  275. package/foundation/discovery/entry-name-files.mjs +264 -0
  276. package/foundation/discovery/hook-discovery.d.mts +8 -2
  277. package/foundation/discovery/hook-discovery.mjs +22 -8
  278. package/foundation/discovery/icon-definition-discovery.mjs +2 -1
  279. package/foundation/discovery/icon-set-checks.mjs +2 -1
  280. package/foundation/discovery/icon-set-discovery.mjs +2 -1
  281. package/foundation/discovery/integration-doc-files.mjs +2 -1
  282. package/foundation/discovery/package-exports.d.mts +10 -0
  283. package/foundation/discovery/package-exports.mjs +56 -3
  284. package/foundation/discovery/prop-drift.mjs +9 -9
  285. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  286. package/foundation/discovery/runner-allowlist.mjs +2 -0
  287. package/foundation/discovery/stored-component-list.d.mts +43 -0
  288. package/foundation/discovery/stored-component-list.mjs +90 -0
  289. package/foundation/discovery/stored-search-catalog.d.mts +115 -0
  290. package/foundation/discovery/stored-search-catalog.mjs +297 -0
  291. package/foundation/discovery/stored-self-audit.d.mts +26 -10
  292. package/foundation/discovery/stored-self-audit.mjs +38 -18
  293. package/foundation/discovery/template-adapter.d.mts +53 -1
  294. package/foundation/discovery/template-adapter.mjs +176 -12
  295. package/foundation/discovery/template-icons.mjs +2 -1
  296. package/foundation/discovery/theme-discovery.mjs +31 -4
  297. package/foundation/discovery/theme-imports.d.mts +14 -0
  298. package/foundation/discovery/theme-imports.mjs +43 -1
  299. package/foundation/discovery/theming-targets.d.mts +6 -5
  300. package/foundation/discovery/theming-targets.mjs +31 -47
  301. package/foundation/doc-compiler/import.mjs +7 -1
  302. package/foundation/doc-compiler/inputs.mjs +3 -2
  303. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  304. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  305. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  306. package/foundation/env/semver.mjs +3 -1
  307. package/foundation/fonts/font-stylesheet.d.mts +38 -0
  308. package/foundation/fonts/font-stylesheet.mjs +141 -0
  309. package/foundation/fs/cache-eviction.d.mts +21 -0
  310. package/foundation/fs/cache-eviction.mjs +73 -0
  311. package/foundation/fs/exact-codec.d.mts +12 -2
  312. package/foundation/fs/exact-codec.mjs +23 -6
  313. package/foundation/fs/file-protection.mjs +4 -3
  314. package/foundation/fs/gather-cache.d.mts +62 -4
  315. package/foundation/fs/gather-cache.mjs +517 -91
  316. package/foundation/fs/jiti-cache.d.mts +19 -1
  317. package/foundation/fs/jiti-cache.mjs +130 -6
  318. package/foundation/fs/kept-tree.d.mts +60 -0
  319. package/foundation/fs/kept-tree.mjs +154 -0
  320. package/foundation/fs/listing-cache.d.mts +7 -0
  321. package/foundation/fs/listing-cache.mjs +10 -0
  322. package/foundation/fs/module-loader.d.mts +24 -2
  323. package/foundation/fs/module-loader.mjs +94 -21
  324. package/foundation/fs/paths.d.mts +18 -0
  325. package/foundation/fs/paths.mjs +15 -2
  326. package/foundation/fs/read-record.d.mts +9 -0
  327. package/foundation/fs/read-record.mjs +19 -11
  328. package/foundation/fs/stop-cleanup.d.mts +12 -0
  329. package/foundation/fs/stop-cleanup.mjs +66 -0
  330. package/foundation/identity/provider-identity.d.mts +6 -0
  331. package/foundation/identity/provider-identity.mjs +10 -1
  332. package/foundation/integrations/contribution-inventory.mjs +4 -3
  333. package/foundation/integrations/example-code.d.mts +63 -0
  334. package/foundation/integrations/example-code.mjs +416 -0
  335. package/foundation/integrations/public-imports.d.mts +9 -0
  336. package/foundation/integrations/public-imports.mjs +3 -2
  337. package/foundation/integrations/validate-contributions.mjs +17 -45
  338. package/foundation/render/embedded-files.d.mts +6 -0
  339. package/foundation/render/embedded-files.mjs +25 -5
  340. package/foundation/render/embedded-imports.d.mts +39 -7
  341. package/foundation/render/embedded-imports.mjs +66 -27
  342. package/foundation/render/file-header.d.mts +66 -0
  343. package/foundation/render/file-header.mjs +118 -0
  344. package/foundation/render/host-answer.d.mts +18 -0
  345. package/foundation/render/host-answer.mjs +46 -0
  346. package/foundation/render/host-exit.d.mts +1 -0
  347. package/foundation/render/host-exit.mjs +6 -1
  348. package/foundation/render/html-reader.d.mts +55 -2
  349. package/foundation/render/html-reader.mjs +145 -26
  350. package/foundation/render/markup-edits.d.mts +57 -0
  351. package/foundation/render/markup-edits.mjs +182 -0
  352. package/foundation/render/page-host.mjs +29 -12
  353. package/foundation/render/page-worker.mjs +121 -13
  354. package/foundation/render/picture-chrome.d.mts +4 -0
  355. package/foundation/render/picture-chrome.mjs +185 -0
  356. package/foundation/render/picture-playwright.d.mts +4 -0
  357. package/foundation/render/picture-playwright.mjs +34 -0
  358. package/foundation/render/rebuild.mjs +2 -1
  359. package/foundation/render/render-page.d.mts +35 -3
  360. package/foundation/render/render-page.mjs +641 -96
  361. package/foundation/render/source-block.d.mts +85 -6
  362. package/foundation/render/source-block.mjs +211 -20
  363. package/foundation/render/ts-paths.d.mts +69 -6
  364. package/foundation/render/ts-paths.mjs +326 -40
  365. package/foundation/response/error-codes.d.mts +3 -1
  366. package/foundation/response/error-codes.d.ts +1 -0
  367. package/foundation/response/error-codes.doc.mjs +12 -1
  368. package/foundation/response/error-codes.mjs +11 -1
  369. package/foundation/response/response-types.doc.mjs +2 -2
  370. package/foundation/text/compare-text.d.mts +14 -0
  371. package/foundation/text/compare-text.mjs +68 -0
  372. package/foundation/text/css-escape.d.mts +18 -0
  373. package/foundation/text/css-escape.mjs +35 -0
  374. package/foundation/text/string-utils.d.mts +3 -2
  375. package/foundation/text/string-utils.mjs +18 -4
  376. package/foundation/text/suggest-order.d.mts +9 -0
  377. package/foundation/text/suggest-order.mjs +15 -1
  378. package/foundation/xle/expand.mjs +2 -1
  379. package/host/index.mjs +8 -6
  380. package/package.json +7 -6
@@ -17,38 +17,64 @@
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, by the docs adapter (api/docs/_adapter.mjs) for
27
+ * the CLI's own docs tree, which `docs` and `search` read,
28
+ * in a run that records modules and variables to its end, and, through
29
+ * `persistedSummary`, by kept-tree.mjs for the docs not-found hint's
30
+ * summaries of folder trees, each kept while every path it stamped is
31
+ * unchanged, in any command. The commands whose runs keep the other
32
+ * entries are `search`, `build`, `component`, `template`, `theme`,
33
+ * `discover`, `docs` and `doctor`. The first such run on a machine makes the
34
+ * folder and keeps its gathers at once, so the next run (most often the same
35
+ * session's next command) uses them; doctor never makes it. A run through
36
+ * the host entry keeps them too, keyed on what the host passed
37
+ * (hostInputs). Only a published CLI stores anything:
27
38
  * its build id (clients/cli/build-id.generated.json, written at `prepack`)
28
39
  * names its exact code, so a checkout, whose code may change between runs
29
40
  * under the same version, always gathers.
30
41
  *
42
+ * An entry stores only the states that the kept entries it builds on do
43
+ * not hold: it names each of them, at the version it read, and is used
44
+ * only while each is still there at that version and holds. A run checks
45
+ * each kept entry once, so entries that read the same files (the component
46
+ * list, its replacements, the project's issues) check those files once. An
47
+ * entry built on is marked as used after the entries built on it, so
48
+ * eviction removes it after them. A gather's entry file holds two lines:
49
+ * what the entry is and stands for (its format, key, version, the entries
50
+ * it builds on, its states and variables), then its value. Checking an
51
+ * entry, or one an entry builds on, parses only the first line.
52
+ *
31
53
  * The folder is `catalogs` in the user's cache folder (userCacheDir):
32
54
  * created 0700, used only while it is a real folder owned by the user and
33
55
  * 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
56
+ * CACHE_LIMIT_BYTES (cache-eviction.mjs). Any failure to read, check, parse or write is silent
35
57
  * and the run gathers fresh, as it would with no cache.
36
58
  */
37
59
 
38
- import {createHash} from 'node:crypto';
60
+ import {createHash, randomBytes} from 'node:crypto';
39
61
  import * as fs from 'node:fs';
40
62
  import * as nodeModule from 'node:module';
41
63
  import * as os from 'node:os';
42
64
  import * as path from 'node:path';
43
65
  import {fileURLToPath} from 'node:url';
44
66
  import {isDeepStrictEqual} from 'node:util';
45
- import {CLI_ROOT} from './paths.mjs';
67
+ import {CLI_ROOT, realDir} from './paths.mjs';
46
68
  import {mayHaveIntegrations} from '../integrations/may-have-integrations.mjs';
47
69
  import {currentUid, isPrivateDir} from '../env/private-dir.mjs';
48
70
  import {userCacheDir} from '../env/user-dirs.mjs';
49
71
  import {jitiCacheDir} from './jiti-cache.mjs';
72
+ import {hostedRun} from '../runtime/package-root.mjs';
73
+ import {cliDoor} from '../env/cli-command.mjs';
74
+ import {CACHE_LIMIT_BYTES, evictOldest} from './cache-eviction.mjs';
50
75
  import {
51
76
  fileStamp,
77
+ keepState,
52
78
  recordReads,
53
79
  recordStates,
54
80
  stillInState,
@@ -78,7 +104,9 @@ const KEPT_COMMANDS = new Set([
78
104
  'theme',
79
105
  'discover',
80
106
  'docs',
81
- // The project check alone (no subcommand): its docs checks are kept.
107
+ // Its project check, with or without `--built`, keeps its docs checks and
108
+ // theme-wiring scan; its `integration` checks run neither, and keep only
109
+ // the project gathers they load, as the commands above do.
82
110
  'doctor',
83
111
  ]);
84
112
 
@@ -90,11 +118,15 @@ export const BUILD_ID_FILE = path.join(
90
118
  'build-id.generated.json',
91
119
  );
92
120
 
93
- /** The most the folder holds; the oldest entries go first past it. */
94
- export const CACHE_LIMIT_BYTES = 64 * 1024 * 1024;
121
+ /**
122
+ * Whether a file in the folder is a kept entry, which eviction counts.
123
+ * @param {string} name
124
+ * @returns {boolean}
125
+ */
126
+ const isEntry = name => name.startsWith('gather-');
95
127
 
96
128
  /** The stored format; a different one is a miss. */
97
- const FORMAT = 1;
129
+ const FORMAT = 2;
98
130
 
99
131
  /** A variable the run read that was unset, as stored. */
100
132
  const UNSET = null;
@@ -106,7 +138,7 @@ const UNSET = null;
106
138
  * variables only while the run is armed: from its start to the end of its
107
139
  * first gather, and during each later gather, or, in a run armed throughout
108
140
  * (`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}
141
+ * @type {{reads: Map<string, string>, env: Map<string, string | null>, arm: () => boolean, disarm: () => void, armedThroughout: boolean, realEnv: NodeJS.ProcessEnv, confirmed: Confirmed} | null}
110
142
  */
111
143
  let run = null;
112
144
 
@@ -137,6 +169,18 @@ let gatherCode = null;
137
169
  /** How many persistedGather calls are running, nested inside one another. */
138
170
  let gatherDepth = 0;
139
171
 
172
+ /**
173
+ * The kept entries a recorded run has read back or stored, by file: the
174
+ * version each holds, every state it stands for (its own and those of the
175
+ * entries it builds on, as the lists each stores, once each), and every entry
176
+ * it builds on, directly or not. A later entry stores only the states none of
177
+ * them holds and names the ones it builds on, so a run checks each state
178
+ * once. A kept entry is checked before the run's command writes anything; the
179
+ * record ends with the run.
180
+ * @typedef {{version: string, lists: [string, string][][], under: Set<string>, deps: string[], merged?: [string, string][]}} ConfirmedEntry
181
+ * @typedef {Map<string, ConfirmedEntry>} Confirmed
182
+ */
183
+
140
184
  /**
141
185
  * The outcome of the last kept gather in this process: whether it was a hit,
142
186
  * and why it missed. Null when no kept gather ran.
@@ -241,8 +285,6 @@ export async function recordRunFor(argv, fn) {
241
285
  if (
242
286
  !command ||
243
287
  !KEPT_COMMANDS.has(command) ||
244
- (command === 'doctor' &&
245
- argv.filter(arg => !arg.startsWith('-')).length > 1) ||
246
288
  runsUnderPnp() ||
247
289
  currentUid() == null ||
248
290
  withoutRecording(buildId) == null ||
@@ -254,9 +296,8 @@ export async function recordRunFor(argv, fn) {
254
296
  ) {
255
297
  return fn();
256
298
  }
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.
299
+ // The first recorded run on a machine (search, build, component, template,
300
+ // theme, discover or docs) makes the folder and keeps its gathers at once.
260
301
  const dir = cacheDirPath();
261
302
  if (!withoutRecording(() => fs.existsSync(dir))) {
262
303
  // Once the per-user cache folder exists doctor uses it; it never makes
@@ -267,8 +308,8 @@ export async function recordRunFor(argv, fn) {
267
308
  withoutRecording(() => fs.mkdirSync(dir, {recursive: true, mode: 0o700}));
268
309
  } catch {
269
310
  // A folder that cannot be made keeps no catalogs; nothing more.
311
+ return fn();
270
312
  }
271
- return fn();
272
313
  }
273
314
  // A folder the cache would refuse (not the user's own, open to others, a
274
315
  // link) leaves the run exactly as without the cache: no hook is installed.
@@ -389,7 +430,15 @@ export async function recordRun(fn, {armedThroughout = false} = {}) {
389
430
  }
390
431
  };
391
432
  if (!arm()) return fn();
392
- run = {reads, env, arm, disarm, armedThroughout, realEnv};
433
+ run = {
434
+ reads,
435
+ env,
436
+ arm,
437
+ disarm,
438
+ armedThroughout,
439
+ realEnv,
440
+ confirmed: new Map(),
441
+ };
393
442
  try {
394
443
  return await recordReads(reads, fn, {exact: true});
395
444
  } finally {
@@ -516,9 +565,29 @@ function entryKey(key, id) {
516
565
  process.platform,
517
566
  process.arch,
518
567
  ...key,
568
+ hostInputs(),
519
569
  ]);
520
570
  }
521
571
 
572
+ /**
573
+ * What a host passed that decides a gather, or null outside a hosted run: its
574
+ * package sets, by real path and in its order, the integrations it approves,
575
+ * and the command it runs the CLI as, which kept findings print. A run reads
576
+ * only some of a package set's files, so their stamps cannot tell a hosted
577
+ * run from a run of the bin in the same folder, and no file holds the
578
+ * command. The run's folder is in each gather's own key.
579
+ * @returns {{roots: string[], integrations: string[], door: string | null} | null}
580
+ */
581
+ function hostInputs() {
582
+ const hosted = hostedRun();
583
+ if (hosted == null) return null;
584
+ return {
585
+ roots: hosted.roots.map(root => withoutRecording(() => realDir(root))),
586
+ integrations: [...hosted.integrations],
587
+ door: cliDoor() ?? null,
588
+ };
589
+ }
590
+
522
591
  /**
523
592
  * Whether every stored state still holds: files, folders and modules, and the
524
593
  * environment.
@@ -546,6 +615,184 @@ function staleReason(stamps, env) {
546
615
  return null;
547
616
  }
548
617
 
618
+ /**
619
+ * The states of the kept entries an entry builds on, each found unchanged
620
+ * once per run, with every entry under them and the files of the ones it
621
+ * names; or null when one is gone, holds another version, or changed.
622
+ * @param {string} dir
623
+ * @param {unknown} deps - [entry file name, version] pairs, as stored
624
+ * @param {Confirmed} confirmed - the entries already found unchanged
625
+ * @param {Set<string>} [reading] - entries being checked, so a loop of
626
+ * entries that name each other is a miss
627
+ * @returns {{lists: [string, string][][], under: Set<string>, direct: string[]} | null}
628
+ */
629
+ function builtOn(dir, deps, confirmed, reading = new Set()) {
630
+ if (!Array.isArray(deps)) return null;
631
+ /** @type {[string, string][][]} */
632
+ const lists = [];
633
+ // Two entries this one names may build on one entry: its list counts once.
634
+ /** @type {Set<[string, string][]>} */
635
+ const listed = new Set();
636
+ /** @type {Set<string>} */
637
+ const under = new Set();
638
+ /** @type {string[]} */
639
+ const direct = [];
640
+ for (const dep of deps) {
641
+ if (!Array.isArray(dep) || typeof dep[0] !== 'string') return null;
642
+ const file = path.join(dir, path.basename(dep[0]));
643
+ const entry = confirmedEntry(file, dep[1], confirmed, reading);
644
+ if (entry == null) return null;
645
+ for (const list of entry.lists) {
646
+ if (listed.has(list)) continue;
647
+ listed.add(list);
648
+ lists.push(list);
649
+ }
650
+ direct.push(file);
651
+ under.add(file);
652
+ for (const below of entry.under) under.add(below);
653
+ }
654
+ return {lists, under, direct};
655
+ }
656
+
657
+ /**
658
+ * Every state a confirmed entry stands for, one per path: its lists, merged
659
+ * the first time something needs them as one (choosing what a new entry
660
+ * builds on).
661
+ * @param {ConfirmedEntry} entry
662
+ * @returns {[string, string][]}
663
+ */
664
+ function statesOf(entry) {
665
+ entry.merged ??= mergeStates(...entry.lists);
666
+ return entry.merged;
667
+ }
668
+
669
+ /**
670
+ * States from several lists, one per path, as a run's record keeps them
671
+ * (keepState).
672
+ * @param {...[string, string][]} lists
673
+ * @returns {[string, string][]}
674
+ */
675
+ function mergeStates(...lists) {
676
+ /** @type {Map<string, string>} */
677
+ const merged = new Map();
678
+ for (const list of lists) {
679
+ for (const [at, state] of list) keepState(merged, at, state);
680
+ }
681
+ return [...merged];
682
+ }
683
+
684
+ /**
685
+ * Mark an entry as used now, and each kept entry it builds on as used a
686
+ * millisecond (the finest step of a file time set from a Date) after the
687
+ * latest entry built on it, so eviction, which removes the least recently
688
+ * used entries first, never removes an entry before one built on it.
689
+ * @param {string} file
690
+ * @param {Confirmed} confirmed
691
+ */
692
+ function markUsed(file, confirmed) {
693
+ /** @type {Map<string, number>} */
694
+ const times = new Map();
695
+ /** @type {Set<string>} */
696
+ const onPath = new Set();
697
+ /**
698
+ * @param {string} at
699
+ * @param {number} time
700
+ */
701
+ const visit = (at, time) => {
702
+ if (onPath.has(at) || (times.get(at) ?? -Infinity) >= time) return;
703
+ times.set(at, time);
704
+ onPath.add(at);
705
+ for (const dep of confirmed.get(at)?.deps ?? []) visit(dep, time + 1);
706
+ onPath.delete(at);
707
+ };
708
+ visit(file, Date.now());
709
+ for (const [at, time] of times) {
710
+ try {
711
+ const when = new Date(time);
712
+ withoutRecording(() => fs.utimesSync(at, when, when));
713
+ } catch {
714
+ // An entry that cannot be touched is evicted sooner; nothing more.
715
+ }
716
+ }
717
+ }
718
+
719
+ /**
720
+ * A kept entry at `version`, found unchanged as a hit finds it: read and
721
+ * checked the first time the run meets it, after that from `confirmed`.
722
+ * @param {string} file
723
+ * @param {unknown} version
724
+ * @param {Confirmed} confirmed
725
+ * @param {Set<string>} reading
726
+ * @returns {ConfirmedEntry | null}
727
+ */
728
+ function confirmedEntry(file, version, confirmed, reading) {
729
+ const known = confirmed.get(file);
730
+ if (known) return known.version === version ? known : null;
731
+ if (reading.has(file)) return null;
732
+ reading.add(file);
733
+ try {
734
+ const stored = readHead(file);
735
+ if (
736
+ stored?.format !== FORMAT ||
737
+ typeof stored.version !== 'string' ||
738
+ stored.version !== version ||
739
+ staleReason(stored.stamps, stored.env) != null
740
+ ) {
741
+ return null;
742
+ }
743
+ const base = builtOn(path.dirname(file), stored.deps, confirmed, reading);
744
+ if (base == null) return null;
745
+ /** @type {ConfirmedEntry} */
746
+ const entry = {
747
+ version: stored.version,
748
+ lists: [...base.lists, stored.stamps],
749
+ under: base.under,
750
+ deps: base.direct,
751
+ };
752
+ confirmed.set(file, entry);
753
+ return entry;
754
+ } catch {
755
+ return null;
756
+ } finally {
757
+ reading.delete(file);
758
+ }
759
+ }
760
+
761
+ /**
762
+ * The kept entries a new entry builds on: those the run has read back or
763
+ * stored whose every state the run read too, as it holds them, largest
764
+ * first, each only when it holds a state the others do not. Never one that
765
+ * builds on the new entry's own file. The new entry stores the rest of its
766
+ * states itself.
767
+ * @param {string} file - the new entry's file
768
+ * @param {[string, string][]} stamps - every state the run read
769
+ * @param {Confirmed} confirmed
770
+ * @returns {{deps: [string, string][], own: [string, string][], under: Set<string>}}
771
+ */
772
+ function chooseBase(file, stamps, confirmed) {
773
+ const states = new Map(stamps);
774
+ /** @type {Set<string>} */
775
+ const covered = new Set();
776
+ /** @type {[string, string][]} */
777
+ const deps = [];
778
+ /** @type {Set<string>} */
779
+ const under = new Set();
780
+ const candidates = [...confirmed]
781
+ .filter(([other, entry]) => other !== file && !entry.under.has(file))
782
+ .map(([other, entry]) => ({other, entry, held: statesOf(entry)}))
783
+ .sort((a, b) => b.held.length - a.held.length);
784
+ for (const {other, entry, held} of candidates) {
785
+ if (held.length === 0) continue;
786
+ if (!held.every(([at, state]) => states.get(at) === state)) continue;
787
+ if (held.every(([at]) => covered.has(at))) continue;
788
+ deps.push([path.basename(other), entry.version]);
789
+ under.add(other);
790
+ for (const below of entry.under) under.add(below);
791
+ for (const [at] of held) covered.add(at);
792
+ }
793
+ return {deps, own: stamps.filter(([at]) => !covered.has(at)), under};
794
+ }
795
+
549
796
  /**
550
797
  * The gather `load` returns, or the one a run with the same key stored while
551
798
  * every input it read is unchanged (see the file header).
@@ -604,21 +851,45 @@ async function gatherAt(key, load, codec, keep) {
604
851
  const file = entryFile(dir, key, id);
605
852
  let reason = 'no entry';
606
853
  try {
607
- const stored = JSON.parse(
608
- withoutRecording(() => fs.readFileSync(file, 'utf-8')),
609
- );
854
+ const {head: stored, value: storedValue} = readEntry(file);
610
855
  if (stored?.format !== FORMAT) {
611
856
  reason = 'another format';
612
857
  } else if (stored.key !== entryKey(key, id)) {
613
858
  reason = 'an entry for other inputs';
614
859
  } else {
615
- const stale = staleReason(stored.stamps, stored.env);
616
- const value = stale == null ? readBack(stored, codec) : undefined;
860
+ // An entry the run already found unchanged, as one an earlier entry
861
+ // builds on, is not checked again. Outside a recorded run nothing is
862
+ // remembered from one gather to the next.
863
+ const confirmed = run?.confirmed ?? new Map();
864
+ const known = confirmed.get(file);
865
+ const seen =
866
+ typeof stored.version === 'string' && known?.version === stored.version
867
+ ? known
868
+ : null;
869
+ const own = seen ? null : staleReason(stored.stamps, stored.env);
870
+ const base =
871
+ seen || own != null ? null : builtOn(dir, stored.deps, confirmed);
872
+ const stale =
873
+ own ??
874
+ (seen == null && base == null
875
+ ? 'a kept entry it builds on changed'
876
+ : null);
877
+ const value = stale == null ? readBack(storedValue, codec) : undefined;
617
878
  if (stale == null && value === undefined) {
618
879
  // Gathered again below, and stored over the bad entry.
619
880
  reason = 'a malformed value';
620
881
  } else if (stale == null) {
621
- recordStates(stored.stamps);
882
+ /** @type {ConfirmedEntry} */
883
+ const entry = seen ?? {
884
+ version: stored.version,
885
+ lists: [...(base?.lists ?? []), stored.stamps],
886
+ under: base?.under ?? new Set(),
887
+ deps: base?.direct ?? [],
888
+ };
889
+ confirmed.set(file, entry);
890
+ // Recorded list by list, which keeps one state per path as a merge
891
+ // would (recordStates).
892
+ for (const list of entry.lists) recordStates(list);
622
893
  // A gather around this one depends on what this one read, the
623
894
  // environment included: a hit hands its variables on, as a fresh
624
895
  // gather's reads would be.
@@ -627,16 +898,14 @@ async function gatherAt(key, load, codec, keep) {
627
898
  if (!run.env.has(name)) run.env.set(name, value);
628
899
  }
629
900
  }
630
- gatherCode = cliStates(stored.stamps);
901
+ // The CLI's own files, one state per path, in the order a fresh
902
+ // gather reads them: merging only those is merging all, then picking
903
+ // them out.
904
+ gatherCode = mergeStates(...entry.lists.map(cliStates));
631
905
  lastOutcome = {hit: true, reason: 'unchanged'};
632
- try {
633
- const now = new Date();
634
- withoutRecording(() => fs.utimesSync(file, now, now));
635
- } catch {
636
- // An entry that cannot be touched is evicted sooner; nothing more.
637
- }
906
+ markUsed(file, confirmed);
638
907
  return value;
639
- } else {
908
+ } else if (stale != null) {
640
909
  reason = stale;
641
910
  }
642
911
  }
@@ -650,6 +919,10 @@ async function gatherAt(key, load, codec, keep) {
650
919
  const value = await load();
651
920
  if (run && armed && keep(value)) {
652
921
  withoutRecording(() => store(dir, file, entryKey(key, id), value, codec));
922
+ } else {
923
+ // A value worked out from a gather that is not kept is not kept either.
924
+ // The code a gather inside this one recorded did not build this one.
925
+ gatherCode = null;
653
926
  }
654
927
  return value;
655
928
  }
@@ -716,7 +989,7 @@ export function persistedDerived(key, compute, codec, inputs = []) {
716
989
  stored?.format === FORMAT &&
717
990
  stored.derived === true &&
718
991
  stored.key === entry
719
- ? readBack(stored, codec)
992
+ ? readBack(() => stored.value, codec)
720
993
  : undefined;
721
994
  if (value !== undefined) {
722
995
  try {
@@ -733,18 +1006,15 @@ export function persistedDerived(key, compute, codec, inputs = []) {
733
1006
  const value = compute();
734
1007
  withoutRecording(() => {
735
1008
  try {
736
- const encoded = codec.encode(value);
737
- const text = JSON.stringify({
738
- format: FORMAT,
739
- derived: true,
740
- key: entry,
741
- value: encoded,
742
- });
743
- if (!isDeepStrictEqual(JSON.parse(text).value, encoded)) return;
1009
+ const body = storedText(codec, value);
1010
+ if (body === undefined) return;
1011
+ const head = JSON.stringify({format: FORMAT, derived: true, key: entry});
1012
+ // The value's text goes in as checked, as the head's last field.
1013
+ const text = `${head.slice(0, -1)},"value":${body}}`;
744
1014
  const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
745
1015
  fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
746
1016
  fs.renameSync(temp, file);
747
- evict(dir);
1017
+ evictOldest(dir, isEntry);
748
1018
  } catch {
749
1019
  // A value that cannot be stored is computed again next time.
750
1020
  }
@@ -752,18 +1022,165 @@ export function persistedDerived(key, compute, codec, inputs = []) {
752
1022
  return value;
753
1023
  }
754
1024
 
1025
+ /**
1026
+ * How old every stamped time must be before a summary is stored. A file
1027
+ * system can store times as coarsely as 2 s, so a change within one tick of
1028
+ * the last can leave a stamp as it was.
1029
+ */
1030
+ const SUMMARY_SETTLE_MS = 2000;
1031
+
1032
+ /**
1033
+ * Whether every stamp in `stamps` was taken longer than
1034
+ * {@link SUMMARY_SETTLE_MS} ago, by its modification and change times.
1035
+ * @param {[string, string][]} stamps
1036
+ * @returns {boolean}
1037
+ */
1038
+ function settled(stamps) {
1039
+ const limit = Date.now() - SUMMARY_SETTLE_MS;
1040
+ return stamps.every(([, stamp]) => {
1041
+ if (stamp === '-') return true;
1042
+ const [, mtime, ctime] = stamp.split(':').map(Number);
1043
+ return mtime < limit && ctime < limit;
1044
+ });
1045
+ }
1046
+
1047
+ /**
1048
+ * A summary of files and folders, made at once, kept as an entry beside the
1049
+ * gathers, in or out of a recorded run: the stored value while every path
1050
+ * `make` stamped has the stamp it was stored with, else the one `make` gives
1051
+ * now, which is stored unless a stamp is younger than
1052
+ * {@link SUMMARY_SETTLE_MS}. The folder, the build id, the Yarn Plug'n'Play
1053
+ * rule, the 0600 rename write and the eviction are those of a kept gather;
1054
+ * anywhere a gather is not kept, or on any read or write error, `make` runs
1055
+ * as before.
1056
+ * @template T
1057
+ * @param {unknown[]} key - Everything else the summary depends on.
1058
+ * @param {() => {value: T, stamps: [string, string][]}} make - The summary,
1059
+ * with the stamp ({@link fileStamp}) of each path it depends on, each taken
1060
+ * before the path was read.
1061
+ * @returns {T}
1062
+ */
1063
+ export function persistedSummary(key, make) {
1064
+ const id = withoutRecording(buildId);
1065
+ const dir =
1066
+ id == null || runsUnderPnp() ? null : withoutRecording(gatherCacheDir);
1067
+ if (id == null || dir == null) return make().value;
1068
+ const summaryKey = ['summary', ...key];
1069
+ const file = entryFile(dir, summaryKey, id);
1070
+ const entry = entryKey(summaryKey, id);
1071
+ try {
1072
+ const stored = JSON.parse(
1073
+ withoutRecording(() => fs.readFileSync(file, 'utf-8')),
1074
+ );
1075
+ if (
1076
+ stored?.format === FORMAT &&
1077
+ stored.summary === true &&
1078
+ stored.key === entry &&
1079
+ Object.hasOwn(stored, 'value') &&
1080
+ Array.isArray(stored.stamps) &&
1081
+ stored.stamps.every(
1082
+ (/** @type {[string, string]} */ [at, stamp]) =>
1083
+ typeof at === 'string' && fileStamp(at) === stamp,
1084
+ )
1085
+ ) {
1086
+ try {
1087
+ const now = new Date();
1088
+ withoutRecording(() => fs.utimesSync(file, now, now));
1089
+ } catch {
1090
+ // An entry that cannot be touched is evicted sooner; nothing more.
1091
+ }
1092
+ return stored.value;
1093
+ }
1094
+ } catch {
1095
+ // No usable entry: made below.
1096
+ }
1097
+ const {value, stamps} = make();
1098
+ if (settled(stamps)) {
1099
+ withoutRecording(() => {
1100
+ try {
1101
+ const text = JSON.stringify({
1102
+ format: FORMAT,
1103
+ summary: true,
1104
+ key: entry,
1105
+ stamps,
1106
+ value,
1107
+ });
1108
+ if (!isDeepStrictEqual(JSON.parse(text).value, value)) return;
1109
+ const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
1110
+ fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
1111
+ fs.renameSync(temp, file);
1112
+ evictOldest(dir, isEntry);
1113
+ } catch {
1114
+ // A summary that cannot be stored is made again next time.
1115
+ }
1116
+ });
1117
+ }
1118
+ return value;
1119
+ }
1120
+
1121
+ /**
1122
+ * A gather's entry file: its first line, parsed, and a reader of its value,
1123
+ * the second line, parsed only when called. A file of one line, as an entry
1124
+ * of another format, is all head and has no value.
1125
+ * @param {string} file
1126
+ * @returns {{head: any, value: () => unknown}}
1127
+ */
1128
+ function readEntry(file) {
1129
+ const text = withoutRecording(() => fs.readFileSync(file, 'utf-8'));
1130
+ const cut = text.indexOf('\n');
1131
+ return {
1132
+ head: JSON.parse(cut < 0 ? text : text.slice(0, cut)),
1133
+ value: () => (cut < 0 ? undefined : JSON.parse(text.slice(cut + 1))),
1134
+ };
1135
+ }
1136
+
1137
+ /** How much of an entry file one read takes while looking for its first line. */
1138
+ const HEAD_READ_BYTES = 64 * 1024;
1139
+
1140
+ /**
1141
+ * A gather's entry file's first line, parsed, read without the value after
1142
+ * it: an entry another builds on is checked, never read back. A newline byte
1143
+ * is never part of a longer UTF-8 character, so the bytes before the first
1144
+ * one are the whole line.
1145
+ * @param {string} file
1146
+ * @returns {any}
1147
+ */
1148
+ function readHead(file) {
1149
+ return withoutRecording(() => {
1150
+ const fd = fs.openSync(file, 'r');
1151
+ try {
1152
+ /** @type {Buffer[]} */
1153
+ const chunks = [];
1154
+ const chunk = Buffer.allocUnsafe(HEAD_READ_BYTES);
1155
+ for (let at = 0; ;) {
1156
+ const read = fs.readSync(fd, chunk, 0, chunk.length, at);
1157
+ if (read === 0) break;
1158
+ const piece = chunk.subarray(0, read);
1159
+ const end = piece.indexOf(0x0a);
1160
+ chunks.push(Buffer.from(end < 0 ? piece : piece.subarray(0, end)));
1161
+ if (end >= 0) break;
1162
+ at += read;
1163
+ }
1164
+ return JSON.parse(Buffer.concat(chunks).toString('utf-8'));
1165
+ } finally {
1166
+ fs.closeSync(fd);
1167
+ }
1168
+ });
1169
+ }
1170
+
755
1171
  /**
756
1172
  * A stored entry's value, decoded, or undefined when it has none, its decode
757
1173
  * throws, or the decoded value fails the codec's shape check (`valid`): such
758
1174
  * an entry is a miss, worked out again and stored over.
759
- * @param {any} stored
1175
+ * @param {() => unknown} read - reads the entry's value
760
1176
  * @param {{decode: (stored: any) => any, valid?: (value: any) => boolean}} codec
761
1177
  * @returns {any}
762
1178
  */
763
- function readBack(stored, codec) {
764
- if (!Object.hasOwn(stored, 'value') || stored.value == null) return undefined;
1179
+ function readBack(read, codec) {
765
1180
  try {
766
- const value = codec.decode(stored.value);
1181
+ const stored = read();
1182
+ if (stored == null) return undefined;
1183
+ const value = codec.decode(stored);
767
1184
  if (value == null) return undefined;
768
1185
  return !codec.valid || codec.valid(value) ? value : undefined;
769
1186
  } catch {
@@ -771,6 +1188,26 @@ function readBack(stored, codec) {
771
1188
  }
772
1189
  }
773
1190
 
1191
+ /**
1192
+ * The JSON text a kept value is stored as, checked once to read back as what
1193
+ * the codec meant: the codec's own checked text when it gives one
1194
+ * (`encodeText`, as EXACT_CODEC does), else its plain form, written and
1195
+ * checked to read back as that form. Undefined for a value that does not
1196
+ * read back exactly, which is never kept; a codec's refusal throws.
1197
+ * @template T
1198
+ * @param {{encode: (value: T) => unknown, encodeText?: (value: T) => string}} codec
1199
+ * @param {T} value
1200
+ * @returns {string | undefined}
1201
+ */
1202
+ function storedText(codec, value) {
1203
+ if (codec.encodeText) return codec.encodeText(value);
1204
+ const encoded = codec.encode(value);
1205
+ const text = JSON.stringify(encoded);
1206
+ return text !== undefined && isDeepStrictEqual(JSON.parse(text), encoded)
1207
+ ? text
1208
+ : undefined;
1209
+ }
1210
+
774
1211
  /**
775
1212
  * Store a fresh gather with what the run read, unless its plain form does not
776
1213
  * read back as the same value. Silent on any failure.
@@ -784,57 +1221,38 @@ function readBack(stored, codec) {
784
1221
  function store(dir, file, entry, value, codec) {
785
1222
  if (!run) return;
786
1223
  try {
787
- const encoded = codec.encode(value);
1224
+ const body = storedText(codec, value);
1225
+ if (body === undefined) return;
788
1226
  const stamps = [...run.reads].filter(([file]) => !inJitiCache(file));
789
1227
  gatherCode = cliStates(stamps);
790
- const text = JSON.stringify({
1228
+ const {deps, own, under} = chooseBase(file, stamps, run.confirmed);
1229
+ const version = randomBytes(12).toString('hex');
1230
+ const head = JSON.stringify({
791
1231
  format: FORMAT,
792
1232
  key: entry,
793
- stamps,
1233
+ version,
1234
+ deps,
1235
+ stamps: own,
794
1236
  env: [...run.env],
795
- value: encoded,
796
1237
  });
797
- if (!isDeepStrictEqual(JSON.parse(text).value, encoded)) return;
1238
+ // JSON text holds no line break, so the first one ends the head.
1239
+ const text = `${head}\n${body}`;
798
1240
  const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
799
1241
  fs.writeFileSync(temp, text, {mode: 0o600, flag: 'wx'});
800
1242
  fs.renameSync(temp, file);
801
- evict(dir);
1243
+ run.confirmed.set(file, {
1244
+ version,
1245
+ lists: [stamps],
1246
+ under,
1247
+ deps: deps.map(([name]) => path.join(dir, name)),
1248
+ });
1249
+ markUsed(file, run.confirmed);
1250
+ evictOldest(dir, isEntry);
802
1251
  } catch {
803
1252
  // A gather that cannot be stored is gathered again next time.
804
1253
  }
805
1254
  }
806
1255
 
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
1256
  /**
839
1257
  * Whether this run is under Yarn Plug'n'Play's loader (`yarn astryx` in a PnP
840
1258
  * project): Node's module hooks hand PnP's resolver options it refuses, so
@@ -849,15 +1267,16 @@ export function runsUnderPnp() {
849
1267
  /**
850
1268
  * The cache folder and how much it holds, for `doctor`: null when the CLI
851
1269
  * 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)
1270
+ * @returns {{dir: string, bytes: number, entries: number, pnp: boolean, limitBytes: number, jitiDir: string} | null}
1271
+ * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp);
1272
+ * `limitBytes`: the most the folder, and jiti's folder `jitiDir`, holds
854
1273
  */
855
1274
  export function gatherCacheSummary() {
856
1275
  // The cache's own bookkeeping, never an input of what it keeps.
857
1276
  return withoutRecording(summarize);
858
1277
  }
859
1278
 
860
- /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null} */
1279
+ /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean, limitBytes: number, jitiDir: string} | null} */
861
1280
  function summarize() {
862
1281
  if (buildId() == null) return null;
863
1282
  const dir = gatherCacheDir();
@@ -877,5 +1296,12 @@ function summarize() {
877
1296
  } catch {
878
1297
  return null;
879
1298
  }
880
- return {dir, bytes, entries, pnp: runsUnderPnp()};
1299
+ return {
1300
+ dir,
1301
+ bytes,
1302
+ entries,
1303
+ pnp: runsUnderPnp(),
1304
+ limitBytes: CACHE_LIMIT_BYTES,
1305
+ jitiDir: jitiCacheDir(),
1306
+ };
881
1307
  }