@astryxdesign/cli 0.7.16 → 0.7.17-canary.g49a475f

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 (354) 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 +75 -0
  79. package/api/theme/build/build.mjs +243 -58
  80. package/api/theme/build/font-warning.d.mts +7 -0
  81. package/api/theme/build/font-warning.mjs +17 -0
  82. package/api/theme/theme.type.d.mts +10 -0
  83. package/api/theme/theme.type.mjs +7 -1
  84. package/api/theme-edit/_adapter.d.mts +63 -2
  85. package/api/theme-edit/_adapter.mjs +771 -57
  86. package/api/theme-edit/_check.d.mts +40 -22
  87. package/api/theme-edit/_check.mjs +2029 -336
  88. package/api/theme-edit/_color.d.mts +10 -0
  89. package/api/theme-edit/_color.mjs +49 -1
  90. package/api/theme-edit/_compiler.mjs +9 -0
  91. package/api/theme-edit/_context.d.mts +3 -3
  92. package/api/theme-edit/_context.mjs +111 -48
  93. package/api/theme-edit/_css.d.mts +14 -0
  94. package/api/theme-edit/_css.mjs +46 -1
  95. package/api/theme-edit/_edit.d.mts +18 -4
  96. package/api/theme-edit/_edit.mjs +1685 -155
  97. package/api/theme-edit/_how-commands.d.mts +65 -0
  98. package/api/theme-edit/_how-commands.mjs +633 -1
  99. package/api/theme-edit/_how-question.d.mts +84 -0
  100. package/api/theme-edit/_how-question.mjs +5124 -281
  101. package/api/theme-edit/_how.mjs +140 -29
  102. package/api/theme-edit/_marks.d.mts +31 -5
  103. package/api/theme-edit/_marks.mjs +57 -5
  104. package/api/theme-edit/_name-rule.d.mts +11 -0
  105. package/api/theme-edit/_name-rule.mjs +12 -0
  106. package/api/theme-edit/_normal.d.mts +257 -0
  107. package/api/theme-edit/_normal.mjs +1252 -0
  108. package/api/theme-edit/_palette.mjs +10 -4
  109. package/api/theme-edit/_parts.d.mts +10 -1
  110. package/api/theme-edit/_parts.mjs +179 -3
  111. package/api/theme-edit/_preview-app.d.mts +7 -1
  112. package/api/theme-edit/_preview-app.mjs +515 -148
  113. package/api/theme-edit/_preview.d.mts +100 -14
  114. package/api/theme-edit/_preview.mjs +549 -137
  115. package/api/theme-edit/_settings.d.mts +347 -30
  116. package/api/theme-edit/_settings.mjs +2971 -327
  117. package/api/theme-edit/_tables.d.mts +6 -0
  118. package/api/theme-edit/_tables.mjs +44 -1
  119. package/api/theme-edit/_vet.d.mts +18 -0
  120. package/api/theme-edit/_vet.mjs +32 -0
  121. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  122. package/api/theme-edit/theme-edit.type.mjs +4 -1
  123. package/api/theme-edit/themeEditHow.doc.mjs +1 -1
  124. package/api/theme-edit/themeEditNew.doc.d.mts +0 -1
  125. package/api/theme-edit/themeEditNew.doc.mjs +7 -4
  126. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  127. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -2
  128. package/api/upgrade/run/run.mjs +4 -3
  129. package/assets/codemods/integration-discovery.mjs +2 -1
  130. package/assets/codemods/run-codemod.mjs +87 -7
  131. package/assets/codemods/transforms/next/index.mjs +11 -1
  132. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  133. package/assets/component-list.generated.json +1 -0
  134. package/assets/docs/author-a-theme.doc.mjs +1 -0
  135. package/assets/docs/theme.doc.dense.mjs +2 -2
  136. package/assets/docs/theme.doc.mjs +1 -1
  137. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  138. package/assets/docs/tree/checks.doc.mjs +1 -1
  139. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  140. package/assets/docs/tree/render-pages.doc.mjs +44 -3
  141. package/assets/docs/tree/single-component.doc.mjs +4 -0
  142. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  143. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  144. package/assets/docs/use-a-theme.doc.mjs +1 -1
  145. package/assets/docs.generated.json +2 -2
  146. package/assets/search-catalog.generated.json +2 -0
  147. package/assets/self-audit.generated.json +1 -1
  148. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  149. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  150. package/assets/templates/blocks/components/Hooks/useContainerRevealHookUsage.tsx +2 -2
  151. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  152. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  153. package/assets/templates/components.generated.json +1 -1
  154. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  155. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  156. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  157. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  158. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  159. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  160. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  161. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  162. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  163. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  164. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  165. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  166. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  167. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  168. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  169. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  170. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  171. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  172. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  173. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  174. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  175. package/assets/templates/pages/ide/page.tsx +24 -2
  176. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  177. package/assets/templates/pages/library/template.doc.mjs +4 -1
  178. package/assets/templates/pages/login/template.doc.mjs +2 -1
  179. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  180. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  181. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  182. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  183. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  184. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  185. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  186. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  187. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  188. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  189. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  190. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  191. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  192. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  193. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  194. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  195. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  196. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  197. package/assets/theme.template.ts +6 -0
  198. package/authoring/debug/parse.d.mts +3 -3
  199. package/authoring/doctypes/_schema.d.mts +3 -3
  200. package/authoring/doctypes/command/command.doc.mjs +1 -1
  201. package/authoring/doctypes/component/categories.d.mts +37 -0
  202. package/authoring/doctypes/component/categories.mjs +54 -0
  203. package/authoring/doctypes/template/template.doc.mjs +1 -1
  204. package/authoring/doctypes/template/type.ts +9 -0
  205. package/authoring/example/example.doc.d.mts +12 -0
  206. package/authoring/example/example.doc.mjs +69 -0
  207. package/authoring/example/preview.d.mts +92 -0
  208. package/authoring/example/preview.mjs +994 -0
  209. package/authoring/example/type.ts +102 -0
  210. package/authoring/index.d.mts +1 -0
  211. package/authoring/index.d.ts +12 -0
  212. package/authoring/index.mjs +3 -1
  213. package/clients/cli/bin/astryx.mjs +6 -9
  214. package/clients/cli/build-id.generated.json +1 -1
  215. package/clients/cli/commands/component.doc.mjs +1 -1
  216. package/clients/cli/commands/component.mjs +22 -3
  217. package/clients/cli/commands/docs.doc.mjs +7 -1
  218. package/clients/cli/commands/docs.mjs +3 -1
  219. package/clients/cli/commands/doctor.mjs +32 -14
  220. package/clients/cli/commands/experimental/render.mjs +42 -1
  221. package/clients/cli/commands/experimental/theme.mjs +163 -28
  222. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  223. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  224. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +87 -1
  225. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +10 -3
  226. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -1
  227. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  228. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  229. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  230. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  231. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
  232. package/clients/cli/commands/experimental-theme-preview.doc.mjs +2 -1
  233. package/clients/cli/commands/experimental-theme.doc.mjs +5 -1
  234. package/clients/cli/commands/search.doc.mjs +2 -1
  235. package/clients/cli/commands/search.mjs +6 -1
  236. package/clients/cli/commands/template.mjs +4 -2
  237. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  238. package/clients/cli/commands/theme.doc.mjs +1 -1
  239. package/clients/cli/commands/theme.mjs +209 -8
  240. package/clients/cli/commands/upgrade.mjs +42 -7
  241. package/clients/cli/index.mjs +1 -1
  242. package/clients/cli/lib/cli-error.mjs +2 -1
  243. package/clients/cli/lib/component-format.mjs +205 -98
  244. package/clients/cli/lib/hook-format.mjs +9 -19
  245. package/clients/cli/lib/json-shim.mjs +77 -21
  246. package/clients/cli/lib/manifest.mjs +3 -2
  247. package/clients/cli/manifest.generated.json +1 -1
  248. package/foundation/agent-docs/agent-docs.mjs +1 -1
  249. package/foundation/config/theme-state.d.mts +13 -1
  250. package/foundation/config/theme-state.mjs +47 -7
  251. package/foundation/discovery/authoring-self-docs.mjs +1 -0
  252. package/foundation/discovery/component-discovery.d.mts +11 -0
  253. package/foundation/discovery/component-discovery.mjs +56 -8
  254. package/foundation/discovery/component-replacement.mjs +130 -1
  255. package/foundation/discovery/copied-passages.mjs +2 -1
  256. package/foundation/discovery/doc-types.mjs +4 -3
  257. package/foundation/discovery/entry-name-files.d.mts +43 -0
  258. package/foundation/discovery/entry-name-files.mjs +264 -0
  259. package/foundation/discovery/hook-discovery.d.mts +8 -2
  260. package/foundation/discovery/hook-discovery.mjs +22 -8
  261. package/foundation/discovery/icon-definition-discovery.mjs +2 -1
  262. package/foundation/discovery/icon-set-checks.mjs +2 -1
  263. package/foundation/discovery/icon-set-discovery.mjs +2 -1
  264. package/foundation/discovery/integration-doc-files.mjs +2 -1
  265. package/foundation/discovery/package-exports.d.mts +10 -0
  266. package/foundation/discovery/package-exports.mjs +56 -3
  267. package/foundation/discovery/prop-drift.mjs +9 -9
  268. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  269. package/foundation/discovery/runner-allowlist.mjs +2 -0
  270. package/foundation/discovery/stored-component-list.d.mts +43 -0
  271. package/foundation/discovery/stored-component-list.mjs +90 -0
  272. package/foundation/discovery/stored-search-catalog.d.mts +115 -0
  273. package/foundation/discovery/stored-search-catalog.mjs +297 -0
  274. package/foundation/discovery/stored-self-audit.d.mts +26 -10
  275. package/foundation/discovery/stored-self-audit.mjs +38 -18
  276. package/foundation/discovery/template-adapter.d.mts +52 -0
  277. package/foundation/discovery/template-adapter.mjs +176 -12
  278. package/foundation/discovery/template-icons.mjs +2 -1
  279. package/foundation/discovery/theme-discovery.mjs +3 -2
  280. package/foundation/discovery/theme-imports.d.mts +14 -0
  281. package/foundation/discovery/theme-imports.mjs +43 -1
  282. package/foundation/discovery/theming-targets.mjs +3 -2
  283. package/foundation/doc-compiler/import.mjs +7 -1
  284. package/foundation/doc-compiler/inputs.mjs +3 -2
  285. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  286. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  287. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  288. package/foundation/env/semver.mjs +3 -1
  289. package/foundation/fonts/font-stylesheet.d.mts +31 -0
  290. package/foundation/fonts/font-stylesheet.mjs +115 -0
  291. package/foundation/fs/cache-eviction.d.mts +21 -0
  292. package/foundation/fs/cache-eviction.mjs +73 -0
  293. package/foundation/fs/file-protection.mjs +4 -3
  294. package/foundation/fs/gather-cache.d.mts +25 -4
  295. package/foundation/fs/gather-cache.mjs +164 -55
  296. package/foundation/fs/jiti-cache.d.mts +19 -1
  297. package/foundation/fs/jiti-cache.mjs +130 -6
  298. package/foundation/fs/kept-tree.d.mts +60 -0
  299. package/foundation/fs/kept-tree.mjs +154 -0
  300. package/foundation/fs/listing-cache.d.mts +7 -0
  301. package/foundation/fs/listing-cache.mjs +10 -0
  302. package/foundation/fs/module-loader.d.mts +24 -2
  303. package/foundation/fs/module-loader.mjs +94 -21
  304. package/foundation/fs/paths.d.mts +18 -0
  305. package/foundation/fs/paths.mjs +15 -2
  306. package/foundation/identity/provider-identity.d.mts +6 -0
  307. package/foundation/identity/provider-identity.mjs +10 -1
  308. package/foundation/integrations/contribution-inventory.mjs +4 -3
  309. package/foundation/integrations/example-code.d.mts +63 -0
  310. package/foundation/integrations/example-code.mjs +416 -0
  311. package/foundation/integrations/public-imports.d.mts +9 -0
  312. package/foundation/integrations/public-imports.mjs +3 -2
  313. package/foundation/integrations/validate-contributions.mjs +17 -45
  314. package/foundation/render/embedded-files.d.mts +6 -0
  315. package/foundation/render/embedded-files.mjs +25 -5
  316. package/foundation/render/embedded-imports.d.mts +39 -7
  317. package/foundation/render/embedded-imports.mjs +66 -27
  318. package/foundation/render/file-header.d.mts +66 -0
  319. package/foundation/render/file-header.mjs +118 -0
  320. package/foundation/render/host-answer.d.mts +18 -0
  321. package/foundation/render/host-answer.mjs +46 -0
  322. package/foundation/render/host-exit.d.mts +1 -0
  323. package/foundation/render/host-exit.mjs +6 -1
  324. package/foundation/render/html-reader.d.mts +55 -2
  325. package/foundation/render/html-reader.mjs +145 -26
  326. package/foundation/render/markup-edits.d.mts +57 -0
  327. package/foundation/render/markup-edits.mjs +182 -0
  328. package/foundation/render/page-host.mjs +29 -12
  329. package/foundation/render/page-worker.mjs +121 -13
  330. package/foundation/render/picture-chrome.d.mts +4 -0
  331. package/foundation/render/picture-chrome.mjs +185 -0
  332. package/foundation/render/picture-playwright.d.mts +4 -0
  333. package/foundation/render/picture-playwright.mjs +34 -0
  334. package/foundation/render/rebuild.mjs +2 -1
  335. package/foundation/render/render-page.d.mts +35 -3
  336. package/foundation/render/render-page.mjs +316 -42
  337. package/foundation/render/source-block.d.mts +85 -6
  338. package/foundation/render/source-block.mjs +211 -20
  339. package/foundation/render/ts-paths.d.mts +69 -6
  340. package/foundation/render/ts-paths.mjs +326 -40
  341. package/foundation/response/error-codes.d.mts +3 -1
  342. package/foundation/response/error-codes.d.ts +1 -0
  343. package/foundation/response/error-codes.doc.mjs +12 -1
  344. package/foundation/response/error-codes.mjs +11 -1
  345. package/foundation/response/response-types.doc.mjs +2 -2
  346. package/foundation/text/compare-text.d.mts +14 -0
  347. package/foundation/text/compare-text.mjs +68 -0
  348. package/foundation/text/string-utils.d.mts +3 -2
  349. package/foundation/text/string-utils.mjs +18 -4
  350. package/foundation/text/suggest-order.d.mts +9 -0
  351. package/foundation/text/suggest-order.mjs +15 -1
  352. package/foundation/xle/expand.mjs +2 -1
  353. package/host/index.mjs +8 -6
  354. package/package.json +7 -6
@@ -32,9 +32,15 @@
32
32
  */
33
33
 
34
34
  import {writeThemeTemplate} from '../theme/template/template.mjs';
35
- import {buildSuggestion} from '../theme/build/build.mjs';
35
+ import {
36
+ buildInputProblem,
37
+ buildSuggestion,
38
+ realLocation,
39
+ refusedOutput,
40
+ } from '../theme/build/build.mjs';
36
41
  import {createHash} from 'node:crypto';
37
42
  import * as fs from 'node:fs';
43
+ import {createRequire} from 'node:module';
38
44
  import * as path from 'node:path';
39
45
  import * as edit from './_edit.mjs';
40
46
  import {baseInstalled, themeCacheRoot} from './_project.mjs';
@@ -42,7 +48,12 @@ import * as preview from './_preview-app.mjs';
42
48
  import {AstryxError} from '../error.mjs';
43
49
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
44
50
  import {findPackageRoot} from '../../foundation/fs/paths.mjs';
45
- import {coreApp} from './_context.mjs';
51
+ import {context, coreApp} from './_context.mjs';
52
+ import {APP_COPY_MARK} from './_marks.mjs';
53
+ import {
54
+ fontKey,
55
+ importedFontFamilies,
56
+ } from '../../foundation/fonts/font-stylesheet.mjs';
46
57
  import {isOutsideProject} from '../../foundation/env/project-scope.mjs';
47
58
  import {withFolderLock} from '../../foundation/release/folder-lock.mjs';
48
59
  import {
@@ -50,15 +61,33 @@ import {
50
61
  quoteCommandArgument,
51
62
  } from '../../foundation/env/package-manager.mjs';
52
63
  import {LIKE, P} from './_tables.mjs';
64
+
65
+ /** The preview command, as a person runs it. */
66
+ const PREVIEW_WORDS = P.replace(/ editor$/, ' preview');
53
67
  import {setLine} from './_check.mjs';
68
+ import {followLine, splitCommand} from './_vet.mjs';
54
69
  import {
55
70
  SHOWCASE_GROUPS,
71
+ appCopyBehind,
72
+ appCopyStale,
73
+ copyFontsCss,
56
74
  takeFirstAppCopy,
57
75
  writeAppCopy,
58
76
  writeAppCopyAt,
59
77
  } from './_preview.mjs';
60
78
  import {humanWarn} from '../../foundation/response/json.mjs';
61
- import {folderIsEmpty, setAfterBuild, typedThemeName} from './_settings.mjs';
79
+ import {
80
+ buildColorModes,
81
+ camel,
82
+ folderIsEmpty,
83
+ loadTheme,
84
+ setAfterBuild,
85
+ editedThemes,
86
+ themeFiles,
87
+ fontsourcePackageOf,
88
+ themeSourceRefusal,
89
+ typedThemeName,
90
+ } from './_settings.mjs';
62
91
 
63
92
  // Every build of a theme writes its app copy (src/themes/<theme>/).
64
93
  setAfterBuild(writeAppCopy);
@@ -367,16 +396,43 @@ const lockName = name =>
367
396
  */
368
397
  function absolute(result, root, from) {
369
398
  if (path.resolve(from) === root) return result;
370
- /** @param {any} v @returns {any} */
371
- const map = v =>
399
+ /**
400
+ * Lines that name project files inside them: `see`, a picture note, the
401
+ * in-app preview's stale entries. Every project path in them (a theme
402
+ * file, an in-app preview folder) is made absolute, one style per line.
403
+ */
404
+ const PROSE = new Set(['see', 'pictureProblem', 'previewStale', 'wiring']);
405
+ /** A project-relative path that names a file or folder there. @param {string} p */
406
+ const inRoot = p => {
407
+ const bare = p.replace(/\/+$/, '');
408
+ return (
409
+ Boolean(bare) &&
410
+ !path.isAbsolute(bare) &&
411
+ !bare.startsWith('.') &&
412
+ fs.existsSync(path.join(root, bare))
413
+ );
414
+ };
415
+ /** @param {any} v @param {string} [key] @returns {any} */
416
+ const map = (v, key) =>
372
417
  typeof v === 'string'
373
- ? /^themes\//.test(v)
418
+ ? // A path alone; a line that starts with one is read as prose below.
419
+ /^themes\/\S*$/.test(v) && !(key && PROSE.has(key))
374
420
  ? path.join(root, v)
375
- : v
421
+ : key && PROSE.has(key)
422
+ ? v.replace(
423
+ /(^|[\s(,])((?:themes|src|app)\/[^\s,;)]*)/g,
424
+ (all, before, file) =>
425
+ /^themes\//.test(file) || inRoot(file)
426
+ ? `${before}${path.join(root, file)}`
427
+ : all,
428
+ )
429
+ : v
376
430
  : Array.isArray(v)
377
- ? v.map(map)
431
+ ? v.map(x => map(x, key))
378
432
  : v && typeof v === 'object'
379
- ? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, map(x)]))
433
+ ? Object.fromEntries(
434
+ Object.entries(v).map(([k, x]) => [k, map(x, k)]),
435
+ )
380
436
  : v;
381
437
  return map(result);
382
438
  }
@@ -550,37 +606,117 @@ export const themeEditShow = (theme, options = {}) =>
550
606
  );
551
607
  /** @type {typeof preview.themeEditPreviewAdd} */
552
608
  export const themeEditPreviewAdd = (description, options = {}) =>
553
- inProject(options, 'preview', o =>
554
- preview.themeEditPreviewAdd(description, withThemeLock(o)),
609
+ stopTold(
610
+ `a preview change stopped part way is finished by the next ${PREVIEW_WORDS} add, and one written in full stands (${PREVIEW_WORDS} list says which)`,
611
+ () =>
612
+ inProject(options, 'preview', o =>
613
+ preview.themeEditPreviewAdd(description, withThemeLock(o)),
614
+ ),
555
615
  );
616
+
617
+ /**
618
+ * Run a preview command with a stop (Ctrl-C, SIGTERM) heard for its whole
619
+ * run, and after it: the stop ends the process as stopped, with one line
620
+ * saying where the change stands (a change stopped part way is finished by
621
+ * the next preview command), never an ok with exit 0.
622
+ * @template T
623
+ * @param {string} where where the change stands, and what finishes it
624
+ * @param {() => Promise<T>} fn
625
+ * @returns {Promise<T>}
626
+ */
627
+ async function stopTold(where, fn) {
628
+ const on = (/** @type {NodeJS.Signals} */ sig) => {
629
+ process.removeListener('SIGINT', on);
630
+ process.removeListener('SIGTERM', on);
631
+ process.stderr.write(`Stopped: ${where}.\n`);
632
+ // With no other listener (a lock's, which ends the process itself),
633
+ // the stop ends it here.
634
+ if (process.listenerCount(sig) === 0) process.kill(process.pid, sig);
635
+ };
636
+ process.on('SIGINT', on);
637
+ process.on('SIGTERM', on);
638
+ try {
639
+ const r = await fn();
640
+ // A turn of the event loop, so a stop that came while the work ran
641
+ // without a pause is heard now, before the result is printed.
642
+ await new Promise(resolve => setImmediate(resolve));
643
+ // Every listener added here is removed on return: the caller's process
644
+ // keeps its own stop handling. The CLI client, which owns its process,
645
+ // holds a stop that lands while it prints the result.
646
+ return r;
647
+ } finally {
648
+ process.removeListener('SIGINT', on);
649
+ process.removeListener('SIGTERM', on);
650
+ }
651
+ }
556
652
  /**
557
653
  * `preview app` runs from the project root, with `dir` kept pointing where
558
654
  * it was given. With a theme it holds that theme's lock, since it may build
559
655
  * the theme and copies its files: a `set` beside it waits.
560
656
  * @type {typeof preview.themeEditPreviewApp}
561
657
  */
562
- export const themeEditPreviewApp = (dir, options = {}) => {
658
+ export const themeEditPreviewApp = async (dir, options = {}) => {
563
659
  const from = options.cwd ?? process.cwd();
564
- return inProject(
565
- options,
566
- options.theme == null ? null : lockName(options.theme),
567
- o =>
568
- preview.themeEditPreviewApp(
569
- typeof dir === 'string' && dir.trim() && dir === dir.trim()
570
- ? path.relative(o.cwd, path.resolve(from, dir)) || '.'
571
- : dir,
572
- o,
573
- ),
574
- true,
660
+ return fromHere(
661
+ await stopTold(
662
+ `the new app may be part made; ${PREVIEW_WORDS} app with a new folder name makes a whole one`,
663
+ () =>
664
+ inProject(
665
+ options,
666
+ options.theme == null ? null : lockName(options.theme),
667
+ o =>
668
+ preview.themeEditPreviewApp(
669
+ typeof dir === 'string' && dir.trim() && dir === dir.trim()
670
+ ? path.relative(o.cwd, path.resolve(from, dir)) || '.'
671
+ : dir,
672
+ o,
673
+ ),
674
+ true,
675
+ ),
676
+ ),
677
+ from,
575
678
  );
576
679
  };
680
+ /**
681
+ * A new app's folder and its open line, as they work from where the command
682
+ * ran (`cd ../../theme-app` from src/app), not from the project root.
683
+ * @template T
684
+ * @param {T} r
685
+ * @param {string} from
686
+ * @returns {T}
687
+ */
688
+ function fromHere(r, from) {
689
+ const d = /** @type {any} */ (r)?.data;
690
+ if (!d || typeof d.folder !== 'string') return r;
691
+ const root = projectRoot(from);
692
+ if (!root || path.resolve(from) === root) return r;
693
+ const here = path.relative(from, path.join(root, d.folder)) || '.';
694
+ const shown = here.split(path.sep).join('/');
695
+ const cdArg = quoteCommandArgument(
696
+ shown.startsWith('-') ? `./${shown}` : shown,
697
+ );
698
+ return /** @type {T} */ ({
699
+ ...r,
700
+ data: {
701
+ ...d,
702
+ folder: shown,
703
+ ...(typeof d.open === 'string'
704
+ ? {open: d.open.replace(/^cd \S+ &&/, `cd ${cdArg} &&`)}
705
+ : {}),
706
+ },
707
+ });
708
+ }
577
709
  /** @type {typeof preview.themeEditPreviewList} */
578
710
  export const themeEditPreviewList = (options = {}) =>
579
711
  inProject(options, null, async o => preview.themeEditPreviewList(o));
580
712
  /** @type {typeof preview.themeEditPreviewRemove} */
581
713
  export const themeEditPreviewRemove = (page, options = {}) =>
582
- inProject(options, 'preview', o =>
583
- preview.themeEditPreviewRemove(page, withThemeLock(o)),
714
+ stopTold(
715
+ `a removal stopped part way is finished by the next ${PREVIEW_WORDS} remove, and one made in full stands (${PREVIEW_WORDS} list says which)`,
716
+ () =>
717
+ inProject(options, 'preview', o =>
718
+ preview.themeEditPreviewRemove(page, withThemeLock(o)),
719
+ ),
584
720
  );
585
721
 
586
722
  /**
@@ -669,8 +805,9 @@ function runLine(cwd, rest) {
669
805
  * @returns {AstryxError | null}
670
806
  */
671
807
  export function editorThemeRefusal(err, cwd) {
672
- // A refused compile ends here: its theme locks go now, not at exit.
673
- void releaseCompileHold?.();
808
+ // A refused compile ends here: its theme locks go now, not at exit, and
809
+ // it writes no app copy (from build files it refused).
810
+ void releaseCompileHold?.(true);
674
811
  if (
675
812
  err.code !== ERROR_CODES.ERR_UNKNOWN_THEME &&
676
813
  !/unknown theme|is not added|not a theme package/i.test(err.message)
@@ -683,10 +820,43 @@ export function editorThemeRefusal(err, cwd) {
683
820
  const word = named ? typedThemeName(named[1] ?? named[2]) : '';
684
821
  const slug = word ? editorThemeOf(root, word) : null;
685
822
  if (!slug) return null;
823
+ // The command that refused: list, add, use, remove or eject.
824
+ const verb =
825
+ process.argv
826
+ .slice(2)
827
+ .find(a => ['list', 'add', 'use', 'remove', 'eject'].includes(a)) ?? null;
828
+ // eject copies a theme package's source into the app; an editor theme's
829
+ // source is the app's already.
830
+ if (verb === 'eject')
831
+ return new AstryxError(
832
+ `${slug} is a theme the editor made: its source is yours already, themes/${slug}/${slug}.theme.ts (your edits there survive every change), and the editor changes the rest. eject copies a theme package's source into the app, so it has nothing to do for ${slug}.`,
833
+ [
834
+ {
835
+ name: runLine(cwd, `experimental theme editor show ${slug}`),
836
+ reason: 'the theme as it is now',
837
+ },
838
+ ],
839
+ ERROR_CODES.ERR_UNKNOWN_THEME,
840
+ );
841
+ const copyThere = !folderIsEmpty(path.join(root, LOCAL_THEME_ROOT, slug));
842
+ // A path to the theme ("themes/d", "src/themes/d"): the commands take its
843
+ // name.
844
+ const typed = named ? (named[1] ?? named[2]) : '';
845
+ if (copyThere && verb && typed !== slug)
846
+ return new AstryxError(
847
+ `"${typed}" is ${/[\\/]|\.[a-z]+$/i.test(typed) ? 'a path' : "not the theme's name"}; the theme commands take the theme's name: ${slug}.`,
848
+ [
849
+ {
850
+ name: runLine(cwd, `theme ${verb} ${slug}`),
851
+ reason: 'the same, by name',
852
+ },
853
+ ],
854
+ ERROR_CODES.ERR_UNKNOWN_THEME,
855
+ );
686
856
  // A local theme of that name (a fork in the app's own themes folder) is
687
857
  // one the stable commands take: their own message stands.
688
858
  // A folder of only system files (or empty) holds no theme: as absent.
689
- if (!folderIsEmpty(path.join(root, LOCAL_THEME_ROOT, slug))) return null;
859
+ if (copyThere) return null;
690
860
  // No app copy yet (a theme made before the editor wrote one, or the
691
861
  // copy removed): the next build writes it, then the same steps every
692
862
  // place gives (`wiring` of new and show).
@@ -722,6 +892,112 @@ export function editorThemeRefusal(err, cwd) {
722
892
  );
723
893
  }
724
894
 
895
+ /** The shared typo rule, for a typed subcommand. */
896
+ export {typoOf} from './_normal.mjs';
897
+
898
+ /**
899
+ * The editor themes of the app the folder `cwd` is in, by name; none when
900
+ * the folder is in no app or its themes cannot be read.
901
+ * @param {string} cwd
902
+ * @returns {string[]}
903
+ */
904
+ export function editorThemesAt(cwd) {
905
+ try {
906
+ return editedThemes(projectRoot(cwd));
907
+ } catch {
908
+ return [];
909
+ }
910
+ }
911
+
912
+ /**
913
+ * A refused compile whose first suggestion is another compile: that compile
914
+ * is run through compile's own checks before it is offered (in memory,
915
+ * nothing built), and each repair a refusal of it offers is folded into the
916
+ * one line. An --out outside the working folder is dropped, and the reason
917
+ * says why; when the rest cannot be repaired, the reason says so. A line
918
+ * that starts with `cd` runs elsewhere, so it is not checked from here.
919
+ * @param {AstryxError} err
920
+ * @param {string} cwd
921
+ * @returns {Promise<AstryxError>}
922
+ */
923
+ export async function editorFollowCompile(err, cwd) {
924
+ const list = err.suggestions ?? [];
925
+ const first = list[0];
926
+ // The build's own refusals name `theme build`; the group's words replace
927
+ // those words when the refusal is shown.
928
+ const wordsIn = (/** @type {string} */ line) =>
929
+ [' experimental theme compile ', ' theme build '].find(w =>
930
+ line.includes(w),
931
+ );
932
+ const isCompile = (/** @type {string} */ line) =>
933
+ /^cd /.test(line) || Boolean(wordsIn(line));
934
+ if (!first || /^cd /.test(first.name) || !wordsIn(first.name)) return err;
935
+ /** @param {string} line */
936
+ const read = line => {
937
+ const words = wordsIn(line);
938
+ if (!words) return null;
939
+ const args = splitCommand(line.slice(line.indexOf(words) + words.length));
940
+ if (!args) return null;
941
+ /** @type {{source?: string, out?: string, iconsSpecifier?: string, check?: boolean}} */
942
+ const r = {};
943
+ for (let i = 0; i < args.length; i += 1) {
944
+ const a = args[i];
945
+ if (a === '--check') r.check = true;
946
+ else if (a === '--out') r.out = args[(i += 1)];
947
+ else if (a === '--icons-specifier') r.iconsSpecifier = args[(i += 1)];
948
+ else if (a.startsWith('-') || r.source) return null;
949
+ else r.source = a;
950
+ }
951
+ return r.source ? r : null;
952
+ };
953
+ const COMPILE = {group: 'experimental theme', build: 'compile'};
954
+ /** @type {string | null} */
955
+ let outDropped = null;
956
+ const followed = await followLine(
957
+ first.name,
958
+ async line => {
959
+ // A line that runs elsewhere is not checked from here.
960
+ if (/^cd /.test(line)) return null;
961
+ const r = read(line);
962
+ if (!r?.source) return null;
963
+ const refused =
964
+ (await editorCompileProblem([r.source], {cwd, ...r})) ??
965
+ buildInputProblem(r.source, r, cwd);
966
+ if (refused) return refused;
967
+ // A build writes only inside the working folder: the same build
968
+ // without --out writes beside the source.
969
+ if (r.out && refusedOutput(path.resolve(cwd, r.out), cwd)) {
970
+ outDropped = r.out;
971
+ return new AstryxError(`--out ${r.out} is outside the working folder`, [
972
+ {
973
+ name: buildSuggestion(
974
+ r.source,
975
+ {check: r.check, iconsSpecifier: r.iconsSpecifier},
976
+ cwd,
977
+ COMPILE,
978
+ ),
979
+ reason: 'do this instead',
980
+ },
981
+ ]);
982
+ }
983
+ return null;
984
+ },
985
+ isCompile,
986
+ );
987
+ if (followed.line === first.name) return err;
988
+ const reason = [
989
+ outDropped && !/ --out /.test(followed.line)
990
+ ? `do this instead, without --out: ${outDropped} is outside the working folder, where a build may not write, so the outputs go beside the source`
991
+ : first.reason,
992
+ ...(followed.why ? [`it is still refused: ${followed.why}`] : []),
993
+ ].join('; ');
994
+ err.suggestions = [
995
+ {...first, name: followed.line, reason},
996
+ ...list.slice(1).filter(x => x.name !== followed.line),
997
+ ];
998
+ return err;
999
+ }
1000
+
725
1001
  /**
726
1002
  * A refusal before `compile` runs on an editor theme's files, or null: the
727
1003
  * editor's generated settings are not a theme source, `--family` does not
@@ -757,25 +1033,172 @@ export async function editorCompileProblem(
757
1033
  from,
758
1034
  COMPILE,
759
1035
  );
1036
+ /**
1037
+ * The compile of `source` from the app root, after a cd there: --out
1038
+ * names the same file from the root while it stays inside it (a build
1039
+ * writes only inside its working folder); one outside is left off, and
1040
+ * `why` says so.
1041
+ * @param {string} source @param {string} root
1042
+ */
1043
+ const fromRootLine = (source, root) => {
1044
+ const rebased = out
1045
+ ? path.relative(root, path.resolve(cwd, slashed(out)))
1046
+ : '';
1047
+ const escapes =
1048
+ !check &&
1049
+ !!rebased &&
1050
+ (rebased.startsWith('..') || path.isAbsolute(rebased));
1051
+ return {
1052
+ name: `cd ${quoteCommandArgument(path.relative(cwd, root) || '.')} && ${
1053
+ escapes
1054
+ ? buildSuggestion(source, {check, iconsSpecifier}, root, COMPILE)
1055
+ : compileLine(source, root)
1056
+ }`,
1057
+ why: escapes
1058
+ ? ` --out ${out} is outside the app root, where a build may not write, so the compile below writes beside the source.`
1059
+ : '',
1060
+ };
1061
+ };
1062
+ // An --out into an editor theme's app copy folder: the copy refresh would
1063
+ // write the theme's own build over it. One into the theme's own editor
1064
+ // folder that is not its default output would leave that folder half
1065
+ // built (the module rewritten, the stylesheet not) and the copy made from
1066
+ // it.
1067
+ if (out) {
1068
+ // Any --out in the copy folder, the folder itself too, in any case,
1069
+ // written with backslashes or through a link, as the build reads it.
1070
+ const rel = path
1071
+ .relative(
1072
+ realLocation(cwdRoot),
1073
+ realLocation(path.resolve(cwd, slashed(out))),
1074
+ )
1075
+ .split(path.sep)
1076
+ .filter(Boolean)
1077
+ .map(x => x.toLowerCase());
1078
+ const inCopy =
1079
+ rel[0] === 'src' &&
1080
+ rel[1] === 'themes' &&
1081
+ rel.length >= 3 &&
1082
+ fs.existsSync(themeFiles(cwdRoot, rel[2]).settings);
1083
+ // Only for a compile of that theme: another source written there is
1084
+ // the build's own case (it warns).
1085
+ const inEditor =
1086
+ rel[0] === 'themes' &&
1087
+ rel.length >= 3 &&
1088
+ fs.existsSync(themeFiles(cwdRoot, rel[1]).settings) &&
1089
+ files.some(
1090
+ file =>
1091
+ editorThemeAt(cwdRoot, path.resolve(cwd, file))?.slug === rel[1],
1092
+ ) &&
1093
+ !outIsDefault(cwdRoot, rel[1], cwd, out);
1094
+ if (inCopy || inEditor) {
1095
+ const t = inCopy ? rel[2] : rel[1];
1096
+ return new AstryxError(
1097
+ inCopy
1098
+ ? `--out ${out} is in ${t}'s app copy (src/themes/${t}/), which the editor writes after every build of themes/${t}/; write the build somewhere else.`
1099
+ : `--out ${out} is in ${t}'s own folder (themes/${t}/), where the theme's build writes its module and stylesheet together; compile without --out to build there, or write the build outside themes/${t}/.`,
1100
+ [
1101
+ {
1102
+ // The same compile without --out: it builds into themes/<t>/,
1103
+ // and the copy follows.
1104
+ // From a subfolder, with the cd to the app root, where an
1105
+ // editor theme compiles.
1106
+ name:
1107
+ path.resolve(cwd) === path.resolve(cwdRoot)
1108
+ ? buildSuggestion(
1109
+ files.length === 1 ? files[0] : `themes/${t}/${t}.theme.ts`,
1110
+ {check, iconsSpecifier},
1111
+ cwd,
1112
+ COMPILE,
1113
+ )
1114
+ : `cd ${quoteCommandArgument(path.relative(cwd, cwdRoot) || '.')} && ${buildSuggestion(
1115
+ files.length === 1
1116
+ ? path.relative(cwdRoot, path.resolve(cwd, files[0])) ||
1117
+ files[0]
1118
+ : `themes/${t}/${t}.theme.ts`,
1119
+ {check, iconsSpecifier},
1120
+ cwdRoot,
1121
+ COMPILE,
1122
+ )}`,
1123
+ reason: 'do this instead',
1124
+ },
1125
+ ],
1126
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1127
+ );
1128
+ }
1129
+ }
760
1130
  for (const file of files) {
761
- const at = editorThemeAt(cwdRoot, path.resolve(cwd, file));
1131
+ const abs = path.resolve(cwd, file);
1132
+ const relParts = path
1133
+ .relative(
1134
+ realLocation(cwdRoot),
1135
+ realLocation(path.resolve(cwd, slashed(file))),
1136
+ )
1137
+ .split(path.sep);
1138
+ // The app's theme module `theme add` writes: not a theme source.
1139
+ if (relParts.join('/') === 'src/astryx-themes.ts')
1140
+ return new AstryxError(
1141
+ `${file} is the app's theme module (\`theme add\` writes it), not a theme source; compile the editor theme instead.`,
1142
+ editedThemes(cwdRoot)
1143
+ .slice(0, 3)
1144
+ .map(t => ({
1145
+ name: compileLine(`themes/${t}/${t}.theme.ts`, cwdRoot),
1146
+ reason: 'do this instead',
1147
+ })),
1148
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1149
+ );
1150
+ // A file of an editor theme's app copy (src/themes/<t>/): the copy
1151
+ // is written from themes/<t>/, so that is what compiles.
1152
+ if (
1153
+ relParts[0] === 'src' &&
1154
+ relParts[1] === 'themes' &&
1155
+ (relParts.length === 3 || relParts.length === 4) &&
1156
+ fs.existsSync(themeFiles(cwdRoot, relParts[2]).settings) &&
1157
+ // A file of yours there (no editor mark, no build header) compiles
1158
+ // like any theme source.
1159
+ (relParts.length === 3 ||
1160
+ isCopyFile(path.join(cwdRoot, ...relParts)) ||
1161
+ relParts[3] === `${camel(relParts[2])}Theme.ts`)
1162
+ ) {
1163
+ const t = relParts[2];
1164
+ const own = `themes/${t}/${t}.theme.ts`;
1165
+ if (path.resolve(cwd) !== path.resolve(cwdRoot)) {
1166
+ const line = fromRootLine(own, cwdRoot);
1167
+ return new AstryxError(
1168
+ `${t} is a theme the editor made; compile it from the app root (${path.relative(cwd, cwdRoot) || '.'}), where the editor builds it.${line.why}`,
1169
+ [{name: line.name, reason: 'do this instead'}],
1170
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1171
+ );
1172
+ }
1173
+ // Its source too: a compile of the copy builds neither the theme
1174
+ // nor a current copy.
1175
+ return new AstryxError(
1176
+ `${file} is ${relParts.length === 3 ? '' : 'part of '}${t}'s app copy, which the editor writes from themes/${t}/ after every build; compile the theme itself.`,
1177
+ [
1178
+ {name: compileLine(own, cwd), reason: 'do this instead'},
1179
+ {
1180
+ name: runLine(cwdRoot, `experimental theme editor show ${t}`),
1181
+ reason: 'writes the app copy again from the theme',
1182
+ },
1183
+ ],
1184
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1185
+ );
1186
+ }
1187
+ const at = editorThemeAt(cwdRoot, abs);
762
1188
  if (!at || !editorOwnFile(at.slug, file)) continue;
763
1189
  const {root, slug} = at;
764
1190
  const source = `themes/${slug}/${slug}.theme.ts`;
765
1191
  // An editor theme is built from its project's root, as every editor
766
1192
  // command runs there: from a subfolder the build would write outside
767
1193
  // its working folder. The run names the root.
768
- if (path.resolve(cwd) !== path.resolve(root))
1194
+ if (path.resolve(cwd) !== path.resolve(root)) {
1195
+ const line = fromRootLine(source, root);
769
1196
  return new AstryxError(
770
- `${slug} is a theme the editor made; compile it from the app root (${path.relative(cwd, root) || '.'}), where the editor builds it.`,
771
- [
772
- {
773
- name: `cd ${quoteCommandArgument(path.relative(cwd, root) || '.')} && ${compileLine(source, root)}`,
774
- reason: 'do this instead',
775
- },
776
- ],
1197
+ `${slug} is a theme the editor made; compile it from the app root (${path.relative(cwd, root) || '.'}), where the editor builds it.${line.why}`,
1198
+ [{name: line.name, reason: 'do this instead'}],
777
1199
  ERROR_CODES.ERR_INVALID_ARGUMENT,
778
1200
  );
1201
+ }
779
1202
  if (/\.settings\.(ts|json)$/.test(file))
780
1203
  return new AstryxError(
781
1204
  `${file} is the editor's own settings for ${slug}, not a theme source; compile the theme source.`,
@@ -790,6 +1213,13 @@ export async function editorCompileProblem(
790
1213
  ],
791
1214
  ERROR_CODES.ERR_INVALID_ARGUMENT,
792
1215
  );
1216
+ // A source that no longer carries the settings (another theme's name,
1217
+ // or no extends of them) is refused as set refuses it: a build would
1218
+ // write another theme's files, or drop every setting.
1219
+ {
1220
+ const refusal = await themeSourceRefusal(root, slug);
1221
+ if (refusal) return refusal;
1222
+ }
793
1223
  if (family)
794
1224
  return new AstryxError(
795
1225
  `${slug} is a theme the editor made; --family builds package theme families, not editor themes. Compile it on its own.`,
@@ -868,12 +1298,11 @@ export async function editorCompileProblem(
868
1298
  ],
869
1299
  ERROR_CODES.ERR_THEME_INVALID,
870
1300
  );
871
- // A [light, dark] pair on a token that is not a color, however the
872
- // file writes it: what the theme builds now is read (never written
873
- // beside it), as the editor's own build reads it.
1301
+ // The file's own token values, judged as a build of it writes them
1302
+ // (never written beside it), by the check the editor's build runs.
874
1303
  if (path.basename(file) === `${slug}.theme.ts`) {
875
- const pairs = await edit.editorPairRefusal(root, slug);
876
- if (pairs) return pairs;
1304
+ const values = await edit.editorOwnTokenRefusal(root, slug);
1305
+ if (values) return values;
877
1306
  }
878
1307
  }
879
1308
  }
@@ -883,7 +1312,7 @@ export async function editorCompileProblem(
883
1312
  /** The editor themes a running `compile` holds the lock of, by project and name. */
884
1313
  const COMPILE_HELD = new Set();
885
1314
 
886
- /** @type {(() => Promise<void>) | null} The release of the running compile's hold. */
1315
+ /** @type {((refused?: boolean) => Promise<void>) | null} The release of the running compile's hold; `refused`: it wrote nothing, so no app copy either. */
887
1316
  let releaseCompileHold = null;
888
1317
 
889
1318
  /**
@@ -891,10 +1320,10 @@ let releaseCompileHold = null;
891
1320
  * `compile` builds or checks them, so it never reads a change half
892
1321
  * written. Resolves to the release.
893
1322
  * @param {string[]} files
894
- * @param {{cwd: string}} options
1323
+ * @param {{cwd: string, check?: boolean, out?: string}} options
895
1324
  * @returns {Promise<() => Promise<void>>}
896
1325
  */
897
- export async function editorThemeHold(files, {cwd}) {
1326
+ export async function editorThemeHold(files, {cwd, check = false, out}) {
898
1327
  // Two compiles of one theme in one process: the second waits for the
899
1328
  // lock like any other command.
900
1329
  const cwdRoot = projectRoot(cwd);
@@ -903,13 +1332,23 @@ export async function editorThemeHold(files, {cwd}) {
903
1332
  const at = editorThemeAt(cwdRoot, path.resolve(cwd, f));
904
1333
  if (at && editorOwnFile(at.slug, f))
905
1334
  found.set(`${at.root}\0${at.slug}`, at);
1335
+ // The app copy's source (src/themes/<t>/<t>Theme.ts) of an editor
1336
+ // theme: built under the theme's lock, so it never lands between a
1337
+ // change's two writes.
1338
+ const copy = appCopyTheme(cwdRoot, path.resolve(cwd, f));
1339
+ if (copy) found.set(`${copy.root}\0${copy.slug}`, copy);
906
1340
  }
907
1341
  const themes = [...found.keys()].sort().map(k => found.get(k));
908
- /** @type {Array<() => Promise<void>>} */
1342
+ /** @type {Array<{copy: (refused: boolean) => boolean, finish: (later: boolean) => Promise<void>}>} */
909
1343
  const releases = [];
910
- const releaseAll = async () => {
1344
+ // Every theme's copy is written first, at once (a refusal ends the run
1345
+ // without waiting), then each lock goes.
1346
+ const releaseAll = async (refused = false) => {
911
1347
  if (releaseCompileHold === releaseAll) releaseCompileHold = null;
912
- for (const r of releases.splice(0).reverse()) await r();
1348
+ const items = releases.splice(0);
1349
+ const later = items.map(x => x.copy(refused));
1350
+ for (const [i, x] of [...items.entries()].reverse())
1351
+ await x.finish(later[i]);
913
1352
  };
914
1353
  releaseCompileHold = releaseAll;
915
1354
  for (const {
@@ -927,6 +1366,20 @@ export async function editorThemeHold(files, {cwd}) {
927
1366
  ready = r;
928
1367
  });
929
1368
  const key = `${root}\0${slug}`;
1369
+ // When the lock was taken, and the context a copy write needs (its
1370
+ // font stylesheets), ready before the build so the copy is written at
1371
+ // once when the run ends.
1372
+ const heldAt = Date.now() - 1;
1373
+ // An --out elsewhere builds neither themes/<theme>/ nor its copy.
1374
+ const skipped = outSkipsCopy(root, slug, cwd, {check, out});
1375
+ let ran = false;
1376
+ /** @type {any} */
1377
+ let copyCtx = null;
1378
+ try {
1379
+ copyCtx = await context(root);
1380
+ } catch {
1381
+ copyCtx = null;
1382
+ }
930
1383
  const done = withLock(root, lockName(slug), async () => {
931
1384
  COMPILE_HELD.add(key);
932
1385
  ready();
@@ -941,12 +1394,50 @@ export async function editorThemeHold(files, {cwd}) {
941
1394
  await releaseAll();
942
1395
  throw err;
943
1396
  }
944
- releases.push(async () => {
1397
+ releases.push({
945
1398
  // A compile that wrote the build refreshes the app's copy too
946
- // (src/themes/<theme>/), under the same lock; a check writes nothing.
947
- if (!process.argv.includes('--check')) {
1399
+ // (src/themes/<theme>/), under the same lock; a check writes
1400
+ // nothing. Each theme's copy follows its own build: a theme this run
1401
+ // built (its build now matches its settings) gets its copy, whatever
1402
+ // a later theme did; one that was refused or failed keeps its copy.
1403
+ // True when the copy still needs its font stylesheet from the
1404
+ // theme's settings (written in finish).
1405
+ copy: (/** @type {boolean} */ refused) => {
1406
+ ran = !refused && (process.exitCode ?? 0) === 0;
1407
+ if (check || skipped) return false;
1408
+ // This run built the theme: its module and stylesheet, one unit,
1409
+ // were written since the lock was taken, or the run ended well. A
1410
+ // refused or failed compile writes no copy.
1411
+ const f = themeFiles(root, slug);
1412
+ const written = [f.css, f.js].every(x => {
1413
+ try {
1414
+ return fs.statSync(x).mtimeMs >= heldAt;
1415
+ } catch {
1416
+ return false;
1417
+ }
1418
+ });
1419
+ if (!written && (refused || (process.exitCode ?? 0) !== 0))
1420
+ return false;
948
1421
  try {
949
- writeAppCopyAt(root, slug, null);
1422
+ // From what was just built, its font stylesheets from the fonts
1423
+ // the theme loads now.
1424
+ if (copyCtx) writeAppCopy(copyCtx, slug, loadTheme(root, slug));
1425
+ else writeAppCopyAt(root, slug, null);
1426
+ } catch {
1427
+ try {
1428
+ writeAppCopyAt(root, slug, null);
1429
+ } catch {
1430
+ // A theme that does not load keeps its copy as it was.
1431
+ }
1432
+ }
1433
+ return false;
1434
+ },
1435
+ finish: async () => {
1436
+ // Said only after a build or check that ran: a refused one wrote
1437
+ // and checked nothing.
1438
+ if (skipped) {
1439
+ if (ran) humanWarn(skipped);
1440
+ } else if (!check) {
950
1441
  // The first write of the app copy is named once (the compile's
951
1442
  // own JSON envelope is the stable command's).
952
1443
  const wrote = takeFirstAppCopy(root, slug);
@@ -954,17 +1445,209 @@ export async function editorThemeHold(files, {cwd}) {
954
1445
  humanWarn(
955
1446
  `wrote ${slug}'s app copy, a local theme \`theme add ${slug}\` takes, written again after every build: ${wrote.join(', ')}`,
956
1447
  );
957
- } catch {
958
- // A theme that does not load keeps its copy as it was.
1448
+ // A copy a file or a link keeps behind, or one that could not be
1449
+ // written, is said, as set and show say it.
1450
+ const stale = appCopyStale(root, slug).appCopyStale;
1451
+ if (stale) humanWarn(stale);
959
1452
  }
960
- }
961
- release();
962
- await done;
1453
+ release();
1454
+ await done;
1455
+ },
963
1456
  });
964
1457
  }
965
1458
  return releaseAll;
966
1459
  }
967
1460
 
1461
+ /** @param {string} p a path as typed, with Windows separators read as `/` */
1462
+ const slashed = p => String(p).replace(/\\/g, '/');
1463
+
1464
+ /**
1465
+ * Whether --out names an editor theme's default output (its folder, or
1466
+ * its stylesheet there), where the build writes without --out; no --out
1467
+ * counts.
1468
+ * @param {string} root the app root
1469
+ * @param {string} slug
1470
+ * @param {string} cwd
1471
+ * @param {string | undefined} out
1472
+ */
1473
+ function outIsDefault(root, slug, cwd, out) {
1474
+ if (!out) return true;
1475
+ const at = realLocation(path.resolve(cwd, slashed(out)));
1476
+ const dir = realLocation(themeFiles(root, slug).dir);
1477
+ return (
1478
+ at.toLowerCase() === dir.toLowerCase() ||
1479
+ at.toLowerCase() === path.join(dir, `${slug}.css`).toLowerCase()
1480
+ );
1481
+ }
1482
+
1483
+ /**
1484
+ * Why an editor theme's app copy was not written, or checked, by a build
1485
+ * with --out elsewhere; null without one.
1486
+ * @param {string} root
1487
+ * @param {string} slug
1488
+ * @param {string} cwd
1489
+ * @param {{check?: boolean, out?: string}} opts
1490
+ */
1491
+ function outSkipsCopy(root, slug, cwd, {check, out}) {
1492
+ if (outIsDefault(root, slug, cwd, out)) return null;
1493
+ return check
1494
+ ? `--check with --out checks only the files --out names: themes/${slug}/ and ${slug}'s app copy in src/themes/${slug}/ are not checked. Check without --out to check them.`
1495
+ : `--out builds only the files it names: themes/${slug}/ and ${slug}'s app copy in src/themes/${slug}/ were not written. Compile without --out to write them.`;
1496
+ }
1497
+
1498
+ /**
1499
+ * Whether a file in an app copy folder is the editor's: its mark, or the
1500
+ * header the build writes. A missing file counts (it would be the copy's).
1501
+ * @param {string} file
1502
+ */
1503
+ function isCopyFile(file) {
1504
+ try {
1505
+ const head = fs.readFileSync(file, 'utf8').slice(0, 600);
1506
+ return (
1507
+ head.includes(APP_COPY_MARK) ||
1508
+ /@generated by `astryx theme build`/.test(head)
1509
+ );
1510
+ } catch {
1511
+ return true;
1512
+ }
1513
+ }
1514
+
1515
+ /**
1516
+ * The editor theme whose app copy source a file is
1517
+ * (`src/themes/<t>/<t>Theme.ts`, with themes/<t>/ an editor theme), or
1518
+ * null.
1519
+ * @param {string} root the project root
1520
+ * @param {string} file absolute
1521
+ * @returns {{root: string, slug: string} | null}
1522
+ */
1523
+ function appCopyTheme(root, file) {
1524
+ const rel = path.relative(root, file).split(path.sep);
1525
+ if (rel.length !== 4 || rel[0] !== 'src' || rel[1] !== 'themes') return null;
1526
+ const slug = rel[2];
1527
+ if (rel[3] !== `${camel(slug)}Theme.ts`) return null;
1528
+ return fs.existsSync(themeFiles(root, slug).settings) ? {root, slug} : null;
1529
+ }
1530
+
1531
+ /**
1532
+ * For `compile` of an editor theme: `appCopyStale` for its result data when
1533
+ * the app copy is behind (the same line set and show give), or
1534
+ * `appCopySkipped` when --out builds elsewhere (neither themes/<theme>/ nor
1535
+ * the copy is written or checked); null for any other file or a current
1536
+ * copy.
1537
+ * @param {string} file the path as given
1538
+ * @param {string} cwd
1539
+ * @param {{check: boolean, upToDate?: boolean, out?: string}} opts
1540
+ * `upToDate`: the check's own verdict on the theme's outputs
1541
+ * @returns {Promise<Record<string, unknown> | null>}
1542
+ */
1543
+ export async function editorBuildExtra(file, cwd, opts) {
1544
+ const root = projectRoot(cwd);
1545
+ const abs = path.resolve(cwd, file);
1546
+ const own = editorThemeAt(root, abs);
1547
+ const slug =
1548
+ appCopyTheme(root, abs)?.slug ??
1549
+ (own && editorOwnFile(own.slug, file) ? own.slug : null);
1550
+ if (!slug) return null;
1551
+ {
1552
+ const skipped = outSkipsCopy(root, slug, cwd, opts);
1553
+ if (skipped) return {appCopySkipped: skipped};
1554
+ }
1555
+ if (opts.check) {
1556
+ // A theme output that is stale is the check's own finding (it names
1557
+ // the file); the copy is compared only with a current build.
1558
+ if (opts.upToDate === false) return null;
1559
+ /** @type {string[] | null} */
1560
+ let fonts;
1561
+ try {
1562
+ fonts = copyFontsCss(await context(root), slug, loadTheme(root, slug));
1563
+ } catch {
1564
+ fonts = null;
1565
+ }
1566
+ const behind = appCopyBehind(root, slug, fonts);
1567
+ return behind
1568
+ ? {
1569
+ upToDate: false,
1570
+ code: 'ERR_THEME_STALE',
1571
+ // Something of yours in the way: its own line says what to move;
1572
+ // no editor command writes over it.
1573
+ appCopyStale: appCopyStale(root, slug).appCopyStale
1574
+ ? `${slug}'s app copy in src/themes/${slug}/ is not up to date: ${behind}`
1575
+ : `${slug}'s app copy in src/themes/${slug}/ is not up to date: ${behind}. ${runLine(root, `experimental theme editor show ${slug}`)} writes it again`,
1576
+ }
1577
+ : null;
1578
+ }
1579
+ const stale = appCopyStale(root, slug).appCopyStale;
1580
+ return stale ? {appCopyStale: stale} : null;
1581
+ }
1582
+
1583
+ /**
1584
+ * For `compile` of an editor theme's own source (themes/<t>/<t>.theme.ts):
1585
+ * the one color mode a light-only or dark-only theme has, which its source
1586
+ * cannot say, for the build to write into the built module. Null for any
1587
+ * other file (the app copy's source re-exports it from its build).
1588
+ * @param {string} file the path as given
1589
+ * @param {string} cwd
1590
+ * @returns {['light'] | ['dark'] | null}
1591
+ */
1592
+ export function editorColorModes(file, cwd) {
1593
+ const root = projectRoot(cwd);
1594
+ const at = editorThemeAt(root, path.resolve(cwd, file));
1595
+ if (!at || path.basename(file) !== `${at.slug}.theme.ts`) return null;
1596
+ return buildColorModes(at.root, at.slug);
1597
+ }
1598
+
1599
+ /**
1600
+ * For `compile` of an editor theme (themes/<t>/ or its app copy's source):
1601
+ * whether a font family is loaded by the app copy's font stylesheet as
1602
+ * this compile writes it (src/themes/<t>/<t>.fonts.css, from the fonts the
1603
+ * theme loads now: an @fontsource package by its name, a stylesheet by the
1604
+ * `family=` it requests), so the build's "named but not loaded" notice
1605
+ * leaves it out. Null for any other file.
1606
+ * @param {string} file the path as given
1607
+ * @param {string} cwd
1608
+ * @returns {Promise<((family: string) => boolean) | null>}
1609
+ */
1610
+ export async function editorLoadedFonts(file, cwd) {
1611
+ const root = projectRoot(cwd);
1612
+ const abs = path.resolve(cwd, file);
1613
+ const slug =
1614
+ appCopyTheme(root, abs)?.slug ??
1615
+ (editorThemeAt(root, abs) &&
1616
+ editorOwnFile(editorThemeAt(root, abs)?.slug ?? '', file)
1617
+ ? editorThemeAt(root, abs)?.slug
1618
+ : null);
1619
+ if (!slug) return null;
1620
+ // The stylesheets the copy's font file imports once this compile has
1621
+ // written it: from the fonts the theme loads now, as the copy write
1622
+ // computes them.
1623
+ /** @type {string[]} */
1624
+ let specs;
1625
+ try {
1626
+ specs = copyFontsCss(await context(root), slug, loadTheme(root, slug));
1627
+ } catch {
1628
+ return null;
1629
+ }
1630
+ const copyDir = path.join(root, 'src', 'themes', slug);
1631
+ const from = fs.existsSync(copyDir) ? copyDir : root;
1632
+ /** @type {Set<string>} */
1633
+ const loaded = new Set();
1634
+ for (const spec of specs)
1635
+ for (const f of importedFontFamilies(spec, from)) loaded.add(f);
1636
+ // A family the build names whose @fontsource package is installed: the
1637
+ // copy written after this build imports that package (copyFontsCss).
1638
+ const req = createRequire(path.join(root, 'package.json'));
1639
+ /** @param {string} family */
1640
+ const installed = family => {
1641
+ try {
1642
+ req.resolve(`${fontsourcePackageOf(family)}/package.json`);
1643
+ return true;
1644
+ } catch {
1645
+ return false;
1646
+ }
1647
+ };
1648
+ return family => loaded.has(fontKey(family)) || installed(family);
1649
+ }
1650
+
968
1651
  /**
969
1652
  * For `compile` of a file that is not there: when it is an editor theme's
970
1653
  * own source (`themes/<t>/<t>.theme.ts` beside its settings), the command
@@ -974,6 +1657,37 @@ export async function editorThemeHold(files, {cwd}) {
974
1657
  * @returns {{name: string, reason: string} | null}
975
1658
  */
976
1659
  export function editorMissingSource(file, cwd) {
1660
+ // The app copy's source of an editor theme, its folder gone: the editor
1661
+ // writes it again.
1662
+ const copy = appCopyTheme(projectRoot(cwd), path.resolve(cwd, slashed(file)));
1663
+ if (copy)
1664
+ // A copy file named with Windows separators: the theme is what
1665
+ // compiles (the copy follows); with the copy gone, show writes it.
1666
+ return fs.existsSync(path.join(copy.root, 'src', 'themes', copy.slug))
1667
+ ? {
1668
+ name: runLine(
1669
+ cwd,
1670
+ `experimental theme compile ${
1671
+ path
1672
+ .relative(
1673
+ cwd,
1674
+ path.join(
1675
+ copy.root,
1676
+ 'themes',
1677
+ copy.slug,
1678
+ `${copy.slug}.theme.ts`,
1679
+ ),
1680
+ )
1681
+ .split(path.sep)
1682
+ .join('/') || `themes/${copy.slug}/${copy.slug}.theme.ts`
1683
+ }`,
1684
+ ),
1685
+ reason: `${copy.slug}'s app copy follows every compile of the theme`,
1686
+ }
1687
+ : {
1688
+ name: runLine(cwd, `experimental theme editor show ${copy.slug}`),
1689
+ reason: `the editor writes ${copy.slug}'s app copy in src/themes/${copy.slug}/ again`,
1690
+ };
977
1691
  const at = editorThemeAt(projectRoot(cwd), path.resolve(cwd, file));
978
1692
  if (!at || path.basename(file) !== `${at.slug}.theme.ts`) return null;
979
1693
  return {