@astryxdesign/cli 0.7.16 → 0.7.17-canary.g0135e03

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