@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
@@ -36,9 +36,10 @@ export function condition(ctx: import("./_settings.mjs").Ctx, target: string, co
36
36
  * Read `--at` points: "x,y;x,y", "x,y x,y", repeated, or a flat "a,b,c,d".
37
37
  * Each number is a whole pixel; anything else is refused by name.
38
38
  * @param {string[]} at
39
+ * @param {string | null} [file] the picture the points are on
39
40
  * @returns {Array<{x: number, y: number}>}
40
41
  */
41
- export function parsePoints(at: string[]): Array<{
42
+ export function parsePoints(at: string[], file?: string | null): Array<{
42
43
  x: number;
43
44
  y: number;
44
45
  }>;
@@ -33,15 +33,17 @@ import {
33
33
  editedThemes,
34
34
  loadTheme,
35
35
  nearest,
36
+ partComponentDocs,
36
37
  refuse,
38
+ themeSourceParseError,
37
39
  themeSourceProblem,
38
40
  } from './_settings.mjs';
39
- import {ASPECTS, checkSetting} from './_check.mjs';
41
+ import {ASPECTS, settableStates} from './_check.mjs';
40
42
  import {MODAL} from './_compiler.mjs';
41
43
  import {PALETTE_DIALS, mappingRows, paletteFiles} from './_palette.mjs';
42
44
  import {requireProject, context} from './_context.mjs';
43
45
  import {checked, exampleStep, howTheme, render} from './_how-commands.mjs';
44
- import {draftAnswer} from './_how-question.mjs';
46
+ import {draftAnswer, partLexicon, valueAsked} from './_how-question.mjs';
45
47
  import {
46
48
  getCliInvocation,
47
49
  quoteCommandArgument,
@@ -191,6 +193,16 @@ async function answer(subject, options, cwd) {
191
193
  // "½rem": a fraction is a number before anything reads the question.
192
194
  const t = (subject ?? '')
193
195
  .trim()
196
+ // "corners:", "var(--radius-element)": a setting pasted as written is
197
+ // a lookup of it.
198
+ .replace(/^([a-z][a-z -]*):\s*$/i, '$1')
199
+ .replace(/^var\(\s*(--[a-z0-9-]+)\s*\)$/i, '$1')
200
+ // A lone "½" with no unit after it is a share of now ("card padding
201
+ // ½"), never 0.5px.
202
+ .replace(
203
+ /(\d*)(?:½|1\u20442)(?!\s*(?:rem|px|em|%|x\b|×|times\b))/g,
204
+ (_, n) => (n ? `${n}½` : ' half '),
205
+ )
194
206
  .replace(/(\d*)(?:½|1\u20442)/g, (_, n) => `${n ? Number(n) + 0.5 : '0.5'}`)
195
207
  .replace(
196
208
  /(\d*)(?:¼|1\u20444)/g,
@@ -200,6 +212,12 @@ async function answer(subject, options, cwd) {
200
212
  /(\d*)(?:¾|3\u20444)/g,
201
213
  (_, n) => `${n ? Number(n) + 0.75 : '0.75'}`,
202
214
  );
215
+ // "paddingLeft", "borderRadius": a property as JavaScript spells it is
216
+ // that CSS property, as set takes it.
217
+ if (/^[a-z]+(?:[A-Z][a-z]+)+$/.test(t)) {
218
+ const kebab = t.replace(/[A-Z]/g, ch => `-${ch.toLowerCase()}`);
219
+ if (cssProperties().has(kebab)) return answer(kebab, options, cwd);
220
+ }
203
221
  // "explain spacing", "tell me about the accent": the lookup of what is
204
222
  // named, never a change.
205
223
  const explain =
@@ -214,14 +232,36 @@ async function answer(subject, options, cwd) {
214
232
  run: [`${HOW} ${t ? JSON.stringify(t) : 'card'}`],
215
233
  });
216
234
  if (options.theme != null) {
217
- loadTheme(cwd, options.theme);
235
+ try {
236
+ loadTheme(cwd, options.theme);
237
+ } catch (e) {
238
+ // A theme name near the one typed: the same question for it, never
239
+ // only `show` (the question is kept).
240
+ const err = /** @type {any} */ (e);
241
+ // (A name typed in another case or as a path too.)
242
+ if (err?.suggestions)
243
+ for (const s of err.suggestions) {
244
+ const near = / show ([a-z][a-z0-9-]*)$/.exec(
245
+ String(s.name ?? ''),
246
+ )?.[1];
247
+ if (near)
248
+ s.name = `${HOW} ${quoteCommandArgument(t || 'card')} --theme ${near}`;
249
+ }
250
+ throw e;
251
+ }
218
252
  // A theme whose source no longer extends its settings takes no setting:
219
253
  // an answer would suggest commands that cannot run.
220
- const broken = themeSourceProblem(cwd, options.theme);
254
+ const parseError = themeSourceParseError(cwd, options.theme);
255
+ const broken = parseError
256
+ ? {
257
+ problem: parseError,
258
+ fix: `Fix themes/${options.theme}/${options.theme}.theme.ts.`,
259
+ }
260
+ : await themeSourceProblem(cwd, options.theme);
221
261
  if (broken)
222
262
  refuse(
223
263
  E.ERR_THEME_INVALID,
224
- `${broken}, so settings cannot take effect and no command here would change the theme. Put back the import, extends and name lines in themes/${options.theme}/${options.theme}.theme.ts, then ask again.`,
264
+ `${broken.problem}, so settings cannot take effect and no command here would change the theme. ${broken.fix} Then ask again.`,
225
265
  {run: [`${P} show ${options.theme}`]},
226
266
  );
227
267
  }
@@ -247,16 +287,21 @@ async function answer(subject, options, cwd) {
247
287
  }
248
288
  if (isPicture(t, cwd))
249
289
  return {kind: 'picture', subject: t, ...pictureColors(t, cwd, at, count)};
290
+ // What runs instead: the same subject without the picture option (a
291
+ // real command), never a made-up file name.
292
+ const without = t ? `${HOW} ${quoteCommandArgument(t)}` : `${HOW} --help`;
250
293
  if (at.length)
251
294
  refuse(
252
295
  E.ERR_INVALID_ARGUMENT,
253
- '--at reads the color at points of a PNG picture; name the picture first.',
254
- {run: [`${HOW} picture.png --at "40,20"`]},
296
+ `--at reads the color at points of a PNG picture; name your picture first (${HOW} <your .png file> --at x,y).`,
297
+ {run: [without]},
255
298
  );
256
299
  if (count != null)
257
- refuse(E.ERR_INVALID_ARGUMENT, 'A count applies only to a picture.', {
258
- run: [`${HOW} picture.png 5`],
259
- });
300
+ refuse(
301
+ E.ERR_INVALID_ARGUMENT,
302
+ `A count applies only to a picture (${HOW} <your .png file> 5).`,
303
+ {run: [without]},
304
+ );
260
305
  if (!t) {
261
306
  refuse(
262
307
  E.ERR_MISSING_ARGUMENT,
@@ -430,9 +475,31 @@ async function answer(subject, options, cwd) {
430
475
  // "What does --color-accent do", "what's the variable for card padding":
431
476
  // "--focus-outline-width: 3px": a token typed with a value is set exactly,
432
477
  // or refused with the nearest names; never another token.
478
+ // "cards: --color-border red", "--color-border red on inputs": the token
479
+ // on that part.
480
+ const scopeLead = /^\s*([a-z][a-z -]*?)\s*:\s*(?=--)/i.exec(t)?.[1];
481
+ const scopeTail =
482
+ /\s+(?:on|for)\s+(?:the |my |all )?([a-z][a-z -]*?)\s*$/i.exec(t)?.[1];
483
+ const scopeWord = scopeLead ?? scopeTail;
484
+ const scopeOn = scopeWord
485
+ ? (() => {
486
+ const w = ` ${scopeWord.trim().toLowerCase()} `;
487
+ return (
488
+ partLexicon(ctx).parts.find(([re]) => re.test(w))?.[1].on ?? null
489
+ );
490
+ })()
491
+ : null;
433
492
  const typedToken =
434
493
  /^\s*(--[a-z][a-z0-9-]*)\s*(?::|\s+to\s+|\s+)\s*([^;]+?)\s*;?\s*(?:\bin (light|dark) mode\b.*)?$/i.exec(
435
- t.replace(/^\s*(?:change|set|make|token)\s+/i, ' '),
494
+ t
495
+ .replace(/^\s*(?:change|set|make|token)\s+/i, ' ')
496
+ .replace(/^\s*[a-z][a-z -]*?\s*:\s*(?=--)/i, ' ')
497
+ .replace(
498
+ scopeOn && scopeTail
499
+ ? /\s+(?:on|for)\s+(?:the |my |all )?[a-z][a-z -]*?\s*$/i
500
+ : /$^/,
501
+ '',
502
+ ),
436
503
  );
437
504
  if (
438
505
  typedToken &&
@@ -471,6 +538,7 @@ async function answer(subject, options, cwd) {
471
538
  verb: 'set',
472
539
  thing: token,
473
540
  value,
541
+ ...(scopeOn ? {on: scopeOn} : {}),
474
542
  ...(mode && env.data.mode === 'both' ? {mode} : {}),
475
543
  },
476
544
  ],
@@ -510,8 +578,10 @@ async function answer(subject, options, cwd) {
510
578
  results: tool,
511
579
  ...unknown,
512
580
  };
513
- // a question about what something is gets the lookup, not a change.
581
+ // a question about what something is gets the lookup, not a change; a
582
+ // question about a part's value now gets the value (draftAnswer).
514
583
  if (
584
+ !valueAsked(t) &&
515
585
  (/^\s*(what|which|where)\b.*\b(variables?|tokens?|do|does|means?|controls?|for)\b|^\s*what('s| is) the (css )?(variable|token|property)/i.test(
516
586
  t,
517
587
  ) ||
@@ -578,10 +648,13 @@ async function answer(subject, options, cwd) {
578
648
  ...unknown,
579
649
  ...(empty
580
650
  ? {
581
- hint: results.length
582
- ? `No command does all of "${t}" on ${env.slug}; the lines above say why.`
583
- : `No match for "${t}". Name the part (header, nav links, card, button, banner, input...) and what to change (text color, background, gradient, corners, border, shadow, spacing, bold), in English.`,
651
+ hint: `${
652
+ results.length
653
+ ? `No command does all of "${t}" on ${env.slug}; the lines above say why.`
654
+ : `No match for "${t}". Name the part (header, nav links, card, button, banner, input...) and what to change (text color, background, gradient, corners, border, shadow, spacing, bold), in English.`
655
+ } See ${HOW} --help for the asks it understands.`,
584
656
  run: [
657
+ `${HOW} --help`,
585
658
  `${HOW} --list parts`,
586
659
  `${HOW} "rounder buttons"`,
587
660
  `${HOW} colors`,
@@ -591,6 +664,69 @@ async function answer(subject, options, cwd) {
591
664
  };
592
665
  }
593
666
 
667
+ /**
668
+ * The commands a refusal names to run instead that answer the draft: the
669
+ * same thing set, accepted by set's check, and (for a draft that carries
670
+ * what its clause asks) measured to do it.
671
+ * @param {import('./_how-commands.mjs').HowTheme} env
672
+ * @param {import('./_how-question.mjs').Draft} d
673
+ * @param {Array<{command: string, why: string, run?: string[]}>} refused
674
+ * @returns {string[]}
675
+ */
676
+ function insteadOf(env, d, refused) {
677
+ // (The same thing on the same part: another part is another ask.)
678
+ const things = new Set(
679
+ d.options.flat().map(st => `${st.thing}|${st.on ?? ''}`),
680
+ );
681
+ /** @type {string[]} */
682
+ const out = [];
683
+ for (const line of refused.flatMap(r => r.run ?? [])) {
684
+ const words = [
685
+ ...line
686
+ .replace(/^.*? editor /, '')
687
+ .matchAll(/'((?:[^'\\]|\\.)*)'|"((?:[^"\\]|\\.)*)"|(\S+)/g),
688
+ ].map(m => m[1] ?? m[2] ?? m[3]);
689
+ const st = exampleStep(
690
+ words.map(w => (/\s/.test(w) ? `"${w}"` : w)).join(' '),
691
+ );
692
+ if (
693
+ !st ||
694
+ !things.has(`${st.thing}|${st.on ?? ''}`) ||
695
+ /[<>]/.test(String(st.value ?? ''))
696
+ )
697
+ continue;
698
+ if (!env.check([st]).ok) continue;
699
+ // Held to what the draft asks, else to the way its own dial step went
700
+ // ("more"): never offered unmeasured.
701
+ const way = d.options
702
+ .flat()
703
+ .map(x => x.value)
704
+ .find(v => v === 'more' || v === 'less');
705
+ const intent =
706
+ d.intent ??
707
+ (way
708
+ ? /** @type {import('./_how-commands.mjs').MeasureIntent} */ ({
709
+ families: [],
710
+ typed: [],
711
+ opacityDir: null,
712
+ dir: way,
713
+ zero: false,
714
+ lighter: null,
715
+ subjects: [],
716
+ keep: [],
717
+ mode: null,
718
+ })
719
+ : null);
720
+ if (!intent || typeof env.measure !== 'function') continue;
721
+ // A dial word the other way round from the one asked never answers it.
722
+ if (way && (st.value === 'more' || st.value === 'less') && st.value !== way)
723
+ continue;
724
+ if (env.measure([st], intent)) continue;
725
+ out.push(render(st, env.slug));
726
+ }
727
+ return out;
728
+ }
729
+
594
730
  /**
595
731
  * Check each draft's commands and keep what passes; a draft whose commands
596
732
  * are all refused says why, and points to the lookup that helps.
@@ -598,15 +734,83 @@ async function answer(subject, options, cwd) {
598
734
  * @param {import('./_how-question.mjs').Draft[]} drafts
599
735
  */
600
736
  function finish(env, drafts) {
601
- /** @type {Array<{text: string, commands: string[], sequence?: boolean, refused?: Array<{command: string, why: string}>}>} */
737
+ /** @type {Array<{text: string, commands: string[], sequence?: boolean, refused?: Array<{command: string, why: string, run?: string[]}>}>} */
602
738
  const out = [];
739
+ // The measured check runs first on the drafts that carry what their
740
+ // clause asks; a draft that joins them ("all of it, in order") then keeps
741
+ // only the steps some kept option still has, so it never brings back a
742
+ // step the check left out.
743
+ const key = (/** @type {import('./_how-commands.mjs').Step} */ st) =>
744
+ JSON.stringify([
745
+ st.verb,
746
+ st.thing,
747
+ st.value,
748
+ st.on,
749
+ st.when,
750
+ st.state,
751
+ st.mode,
752
+ ]);
753
+ /** @type {Set<string>} */
754
+ const dropped = new Set();
755
+ /** @type {Set<string>} */
756
+ const kept = new Set();
757
+ /** @type {Map<object, string[]>} */
758
+ const unmetOf = new Map();
759
+ if (typeof env.measure === 'function')
760
+ for (const d of drafts) {
761
+ if (!d.intent) continue;
762
+ const intent = d.intent;
763
+ /** @type {string[]} */
764
+ const unmet = [];
765
+ d.options = d.options.filter(o => {
766
+ const why = env.measure(o, intent);
767
+ for (const st of o) (why ? dropped : kept).add(key(st));
768
+ if (why) unmet.push(why);
769
+ return !why;
770
+ });
771
+ unmetOf.set(d, unmet);
772
+ }
603
773
  for (const d of drafts) {
774
+ if (!d.intent && dropped.size) {
775
+ d.options = d.options
776
+ .map(o => o.filter(st => !dropped.has(key(st)) || kept.has(key(st))))
777
+ .filter(o => o.length);
778
+ // A joined answer left holding what another answer already says is
779
+ // that answer: it is not given twice (and never as "all of it").
780
+ const same = (/** @type {import('./_how-commands.mjs').Step[]} */ o) =>
781
+ drafts.some(
782
+ x =>
783
+ x !== d &&
784
+ x.options.some(
785
+ y =>
786
+ y.length === o.length &&
787
+ y.every((st, i) => key(st) === key(o[i])),
788
+ ),
789
+ );
790
+ if (d.options.length && d.options.every(same)) continue;
791
+ }
792
+ // The measured check: an option whose built result does not do what
793
+ // the question asks is left out, with a plain line saying why.
794
+ const unmet = unmetOf.get(d) ?? [];
795
+ if (unmet.length) {
796
+ if (unmet.length && !d.options.length)
797
+ d.text = `couldn't work out a command that does "${d.text}" as asked: ${[...new Set(unmet)].join('; ')}`;
798
+ else if (unmet.length)
799
+ d.text = `${d.text} (left out: ${[...new Set(unmet)].join('; ')})`;
800
+ }
604
801
  const {commands, refused} = checked(env, d.options);
605
802
  const lookups = d.lookup ? [d.lookup] : [];
803
+ if (!commands.length && !d.options.length && unmet.length) {
804
+ out.push({text: d.text, commands: lookups});
805
+ continue;
806
+ }
606
807
  if (!commands.length && d.options.length && !lookups.length) {
607
808
  out.push({
608
809
  text: `${d.text}: no command does it on ${env.slug}${refused[0] ? `: ${refused[0].why}` : ''}`,
609
- commands: [],
810
+ // The command the refusal itself names to run instead, when it
811
+ // sets the same thing, passes set's check and does what the
812
+ // question asks.
813
+ commands: insteadOf(env, d, refused),
610
814
  refused,
611
815
  });
612
816
  continue;
@@ -682,25 +886,7 @@ function partAnswer(ctx, env, subject, key, unknown, themeFields) {
682
886
  ctx.project.target.get(key)
683
887
  );
684
888
  const group = familyOf(key);
685
- // Only the states set takes on this part (it refuses one that never
686
- // applies there).
687
- // Only the states set takes on this part: a state is dropped when the
688
- // same change without it passes and with it is refused.
689
- const takes = (/** @type {string | undefined} */ st) => {
690
- try {
691
- checkSetting(ctx, env.slug, env.data, 'opacity', '1', {
692
- on: key,
693
- ...(st ? {state: st} : {}),
694
- });
695
- return true;
696
- } catch {
697
- return false;
698
- }
699
- };
700
- const plainOk = takes(undefined);
701
- const states = [...new Set([...target.states, ...COMMON_STATES])].filter(
702
- st => !plainOk || takes(st),
703
- );
889
+ const states = settableStates(ctx, env.slug, env.data, key);
704
890
  const reads = Object.fromEntries(
705
891
  Object.keys(GROUPS).map(g => [
706
892
  g,
@@ -721,7 +907,9 @@ function partAnswer(ctx, env, subject, key, unknown, themeFields) {
721
907
  const extra = {};
722
908
  if (!recorded && !ctx.unchecked) {
723
909
  const {commands} = checked(env, [
724
- [{verb: 'set', thing: 'background', value: '#f3f4f6', on: key}],
910
+ // (A fill of one color is the background color: the shorthand would
911
+ // clear the image Core draws its hover tint with.)
912
+ [{verb: 'set', thing: 'background-color', value: '#f3f4f6', on: key}],
725
913
  [{verb: 'set', thing: 'color', value: '#111827', on: key}],
726
914
  [{verb: 'set', thing: 'border-radius', value: '8px', on: key}],
727
915
  ]);
@@ -734,6 +922,9 @@ function partAnswer(ctx, env, subject, key, unknown, themeFields) {
734
922
  kind: 'part',
735
923
  subject,
736
924
  component: target.component ?? null,
925
+ // What the part is, from its component's doc (a side nav heading is the
926
+ // product or account heading at the top, not a section title).
927
+ is: partComponentDocs(ctx, key).at(-1)?.doc.description || null,
737
928
  components,
738
929
  recorded,
739
930
  group,
@@ -1094,7 +1285,8 @@ function toolAnswer(t, env, targets) {
1094
1285
  },
1095
1286
  ];
1096
1287
  if (
1097
- asks ||
1288
+ // A question about a part's value now is answered with that value.
1289
+ (asks && !valueAsked(q)) ||
1098
1290
  /^which parts (are|does|do)\b/.test(q) ||
1099
1291
  (/^why (is|are|does|do)\b/.test(q) && !/\bnot\b|n'?t\b/.test(q))
1100
1292
  ) {
@@ -1342,14 +1534,18 @@ async function listItems(kind, filter, cwd) {
1342
1534
  * Read `--at` points: "x,y;x,y", "x,y x,y", repeated, or a flat "a,b,c,d".
1343
1535
  * Each number is a whole pixel; anything else is refused by name.
1344
1536
  * @param {string[]} at
1537
+ * @param {string | null} [file] the picture the points are on
1345
1538
  * @returns {Array<{x: number, y: number}>}
1346
1539
  */
1347
- export function parsePoints(at) {
1540
+ export function parsePoints(at, file = null) {
1348
1541
  const words = at
1349
1542
  .join(' ')
1350
1543
  .split(/[;\s]+/)
1351
1544
  .filter(Boolean);
1352
- const example = `${HOW} picture.png --at "40,20;300,180"`;
1545
+ // The picture named, never a made-up file.
1546
+ const example = file
1547
+ ? `${HOW} ${quoteCommandArgument(file)} --at "40,20;300,180"`
1548
+ : `${HOW} --help`;
1353
1549
  if (!words.length)
1354
1550
  refuse(E.ERR_INVALID_ARGUMENT, '--at names no point; give x,y pairs.', {
1355
1551
  run: [example],
@@ -1409,7 +1605,7 @@ function pictureColors(file, cwd, at, count) {
1409
1605
  `The count must be a positive whole number, not "${count}".`,
1410
1606
  {run: [`${HOW} ${file} 5`]},
1411
1607
  );
1412
- const points = at.length ? parsePoints(at) : null;
1608
+ const points = at.length ? parsePoints(at, file) : null;
1413
1609
  // The header holds the size, so a huge picture is refused unread.
1414
1610
  const fd = fs.openSync(abs, 'r');
1415
1611
  const head = Buffer.alloc(8);
@@ -0,0 +1,38 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * Files of the project's own that import any of `targets` (preview files
6
+ * about to be deleted), so deleting them would break the app's build. The
7
+ * whole project root is read: every .ts/.tsx/.js/.jsx/.mjs/.cjs/.mdx file
8
+ * outside node_modules, build output and dot folders, the preview's own
9
+ * files left out. Each file's import and export-from declarations,
10
+ * import() and require() calls (a string or a template with no
11
+ * substitution) are read from its syntax tree and resolved: relative,
12
+ * `@/`, or a tsconfig `paths` alias. `tooBig` when the project holds more
13
+ * than `limit` source files, so the caller refuses instead of guessing.
14
+ * @param {string} cwd
15
+ * @param {{files: string[]}} data the preview's record (its own files are
16
+ * left out)
17
+ * @param {string} dir the preview folder
18
+ * @param {string[]} targets absolute paths, without extension
19
+ * @param {{aliases: Array<{prefix: string, to: string[]}>, limit: number}} opts
20
+ * the app's tsconfig `paths` aliases, and how many source files to read
21
+ * at most
22
+ * @returns {{importers: string[], tooBig: boolean, limit: number}}
23
+ */
24
+ export function pageImporters(cwd: string, data: {
25
+ files: string[];
26
+ }, dir: string, targets: string[], { aliases, limit }: {
27
+ aliases: Array<{
28
+ prefix: string;
29
+ to: string[];
30
+ }>;
31
+ limit: number;
32
+ }): {
33
+ importers: string[];
34
+ tooBig: boolean;
35
+ limit: number;
36
+ };
37
+ /** At most this many source files the preview's commands read. */
38
+ export const IMPORTER_SCAN_MAX: 50000;
@@ -0,0 +1,188 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The files of an app that import preview pages about to be removed,
5
+ * read from their syntax trees, with how many source files are read at
6
+ * most as a parameter: past it, the caller refuses rather than guess.
7
+ *
8
+ * @position api/theme-edit — imported by _preview-app.mjs
9
+ */
10
+
11
+ import {parse as parseModule} from '@babel/parser';
12
+ import * as fs from 'node:fs';
13
+ import * as path from 'node:path';
14
+ import {shown} from './_settings.mjs';
15
+
16
+ /** Folders the importer search never enters: installs and build output. */
17
+ const NOT_SOURCE = new Set([
18
+ 'node_modules',
19
+ 'out',
20
+ 'dist',
21
+ 'build',
22
+ 'coverage',
23
+ ]);
24
+
25
+ /** At most this many source files the preview's commands read. */
26
+ export const IMPORTER_SCAN_MAX = 50000;
27
+
28
+ /**
29
+ * Files of the project's own that import any of `targets` (preview files
30
+ * about to be deleted), so deleting them would break the app's build. The
31
+ * whole project root is read: every .ts/.tsx/.js/.jsx/.mjs/.cjs/.mdx file
32
+ * outside node_modules, build output and dot folders, the preview's own
33
+ * files left out. Each file's import and export-from declarations,
34
+ * import() and require() calls (a string or a template with no
35
+ * substitution) are read from its syntax tree and resolved: relative,
36
+ * `@/`, or a tsconfig `paths` alias. `tooBig` when the project holds more
37
+ * than `limit` source files, so the caller refuses instead of guessing.
38
+ * @param {string} cwd
39
+ * @param {{files: string[]}} data the preview's record (its own files are
40
+ * left out)
41
+ * @param {string} dir the preview folder
42
+ * @param {string[]} targets absolute paths, without extension
43
+ * @param {{aliases: Array<{prefix: string, to: string[]}>, limit: number}} opts
44
+ * the app's tsconfig `paths` aliases, and how many source files to read
45
+ * at most
46
+ * @returns {{importers: string[], tooBig: boolean, limit: number}}
47
+ */
48
+ export function pageImporters(cwd, data, dir, targets, {aliases, limit}) {
49
+ const wanted = new Set(targets);
50
+ if (!wanted.size) return {importers: [], tooBig: false, limit};
51
+ const names = [...new Set(targets.map(t => path.basename(t)))];
52
+ const own = new Set(data.files.map(f => path.join(dir, f)));
53
+ /** @type {Set<string>} */
54
+ const out = new Set();
55
+ let files = 0;
56
+ let tooBig = false;
57
+ /** @param {string} file @param {string} spec */
58
+ const resolves = (file, spec) => {
59
+ const alias = aliases.find(x => spec.startsWith(x.prefix));
60
+ const bases = alias
61
+ ? alias.to.map(t => path.join(t, spec.slice(alias.prefix.length)))
62
+ : spec.startsWith('@/')
63
+ ? [path.join(cwd, 'src', spec.slice(2)), path.join(cwd, spec.slice(2))]
64
+ : spec.startsWith('.')
65
+ ? [path.resolve(path.dirname(file), spec)]
66
+ : [];
67
+ return bases.some(
68
+ b =>
69
+ wanted.has(b.replace(/\.(tsx|ts|jsx|js|mjs|cjs)$/, '')) ||
70
+ wanted.has(path.join(b, 'index')),
71
+ );
72
+ };
73
+ /** @param {any} node @param {(spec: string) => void} found */
74
+ const specifiers = (node, found) => {
75
+ if (!node || typeof node !== 'object') return;
76
+ if (Array.isArray(node)) {
77
+ for (const x of node) specifiers(x, found);
78
+ return;
79
+ }
80
+ const text = (/** @type {any} */ n) =>
81
+ n?.type === 'StringLiteral'
82
+ ? n.value
83
+ : n?.type === 'TemplateLiteral' && n.expressions.length === 0
84
+ ? n.quasis.map((/** @type {any} */ q) => q.value.cooked).join('')
85
+ : null;
86
+ if (
87
+ (node.type === 'ImportDeclaration' ||
88
+ node.type === 'ExportNamedDeclaration' ||
89
+ node.type === 'ExportAllDeclaration') &&
90
+ node.source
91
+ ) {
92
+ const t = text(node.source);
93
+ if (t != null) found(t);
94
+ } else if (node.type === 'ImportExpression') {
95
+ const t = text(node.source);
96
+ if (t != null) found(t);
97
+ } else if (
98
+ node.type === 'CallExpression' &&
99
+ (node.callee?.type === 'Import' ||
100
+ (node.callee?.type === 'Identifier' && node.callee.name === 'require'))
101
+ ) {
102
+ const t = text(node.arguments?.[0]);
103
+ if (t != null) found(t);
104
+ } else if (
105
+ node.type === 'TSImportType' ||
106
+ node.type === 'TSExternalModuleReference'
107
+ ) {
108
+ const t = text(node.argument ?? node.expression);
109
+ if (t != null) found(t);
110
+ }
111
+ for (const [k, v] of Object.entries(node))
112
+ if (
113
+ k !== 'loc' &&
114
+ k !== 'start' &&
115
+ k !== 'end' &&
116
+ v &&
117
+ typeof v === 'object'
118
+ )
119
+ specifiers(v, found);
120
+ };
121
+ /** @param {string} at */
122
+ const walk = at => {
123
+ if (tooBig) return;
124
+ /** @type {fs.Dirent[]} */
125
+ let entries;
126
+ try {
127
+ entries = fs.readdirSync(at, {withFileTypes: true});
128
+ } catch {
129
+ return;
130
+ }
131
+ for (const e of entries) {
132
+ const full = path.join(at, e.name);
133
+ if (e.isDirectory()) {
134
+ if (!NOT_SOURCE.has(e.name) && !e.name.startsWith('.')) walk(full);
135
+ if (tooBig) return;
136
+ continue;
137
+ }
138
+ if (!/\.(tsx|ts|jsx|js|mjs|cjs|mdx)$/.test(e.name) || own.has(full))
139
+ continue;
140
+ if (e.name.endsWith('.d.ts')) continue;
141
+ if (++files > limit) {
142
+ tooBig = true;
143
+ return;
144
+ }
145
+ /** @type {string} */
146
+ let source;
147
+ try {
148
+ source = fs.readFileSync(full, 'utf8');
149
+ } catch {
150
+ continue;
151
+ }
152
+ // Only a file that names a target's file name can import it.
153
+ if (!names.some(n => source.includes(n))) continue;
154
+ /** @type {string[]} */
155
+ const specs = [];
156
+ try {
157
+ const ast = parseModule(
158
+ e.name.endsWith('.mdx')
159
+ ? source
160
+ .split('\n')
161
+ .filter(l => /^\s*(import|export)\b/.test(l))
162
+ .join('\n')
163
+ : source,
164
+ {
165
+ sourceType: 'unambiguous',
166
+ errorRecovery: true,
167
+ plugins: [
168
+ 'jsx',
169
+ ...(/\.(ts|tsx|mts|cts)$/.test(e.name)
170
+ ? [/** @type {const} */ ('typescript')]
171
+ : []),
172
+ ],
173
+ },
174
+ );
175
+ specifiers(ast.program, spec => specs.push(spec));
176
+ } catch {
177
+ // Not parsed: read its import strings as text.
178
+ for (const m of source.matchAll(
179
+ /(?:from\s*|import\s*\(\s*|require\s*\(\s*|import\s+)['"`]([^'"`$]+)['"`]/g,
180
+ ))
181
+ specs.push(m[1]);
182
+ }
183
+ if (specs.some(sp => resolves(full, sp))) out.add(shown(cwd, full));
184
+ }
185
+ };
186
+ walk(cwd);
187
+ return {importers: [...out].sort(), tooBig, limit};
188
+ }