@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
@@ -15,8 +15,9 @@
15
15
  import {createHash} from 'node:crypto';
16
16
  import * as fs from 'node:fs';
17
17
  import * as path from 'node:path';
18
- import {spawnSync} from 'node:child_process';
19
- import {fileURLToPath, pathToFileURL} from 'node:url';
18
+ import {spawn} from 'node:child_process';
19
+ import * as os from 'node:os';
20
+ import {fileURLToPath} from 'node:url';
20
21
  import {
21
22
  SAMPLE_PAGE,
22
23
  SHEET_ORDER,
@@ -32,13 +33,12 @@ import {
32
33
  } from './_parts.mjs';
33
34
  import {OVERVIEW_SECTIONS} from './_overview.mjs';
34
35
  import {
35
- FOLDER_JUNK,
36
- folderIsEmpty,
37
36
  camel,
37
+ folderIsEmpty,
38
38
  fontPackages,
39
39
  modesOf,
40
- themeFiles,
41
40
  shown,
41
+ themeFiles,
42
42
  } from './_settings.mjs';
43
43
  import {writeWhole} from './_palette.mjs';
44
44
  import {cliVersion} from '../../foundation/release/run-context.mjs';
@@ -46,7 +46,9 @@ import {importPathOf, installLine} from './_project.mjs';
46
46
  import {COLOR_PROP_PARTS, DIRECT, P, wordTable} from './_tables.mjs';
47
47
  import {extractImportedPackages} from '../../foundation/discovery/template-needs.mjs';
48
48
  import {findNodeModulesPackage} from '../../foundation/fs/paths.mjs';
49
+ import {themeFileImports} from '../../foundation/discovery/theme-discovery.mjs';
49
50
  import {withListingCache} from '../../foundation/fs/listing-cache.mjs';
51
+ import {compareText} from '../../foundation/text/compare-text.mjs';
50
52
 
51
53
  const TEMPLATES_DIR = path.resolve(
52
54
  path.dirname(fileURLToPath(import.meta.url)),
@@ -163,7 +165,7 @@ export function showcaseItems(project, group) {
163
165
  };
164
166
  const chosen = items
165
167
  .filter(it => !group || it.group === group)
166
- .sort((a, b) => order(a) - order(b) || a.name.localeCompare(b.name));
168
+ .sort((a, b) => order(a) - order(b) || compareText(a.name, b.name));
167
169
  /** @type {Record<string, string>} */
168
170
  const imports = {};
169
171
  for (const n of elementNames(chosen.map(i => i.tree))) {
@@ -1303,7 +1305,9 @@ export function previewRouteSource({
1303
1305
  ' <AppShell',
1304
1306
  ' height="fill"',
1305
1307
  ' topNav={',
1306
- ` <TopNav label="Theme preview" heading={<strong>{${JSON.stringify(name)}}</strong>} endContent={`,
1308
+ // Core's own heading, as an app's top bar draws it, so a setting on the
1309
+ // header (its text color) shows here as it does in the app.
1310
+ ` <TopNav label="Theme preview" heading={<TopNavHeading heading={${JSON.stringify(name)}} />} endContent={`,
1307
1311
  ...(mode.length ? mode : [' null']),
1308
1312
  ' } />',
1309
1313
  ' }',
@@ -1328,7 +1332,7 @@ export function previewRouteSource({
1328
1332
  "import * as React from 'react';",
1329
1333
  "import {AppShell} from '@astryxdesign/core/AppShell';",
1330
1334
  "import {SideNav, SideNavItem} from '@astryxdesign/core/SideNav';",
1331
- "import {TopNav} from '@astryxdesign/core/TopNav';",
1335
+ "import {TopNav, TopNavHeading} from '@astryxdesign/core/TopNav';",
1332
1336
  ...(theme
1333
1337
  ? [
1334
1338
  // The mode switch only for a theme with both modes: an import the
@@ -1408,91 +1412,163 @@ export function previewRouteSource({
1408
1412
  /** @param {string} slug */
1409
1413
  const camelName = camel;
1410
1414
 
1415
+ /** The script that takes one picture through the project's Playwright. */
1416
+ const PLAYWRIGHT_PICTURE = fileURLToPath(
1417
+ new URL('../../foundation/render/picture-playwright.mjs', import.meta.url),
1418
+ );
1419
+
1420
+ /** The script that takes one picture through a Chrome or Chromium. */
1421
+ const CHROME_PICTURE = fileURLToPath(
1422
+ new URL('../../foundation/render/picture-chrome.mjs', import.meta.url),
1423
+ );
1424
+
1425
+ /** How long a stopped browser may take to close before it is killed. */
1426
+ const STOP_GRACE_MS = 5_000;
1427
+
1428
+ /** The signals that stop a picture run, the terminal closing among them. */
1429
+ const STOP_SIGNALS = /** @type {const} */ (['SIGINT', 'SIGTERM', 'SIGHUP']);
1430
+
1431
+ /** How long a browser may take for one picture before it is stopped. */
1432
+ const BROWSER_BOUND_MS = 60_000;
1433
+
1411
1434
  /**
1412
- * A PNG of an HTML file, with a Playwright the project has installed, else
1413
- * a Chrome or Chromium found on the machine. Not ok with a reason when
1414
- * neither is there, or when one is there and fails: the reason then names
1415
- * its error.
1435
+ * What the pictures of one run share: the browsers that failed or hung,
1436
+ * each with its reason, so a later picture does not wait on them again.
1437
+ * @typedef {{failed: Map<string, string>}} PictureRun
1438
+ */
1439
+
1440
+ /** A new picture run. @returns {PictureRun} */
1441
+ export function newPictureRun() {
1442
+ return {failed: new Map()};
1443
+ }
1444
+
1445
+ /**
1446
+ * A whole-page PNG of an HTML file, with a Playwright the project has
1447
+ * installed, else a Chrome or Chromium found on the machine. Each browser
1448
+ * gets its own time bound, and the next one its turn when one fails or
1449
+ * hangs. Not ok with a reason when none is there or every one failed: the
1450
+ * reason then names each one's real error. When a later browser took the
1451
+ * picture, `note` says why an earlier one did not.
1416
1452
  * @param {import('./_project.mjs').ThemeProject} project
1417
1453
  * @param {string} html absolute
1418
1454
  * @param {string} png absolute
1419
1455
  * @param {'light' | 'dark'} mode
1420
- * @returns {Promise<{ok: true, file: string, cut?: boolean} | {ok: false, reason: string}>} `cut` when the picture is of the window, not the whole page
1456
+ * @param {PictureRun} [run] what earlier pictures of the same run learned:
1457
+ * a browser that failed or hung there is not tried again
1458
+ * @returns {Promise<{ok: true, file: string, note?: string} | {ok: false, reason: string, stopped?: string}>}
1421
1459
  */
1422
- export async function screenshot(project, html, png, mode) {
1423
- /** What was found and why it could not take the picture. @type {string[]} */
1424
- const failed = [];
1425
- /** The first line of an error, with no path of this machine. @param {unknown} e */
1426
- const firstLine = e =>
1427
- String(/** @type {any} */ (e)?.message ?? e ?? '')
1460
+ export async function screenshot(
1461
+ project,
1462
+ html,
1463
+ png,
1464
+ mode,
1465
+ run = newPictureRun(),
1466
+ ) {
1467
+ /** What was tried and why it could not take the picture. @type {string[]} */
1468
+ const failed = [...run.failed.values()];
1469
+ /** Whether a Playwright was found without its own browser installed. */
1470
+ let pwMissing = false;
1471
+ /** Whether a Playwright was found at all. */
1472
+ let pwFound = false;
1473
+ /** A line with no path of this machine. @param {string} line */
1474
+ const local = line => line.split(project.cwd).join('.');
1475
+ /**
1476
+ * Why a browser did not take the picture: Playwright's first line names
1477
+ * only the call ("browserType.launch: Target … closed"); the cause, such
1478
+ * as a missing library, is in a later line, so that line comes too.
1479
+ * Chrome's startup warnings (dbus, GPU) are not a cause.
1480
+ * @param {string} text
1481
+ */
1482
+ const causeOf = text => {
1483
+ const lines = String(text ?? '')
1428
1484
  .split('\n')
1429
- .map(l => l.trim())
1430
- .find(Boolean)
1431
- ?.split(project.cwd)
1432
- .join('.') ?? 'it stopped with no message';
1485
+ .map(l => l.replace(/^[\s=<>-]+/, '').trim())
1486
+ .filter(Boolean);
1487
+ const head =
1488
+ meaningfulLine(lines.join('\n')) || 'it stopped with no message';
1489
+ const rest = lines
1490
+ .slice(lines.indexOf(head) + 1)
1491
+ .find(l =>
1492
+ /error|cannot|can't|missing|not found|while loading|no such|failed|denied/i.test(
1493
+ l,
1494
+ ),
1495
+ );
1496
+ return local(rest ? `${head} (${rest})` : head);
1497
+ };
1498
+ /** The browsers to try, in order: each a picture script and its args. */
1499
+ /** @type {Array<{who: string, script: string, args: string[]}>} */
1500
+ const tries = [];
1433
1501
  for (const pkg of ['playwright', 'playwright-core']) {
1434
- /** @type {string} */
1435
- let entry;
1436
1502
  try {
1437
- entry = project.req.resolve(pkg);
1503
+ const entry = project.req.resolve(pkg);
1504
+ pwFound = true;
1505
+ tries.push({
1506
+ who: `${pkg}'s own browser`,
1507
+ script: PLAYWRIGHT_PICTURE,
1508
+ args: [entry, html, png, mode],
1509
+ });
1510
+ break;
1438
1511
  } catch {
1439
- continue;
1440
- }
1441
- try {
1442
- const pw = await import(pathToFileURL(entry).href);
1443
- const chromium = pw.chromium ?? pw.default?.chromium;
1444
- const browser = await chromium.launch(
1445
- chromeBinary() ? {executablePath: chromeBinary()} : {},
1446
- );
1447
- try {
1448
- const page = await browser.newPage({
1449
- viewport: {width: 1280, height: 900},
1450
- colorScheme: mode,
1451
- });
1452
- await page.goto(pathToFileURL(html).href, {waitUntil: 'load'});
1453
- await page.screenshot({path: png, fullPage: true});
1454
- } finally {
1455
- await browser.close();
1456
- }
1457
- return {ok: true, file: shown(project.cwd, png)};
1458
- } catch (e) {
1459
- // This Playwright could not launch its browser: try Chrome directly.
1460
- failed.push(`${pkg}: ${firstLine(e)}`);
1512
+ // Not installed here.
1461
1513
  }
1462
1514
  }
1463
1515
  const chrome = chromeBinary();
1464
- if (chrome) {
1465
- const r = spawnSync(
1466
- chrome,
1467
- [
1468
- '--headless=new',
1469
- '--disable-gpu',
1470
- '--no-sandbox',
1471
- '--hide-scrollbars',
1472
- `--screenshot=${png}`,
1473
- '--window-size=1280,2400',
1474
- ...(mode === 'dark' ? ['--force-dark-mode'] : []),
1475
- pathToFileURL(html).href,
1476
- ],
1477
- {encoding: 'utf8', timeout: 60_000},
1478
- );
1479
- // Chrome alone takes the window, not the whole page.
1516
+ if (chrome)
1517
+ tries.push({
1518
+ who: chrome,
1519
+ script: CHROME_PICTURE,
1520
+ args: [chrome, html, png, mode],
1521
+ });
1522
+ for (const t of tries) {
1523
+ if (run.failed.has(t.who)) continue;
1524
+ const r = await runBrowser(t.script, t.args, BROWSER_BOUND_MS, {
1525
+ drop: [png],
1526
+ });
1527
+ if (r.stoppedBy)
1528
+ return {
1529
+ ok: false,
1530
+ stopped: r.stoppedBy,
1531
+ reason: `the pictures were stopped (${r.stoppedBy}) before the browser finished`,
1532
+ };
1533
+ if (r.noTemp)
1534
+ return {
1535
+ ok: false,
1536
+ reason: `no browser was started: the system temp folder cannot be written (${r.noTemp}), and a browser needs a profile folder of its own there; fix the temp folder (TMPDIR), then run the command again`,
1537
+ };
1480
1538
  if (r.status === 0 && fs.existsSync(png))
1481
- return {ok: true, file: shown(project.cwd, png), cut: true};
1482
- failed.push(
1483
- `${chrome}: ${firstLine(
1484
- r.error ??
1485
- (r.stderr ||
1486
- (r.signal
1487
- ? `stopped by ${r.signal}`
1488
- : `exit ${r.status}, and no picture was written`)),
1489
- )}`,
1490
- );
1539
+ return {
1540
+ ok: true,
1541
+ file: shown(project.cwd, png),
1542
+ ...(failed.length
1543
+ ? {
1544
+ note: `the picture was taken with ${t.who}; ${failed.join('; ')}`,
1545
+ }
1546
+ : {}),
1547
+ };
1548
+ const text = r.error ? String(r.error.message) : r.stderr;
1549
+ if (
1550
+ t.script === PLAYWRIGHT_PICTURE &&
1551
+ /Executable doesn't exist|playwright install/i.test(text)
1552
+ )
1553
+ pwMissing = true;
1554
+ const why = r.timedOut
1555
+ ? `${t.who} did not finish within ${BROWSER_BOUND_MS / 1000} seconds, so it was stopped`
1556
+ : `${t.who} could not take it: ${causeOf(text)}`;
1557
+ run.failed.set(t.who, why);
1558
+ failed.push(why);
1491
1559
  }
1492
1560
  if (failed.length)
1493
1561
  return {
1494
1562
  ok: false,
1495
- reason: `A browser was found, but it could not take the picture (${failed.join('; ')}). Give Playwright its browser (npx playwright install chromium), or check that ${chrome ?? 'Google Chrome or Chromium'} starts on this machine. The HTML preview needs neither.`,
1563
+ // Advice that can change the result: Playwright's browser only when
1564
+ // it is missing; a Chrome to check only when there is one.
1565
+ reason: `No picture was taken (${failed.join('; ')}). ${
1566
+ !pwFound
1567
+ ? `Install Playwright with its browser (${installLine(project.cwd, ['playwright'])}, then npx playwright install chromium), or`
1568
+ : pwMissing
1569
+ ? 'Give Playwright its browser (npx playwright install chromium), or'
1570
+ : "Fix what Playwright's browser needs (the reason is above), or"
1571
+ } ${chrome ? `check that ${chrome} starts on this machine` : 'install Google Chrome or Chromium'}. The HTML preview needs neither.`,
1496
1572
  };
1497
1573
  return {
1498
1574
  ok: false,
@@ -1515,11 +1591,23 @@ function chromeBinary() {
1515
1591
  'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
1516
1592
  'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
1517
1593
  ];
1518
- for (const f of fixed) if (fs.existsSync(f)) return f;
1594
+ // Only a file that runs: a folder or a file with no execute permission
1595
+ // by a browser's name is passed over, and the search goes on.
1596
+ /** @param {string} f */
1597
+ const runs = f => {
1598
+ try {
1599
+ if (!fs.statSync(f).isFile()) return false;
1600
+ if (process.platform !== 'win32') fs.accessSync(f, fs.constants.X_OK);
1601
+ return true;
1602
+ } catch {
1603
+ return false;
1604
+ }
1605
+ };
1606
+ for (const f of fixed) if (runs(f)) return f;
1519
1607
  for (const dir of (process.env.PATH ?? '').split(path.delimiter)) {
1520
1608
  for (const n of names) {
1521
1609
  const f = path.join(dir, n);
1522
- if (dir && fs.existsSync(f)) return f;
1610
+ if (dir && runs(f)) return f;
1523
1611
  }
1524
1612
  }
1525
1613
  return null;
@@ -1527,7 +1615,25 @@ function chromeBinary() {
1527
1615
 
1528
1616
  /** The first line of every file the editor writes into a theme's app copy. */
1529
1617
  export {APP_COPY_MARK} from './_marks.mjs';
1530
- import {APP_COPY_MARK} from './_marks.mjs';
1618
+ import {APP_COPY_MARK, appCopyMark} from './_marks.mjs';
1619
+ import {builtColorMode} from '../../foundation/discovery/theme-imports.mjs';
1620
+
1621
+ /**
1622
+ * The font stylesheets a theme's app copy imports, from the fonts the theme
1623
+ * loads now (an installed font package, the base theme's fonts).
1624
+ * @param {import('./_settings.mjs').Ctx} ctx
1625
+ * @param {string} slug
1626
+ * @param {import('./_settings.mjs').ThemeData} data
1627
+ * @returns {string[]}
1628
+ */
1629
+ export function copyFontsCss(ctx, slug, data) {
1630
+ return themeImports(
1631
+ ctx,
1632
+ slug,
1633
+ data,
1634
+ path.join(ctx.project.cwd, 'src', 'themes', slug),
1635
+ ).css.slice(1);
1636
+ }
1531
1637
 
1532
1638
  /**
1533
1639
  * The app's copy of an editor theme: a standard local theme in
@@ -1545,38 +1651,26 @@ import {APP_COPY_MARK} from './_marks.mjs';
1545
1651
  * @returns {string | null}
1546
1652
  */
1547
1653
  export function writeAppCopy(ctx, slug, data) {
1548
- return writeAppCopyAt(
1549
- ctx.project.cwd,
1550
- slug,
1551
- themeImports(
1552
- ctx,
1553
- slug,
1554
- data,
1555
- path.join(ctx.project.cwd, 'src', 'themes', slug),
1556
- ).css.slice(1),
1557
- );
1654
+ return writeAppCopyAt(ctx.project.cwd, slug, copyFontsCss(ctx, slug, data));
1558
1655
  }
1559
1656
 
1560
1657
  /**
1561
- * {@link writeAppCopy} with the font stylesheets given, or null to keep the
1562
- * copy's own (a compile, which changes no setting).
1658
+ * The files of a theme's app copy as the editor would write them now, by
1659
+ * name; null with no build to copy. Font stylesheets as given, or null for
1660
+ * the copy's own.
1563
1661
  * @param {string} cwd
1564
1662
  * @param {string} slug
1565
1663
  * @param {string[] | null} fontsCss
1566
- * @returns {string | null}
1664
+ * @returns {Record<string, string> | null}
1567
1665
  */
1568
- export function writeAppCopyAt(cwd, slug, fontsCss) {
1666
+ function appCopyFiles(cwd, slug, fontsCss) {
1569
1667
  const f = themeFiles(cwd, slug);
1570
1668
  const dir = path.join(cwd, 'src', 'themes', slug);
1571
1669
  const name = `${camel(slug)}Theme`;
1572
- const first = !fs.existsSync(path.join(dir, `${name}.doc.mjs`));
1573
1670
  if (!fs.existsSync(f.js) || !fs.existsSync(f.css)) return null;
1574
- // The copy's source, as the stable build's header names it.
1575
1671
  const source = `src/themes/${slug}/${name}.ts`;
1576
- // Only the editor's own copy is written over.
1577
- if (appCopyBlocker(cwd, slug) !== null) return null;
1578
1672
  const head = (/** @type {string} */ open, /** @type {string} */ close) =>
1579
- `${open} ${APP_COPY_MARK} after every build of themes/${slug}/; do not edit it here. Change the theme with the editor. ${close}\n`;
1673
+ `${open} ${appCopyMark(slug)} after every build of themes/${slug}/; do not edit it here. Change the theme with the editor. ${close}\n`;
1580
1674
  const keptFonts = path.join(dir, `${slug}.fonts.css`);
1581
1675
  const fonts =
1582
1676
  fontsCss ??
@@ -1598,27 +1692,112 @@ export function writeAppCopyAt(cwd, slug, fontsCss) {
1598
1692
  if (fs.existsSync(from))
1599
1693
  built[`${slug}${ext}`] = asBuilt(fs.readFileSync(from, 'utf8'));
1600
1694
  }
1695
+ // A light-only or dark-only theme's build exports its one color mode
1696
+ // (`colorModes`); the copy's source re-exports it, so a build of the copy
1697
+ // writes it too and `theme add` reads it.
1698
+ const oneMode = builtColorMode(f.js).mode;
1601
1699
  const files = {
1602
1700
  [`${name}.doc.mjs`]: `${head('//', '')}\n/** @type {import('@astryxdesign/cli/authoring').ThemeDoc} */\nexport default {\n type: 'theme',\n name: ${JSON.stringify(slug)},\n displayName: ${JSON.stringify(slug)},\n description: ${JSON.stringify(`The ${slug} theme, made with the theme editor (themes/${slug}/).`)},\n maintained: false,\n};\n`,
1603
- [`${name}.ts`]: `${head('//', '')}export {${name}} from './${slug}';\n`,
1701
+ [`${name}.ts`]: `${head('//', '')}export {${name}${oneMode ? ', colorModes' : ''}} from './${slug}';\n`,
1604
1702
  ...built,
1605
- ...(fonts.length
1606
- ? {
1607
- [`${slug}.fonts.css`]: `${head('/*', '*/')}${fonts.map(c => `@import ${JSON.stringify(c)};`).join('\n')}\n`,
1608
- }
1609
- : {}),
1703
+ // Always there, so the app theme module's import of it holds when the
1704
+ // theme's fonts change (none: a comment only).
1705
+ [`${slug}.fonts.css`]: fonts.length
1706
+ ? `${head('/*', '*/')}${fonts.map(c => `@import ${JSON.stringify(c)};`).join('\n')}\n`
1707
+ : `${head('/*', '*/')}/* No font stylesheets: no installed package loads the theme's fonts. A font the system does not have shows only once its package is installed (experimental theme editor show ${slug} names it). */\n`,
1610
1708
  };
1611
- fs.mkdirSync(dir, {recursive: true});
1709
+ return files;
1710
+ }
1711
+
1712
+ /**
1713
+ * Why a theme's app copy is behind its build, or null when it is current:
1714
+ * something in its way (appCopyStale), a copy file missing, or a file
1715
+ * whose content is not what the editor would write now.
1716
+ * @param {string} cwd
1717
+ * @param {string} slug
1718
+ * @param {string[] | null} [fontsCss] the font stylesheets the copy should
1719
+ * import now (null: the copy's own)
1720
+ * @returns {string | null}
1721
+ */
1722
+ export function appCopyBehind(cwd, slug, fontsCss = null) {
1723
+ const stale = appCopyStale(cwd, slug).appCopyStale;
1724
+ if (stale) return stale;
1725
+ const files = appCopyFiles(cwd, slug, fontsCss);
1726
+ if (!files) return null;
1727
+ const dir = path.join(cwd, 'src', 'themes', slug);
1728
+ if (!fs.existsSync(dir))
1729
+ return `the app copy in src/themes/${slug}/ is gone; the editor writes it again`;
1612
1730
  for (const [n, text] of Object.entries(files)) {
1613
- const file = path.join(dir, n);
1614
- if (fs.existsSync(file) && fs.readFileSync(file, 'utf8') === text) continue;
1615
- writeWhole(file, text);
1731
+ /** @type {string} */
1732
+ let now;
1733
+ try {
1734
+ now = fs.readFileSync(path.join(dir, n), 'utf8');
1735
+ } catch {
1736
+ return `src/themes/${slug}/${n} is missing from the app copy`;
1737
+ }
1738
+ // Line endings as a checkout writes them (CRLF on Windows) are the
1739
+ // same content, as the outputs check reads them.
1740
+ const lf = (/** @type {string} */ x) => x.replace(/\r\n/g, '\n');
1741
+ if (lf(now) !== lf(text))
1742
+ return `src/themes/${slug}/${n} is not the current build of themes/${slug}/`;
1743
+ }
1744
+ return null;
1745
+ }
1746
+
1747
+ /**
1748
+ * {@link writeAppCopy} with the font stylesheets given, or null to keep the
1749
+ * copy's own (a compile, which changes no setting).
1750
+ * @param {string} cwd
1751
+ * @param {string} slug
1752
+ * @param {string[] | null} fontsCss
1753
+ * @returns {string | null}
1754
+ */
1755
+ export function writeAppCopyAt(cwd, slug, fontsCss) {
1756
+ const f = themeFiles(cwd, slug);
1757
+ const dir = path.join(cwd, 'src', 'themes', slug);
1758
+ const name = `${camel(slug)}Theme`;
1759
+ const first = !fs.existsSync(path.join(dir, `${name}.doc.mjs`));
1760
+ if (!fs.existsSync(f.js) || !fs.existsSync(f.css)) return null;
1761
+ // Only the editor's own copy is written over.
1762
+ if (appCopyBlocker(cwd, slug) !== null) return null;
1763
+ const files = appCopyFiles(cwd, slug, fontsCss);
1764
+ if (!files) return null;
1765
+ const failKey = `${path.resolve(cwd)}\0${slug}`;
1766
+ try {
1767
+ fs.mkdirSync(dir, {recursive: true});
1768
+ for (const [n, text] of Object.entries(files)) {
1769
+ const file = path.join(dir, n);
1770
+ if (fs.existsSync(file) && fs.readFileSync(file, 'utf8') === text)
1771
+ continue;
1772
+ writeWhole(file, text);
1773
+ }
1774
+ // A copy's own file this build no longer writes goes; anything else in
1775
+ // the folder (yours, or the system's) stays as it is.
1776
+ for (const n of fs.readdirSync(dir))
1777
+ if (appCopyNames(slug).has(n) && !(n in files))
1778
+ fs.rmSync(path.join(dir, n), {force: true});
1779
+ } catch (e) {
1780
+ // A copy that cannot be written is said, with the system's reason;
1781
+ // the change itself stands.
1782
+ const code = /** @type {any} */ (e)?.code ?? 'error';
1783
+ // The folder that failed: the copy's, or a parent it could not be
1784
+ // made in.
1785
+ const failed = /** @type {any} */ (e)?.path
1786
+ ? path.resolve(String(/** @type {any} */ (e).path))
1787
+ : dir;
1788
+ const at = fs.existsSync(failed) ? failed : path.dirname(failed);
1789
+ const rel = path.relative(cwd, at).split(path.sep).join('/');
1790
+ COPY_FAILED.set(
1791
+ failKey,
1792
+ code === 'EACCES' || code === 'EPERM' || code === 'EROFS'
1793
+ ? `${rel} is not writable (${code})`
1794
+ : code === 'ENOSPC'
1795
+ ? `the disk is full (${code})`
1796
+ : `${rel} could not be written (${code})`,
1797
+ );
1798
+ return null;
1616
1799
  }
1617
- // A file an earlier copy had (a font stylesheet no longer needed) goes;
1618
- // system files stay.
1619
- for (const n of fs.readdirSync(dir))
1620
- if (!(n in files) && !FOLDER_JUNK.has(n))
1621
- fs.rmSync(path.join(dir, n), {force: true});
1800
+ COPY_FAILED.delete(failKey);
1622
1801
  // The first write is named in the command's result; later ones are not.
1623
1802
  if (first)
1624
1803
  FIRST_COPIES.set(
@@ -1633,13 +1812,48 @@ export function writeAppCopyAt(cwd, slug, fontsCss) {
1633
1812
  }
1634
1813
 
1635
1814
  /**
1636
- * Why the editor will not write a theme's app copy, or null when it will:
1637
- * the folder is not there yet, or its descriptor carries the editor's mark
1638
- * and it holds only the copy's own files (system files such as .DS_Store
1639
- * aside). Otherwise the name of what is in the way (a file of yours), or
1640
- * the folder itself when it is not the editor's copy. Built bytes are not
1641
- * compared, so a stable rebuild of the copy leaves it the editor's. The one
1642
- * test the copy's writer and `wiring` both use.
1815
+ * The note for a copy a file of yours keeps from being written, as a
1816
+ * result field: `{appCopyStale}` when one does, else nothing.
1817
+ * @param {string} cwd
1818
+ * @param {string} slug
1819
+ * @returns {{appCopyStale?: string}}
1820
+ */
1821
+ export function appCopyStale(cwd, slug) {
1822
+ const blocker =
1823
+ appCopyBlocker(cwd, slug) ??
1824
+ COPY_FAILED.get(`${path.resolve(cwd)}\0${slug}`) ??
1825
+ null;
1826
+ return blocker
1827
+ ? {
1828
+ appCopyStale: `the app copy in src/themes/${slug}/ was not updated: ${blocker}. The app keeps the theme as it was until that is out of the way; then any set or show writes it.`,
1829
+ }
1830
+ : {};
1831
+ }
1832
+
1833
+ /**
1834
+ * The names of a theme's app copy files.
1835
+ * @param {string} slug
1836
+ * @returns {Set<string>}
1837
+ */
1838
+ function appCopyNames(slug) {
1839
+ const name = `${camel(slug)}Theme`;
1840
+ return new Set([
1841
+ `${name}.doc.mjs`,
1842
+ `${name}.ts`,
1843
+ `${slug}.fonts.css`,
1844
+ ...APP_COPY_BUILT.map(ext => `${slug}${ext}`),
1845
+ ]);
1846
+ }
1847
+
1848
+ /**
1849
+ * Why the editor will not write a theme's app copy, or null when it will.
1850
+ * Only a file of yours AT one of the copy's names stops it: a descriptor,
1851
+ * source or font stylesheet without the editor's mark, or any copy file
1852
+ * while the descriptor is not the editor's. Other files there (a note, a
1853
+ * swap file, desktop.ini) are left as they are and the copy is written
1854
+ * around them. Built bytes are not compared, so a stable rebuild of the
1855
+ * copy leaves it the editor's. The one test the copy's writer, the change
1856
+ * results and `wiring` use. Returns the file in the way.
1643
1857
  * @param {string} cwd
1644
1858
  * @param {string} slug
1645
1859
  * @returns {string | null}
@@ -1648,37 +1862,105 @@ export function appCopyBlocker(cwd, slug) {
1648
1862
  const dir = path.join(cwd, 'src', 'themes', slug);
1649
1863
  const name = `${camel(slug)}Theme`;
1650
1864
  const rel = path.relative(cwd, dir).split(path.sep).join('/');
1865
+ // The copy carries the built module whole, and a local theme imports
1866
+ // only files of its own folder: a module that imports a file beside it
1867
+ // (an icon registry of the theme's folder) would break in the app.
1868
+ {
1869
+ const built = themeFiles(cwd, slug).js;
1870
+ /** @type {string[]} */
1871
+ let specifiers;
1872
+ try {
1873
+ specifiers = themeFileImports(built);
1874
+ } catch {
1875
+ specifiers = [];
1876
+ }
1877
+ const local = specifiers.find(s => s.startsWith('.'));
1878
+ if (local)
1879
+ return `themes/${slug}/${path.basename(built)} imports ${local}, a file beside the theme, which the copy cannot carry (a local theme imports only files of its own folder); import it from a package instead`;
1880
+ }
1881
+ // Nothing is written through a link on the way from the app to the copy
1882
+ // (src/, src/themes/, the copy's folder), as for themes/.
1883
+ for (const part of ['src', 'src/themes', rel]) {
1884
+ try {
1885
+ const st = fs.lstatSync(path.join(cwd, part));
1886
+ if (st.isSymbolicLink())
1887
+ return `${part} is a link (to ${fs.readlinkSync(path.join(cwd, part))}), and the editor writes nothing through a link`;
1888
+ // A file where a folder on the way goes: nothing is half-written.
1889
+ if (!st.isDirectory())
1890
+ return part === rel
1891
+ ? `${rel} is a file, where the copy's folder goes`
1892
+ : `${part} is a file, where a folder on the way to the copy goes`;
1893
+ } catch {
1894
+ break;
1895
+ }
1896
+ }
1897
+ try {
1898
+ if (!fs.lstatSync(dir).isDirectory())
1899
+ return `${rel} is a file, where the copy's folder goes`;
1900
+ } catch {
1901
+ // Not there yet: the copy makes it.
1902
+ }
1651
1903
  if (folderIsEmpty(dir)) return null;
1652
1904
  /** @type {string[]} */
1653
1905
  let names;
1654
1906
  try {
1655
1907
  names = fs.readdirSync(dir);
1656
- } catch {
1657
- return rel;
1658
- }
1659
- /** @type {boolean} */
1660
- let marked;
1661
- try {
1662
- marked = fs
1663
- .readFileSync(path.join(dir, `${name}.doc.mjs`), 'utf8')
1664
- .includes(APP_COPY_MARK);
1665
- } catch {
1666
- marked = false;
1908
+ } catch (e) {
1909
+ return `${rel} cannot be read (${/** @type {any} */ (e)?.code ?? 'error'})`;
1667
1910
  }
1668
- if (!marked) return rel;
1669
- const own = new Set([
1670
- `${name}.doc.mjs`,
1671
- `${name}.ts`,
1672
- `${slug}.fonts.css`,
1673
- ...APP_COPY_BUILT.map(ext => `${slug}${ext}`),
1674
- ]);
1675
- const other = names.find(n => !own.has(n) && !FOLDER_JUNK.has(n));
1676
- return other ? `${rel}/${other}` : null;
1911
+ /** @param {string} n */
1912
+ const marked = n => {
1913
+ try {
1914
+ return fs.readFileSync(path.join(dir, n), 'utf8').includes(APP_COPY_MARK);
1915
+ } catch {
1916
+ return false;
1917
+ }
1918
+ };
1919
+ const own = appCopyNames(slug);
1920
+ const present = names.filter(n => own.has(n));
1921
+ // A link at a copy name is never written through or replaced.
1922
+ for (const n of present)
1923
+ try {
1924
+ const st = fs.lstatSync(path.join(dir, n));
1925
+ if (st.isSymbolicLink())
1926
+ return `${rel}/${n} is a link (to ${fs.readlinkSync(path.join(dir, n))}), at a name the copy uses, and the editor writes nothing through a link`;
1927
+ if (st.isDirectory())
1928
+ return `${rel}/${n} is a folder, at a name the copy uses for a file`;
1929
+ } catch {
1930
+ // Gone since it was listed.
1931
+ }
1932
+ // The copy is the editor's when any of its marked files carries the
1933
+ // mark (one of them deleted is written back); then only a marked name
1934
+ // without the mark is yours.
1935
+ const markedNames = [`${name}.doc.mjs`, `${name}.ts`, `${slug}.fonts.css`];
1936
+ // A build output of the copy's source carries the build's own header.
1937
+ const generated = (/** @type {string} */ n) => {
1938
+ try {
1939
+ return /@generated by `astryx theme build`/.test(
1940
+ fs.readFileSync(path.join(dir, n), 'utf8').slice(0, 600),
1941
+ );
1942
+ } catch {
1943
+ return false;
1944
+ }
1945
+ };
1946
+ const ours =
1947
+ markedNames.some(n => present.includes(n) && marked(n)) ||
1948
+ (present.length > 0 &&
1949
+ present.every(n => !markedNames.includes(n) && generated(n)));
1950
+ const foreign = ours
1951
+ ? present.find(n => markedNames.includes(n) && !marked(n))
1952
+ : present[0];
1953
+ return foreign
1954
+ ? `${rel}/${foreign} is yours, at a name the copy uses, and the editor never writes over your files`
1955
+ : null;
1677
1956
  }
1678
1957
 
1679
1958
  /** The build outputs of a theme, by their ending after `<theme>`. */
1680
1959
  const APP_COPY_BUILT = ['.css', '.css.d.ts', '.js', '.d.ts', '.variants.d.ts'];
1681
1960
 
1961
+ /** Why the last write of a theme's app copy failed, by app and theme. @type {Map<string, string>} */
1962
+ const COPY_FAILED = new Map();
1963
+
1682
1964
  /** App copies written for the first time in this process, by app and theme. @type {Map<string, string[]>} */
1683
1965
  const FIRST_COPIES = new Map();
1684
1966
 
@@ -1695,3 +1977,164 @@ export function takeFirstAppCopy(cwd, slug) {
1695
1977
  FIRST_COPIES.delete(key);
1696
1978
  return wrote;
1697
1979
  }
1980
+
1981
+ /**
1982
+ * Run a picture script (one browser, one picture) as its own process group,
1983
+ * without blocking this process. The one owner of every browser run:
1984
+ * - a temp folder of its own (TMPDIR, TMP, TEMP), where the browser keeps
1985
+ * its profile; with none to make, nothing runs (`noTemp`);
1986
+ * - a time bound (`timedOut`);
1987
+ * - a stop (SIGINT, SIGTERM, SIGHUP) that reaches the run stops it
1988
+ * (`stoppedBy`), then ends this process as the stop would have.
1989
+ * A stop or the bound asks the whole group to close, waits for it (killing
1990
+ * it past a short grace), then removes the temp folder and the `drop` files
1991
+ * (a picture part a stopped run leaves); a run that ends on its own removes
1992
+ * the temp folder only.
1993
+ * @param {string} script a picture script
1994
+ * @param {string[]} args
1995
+ * @param {number} [bound] milliseconds before the run is stopped
1996
+ * @param {{drop?: string[]}} [opts]
1997
+ * @returns {Promise<{status: number | null, signal: string | null, stderr: string, error?: Error, stoppedBy?: string, timedOut?: boolean, noTemp?: string}>}
1998
+ */
1999
+ export function runBrowser(script, args, bound = BROWSER_BOUND_MS, opts = {}) {
2000
+ const drop = opts.drop ?? [];
2001
+ /** @type {string} */
2002
+ let temp;
2003
+ try {
2004
+ temp = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-browser-'));
2005
+ } catch (e) {
2006
+ return Promise.resolve({
2007
+ status: null,
2008
+ signal: null,
2009
+ stderr: '',
2010
+ noTemp: String(/** @type {any} */ (e)?.code ?? e),
2011
+ });
2012
+ }
2013
+ return new Promise(resolve => {
2014
+ let stderr = '';
2015
+ const posix = process.platform !== 'win32';
2016
+ const child = spawn(process.execPath, [script, ...args], {
2017
+ stdio: ['ignore', 'ignore', 'pipe'],
2018
+ env: {...process.env, TMPDIR: temp, TMP: temp, TEMP: temp},
2019
+ // A group of its own, so a stop reaches the browser it starts too.
2020
+ detached: posix,
2021
+ });
2022
+ /** @param {NodeJS.Signals} sig */
2023
+ const signalAll = sig => {
2024
+ try {
2025
+ if (posix && child.pid) process.kill(-child.pid, sig);
2026
+ else child.kill(sig);
2027
+ } catch {
2028
+ // Gone already.
2029
+ }
2030
+ };
2031
+ const closed = new Promise(done => child.once('close', done));
2032
+ /** @param {string[]} files */
2033
+ const remove = files => {
2034
+ for (const f of files)
2035
+ try {
2036
+ fs.rmSync(f, {recursive: true, force: true});
2037
+ } catch {
2038
+ // Gone already.
2039
+ }
2040
+ };
2041
+ /** The temp folder goes on every way out; `drop` only with a stop. */
2042
+ const clean = () => remove([temp]);
2043
+ /** Close the group (killed past the grace), then clean up. */
2044
+ const finish = async () => {
2045
+ signalAll('SIGTERM');
2046
+ const grace = new Promise(done =>
2047
+ setTimeout(done, STOP_GRACE_MS).unref(),
2048
+ );
2049
+ if ((await Promise.race([closed.then(() => true), grace])) !== true) {
2050
+ signalAll('SIGKILL');
2051
+ await closed;
2052
+ }
2053
+ remove([temp, ...drop]);
2054
+ };
2055
+ // This process ending some other way: the group is killed, and the temp
2056
+ // folder and the unfinished files removed, as far as an exit allows.
2057
+ const onExit = () => {
2058
+ signalAll('SIGKILL');
2059
+ remove([temp, ...drop]);
2060
+ };
2061
+ process.once('exit', onExit);
2062
+ child.stderr?.setEncoding('utf8');
2063
+ child.stderr?.on('data', d => {
2064
+ if (stderr.length < 8000) stderr += d;
2065
+ });
2066
+ /** Set once the run is being stopped (bound, signal): it ends there. */
2067
+ let stopping = false;
2068
+ let settled = false;
2069
+ /** @param {Awaited<ReturnType<typeof runBrowser>>} out */
2070
+ const settle = out => {
2071
+ if (settled) return;
2072
+ settled = true;
2073
+ clearTimeout(timer);
2074
+ process.removeListener('exit', onExit);
2075
+ for (const sig of STOP_SIGNALS) process.removeListener(sig, onStop);
2076
+ resolve(out);
2077
+ };
2078
+ const timer = setTimeout(() => {
2079
+ stopping = true;
2080
+ void finish().then(() =>
2081
+ settle({status: null, signal: null, stderr, timedOut: true}),
2082
+ );
2083
+ }, bound);
2084
+ /** @param {NodeJS.Signals} sig */
2085
+ const onStop = async sig => {
2086
+ stopping = true;
2087
+ for (const s of STOP_SIGNALS) process.removeListener(s, onStop);
2088
+ // A second stop during the grace does not wait for it: the group is
2089
+ // killed and the files removed at once, then the process ends.
2090
+ /** @param {NodeJS.Signals} again */
2091
+ const onAgain = again => {
2092
+ for (const s of STOP_SIGNALS) process.removeListener(s, onAgain);
2093
+ signalAll('SIGKILL');
2094
+ remove([temp, ...drop]);
2095
+ if (process.listenerCount(again) === 0)
2096
+ process.kill(process.pid, again);
2097
+ };
2098
+ for (const s of STOP_SIGNALS) process.once(s, onAgain);
2099
+ await finish();
2100
+ for (const s of STOP_SIGNALS) process.removeListener(s, onAgain);
2101
+ settle({status: null, signal: null, stderr, stoppedBy: sig});
2102
+ // Nothing else listening: the stop ends this process, as it would
2103
+ // have without this listener.
2104
+ if (process.listenerCount(sig) === 0) process.kill(process.pid, sig);
2105
+ };
2106
+ for (const sig of STOP_SIGNALS) process.once(sig, onStop);
2107
+ child.once('error', error => {
2108
+ clean();
2109
+ settle({status: null, signal: null, stderr, error});
2110
+ });
2111
+ child.once('close', (status, signal) => {
2112
+ if (stopping) return;
2113
+ clean();
2114
+ settle({status, signal, stderr});
2115
+ });
2116
+ });
2117
+ }
2118
+
2119
+ /**
2120
+ * The first stderr line that says something about the picture: Chrome's
2121
+ * startup warnings (dbus, GPU, sandbox notices) come first on Linux and
2122
+ * say nothing about why no picture was written.
2123
+ * @param {string} text
2124
+ */
2125
+ export function meaningfulLine(text) {
2126
+ const lines = String(text ?? '')
2127
+ .split(/\r?\n/)
2128
+ .map(l => l.trim())
2129
+ .filter(Boolean);
2130
+ return (
2131
+ lines.find(
2132
+ l =>
2133
+ !/dbus|bus\.cc|gpu_|gl_|sandbox|fontconfig|crashpad|DevTools listening|WARNING:/i.test(
2134
+ l,
2135
+ ),
2136
+ ) ??
2137
+ lines[0] ??
2138
+ ''
2139
+ );
2140
+ }