@astryxdesign/cli 0.7.16 → 0.7.17-canary.g335a172

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 (339) 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.mjs +69 -16
  13. package/api/build/kit/start.d.mts +5 -4
  14. package/api/build/kit/start.mjs +12 -8
  15. package/api/build/kit/weights.d.mts +16 -3
  16. package/api/build/kit/weights.json +1 -1
  17. package/api/build/kit/weights.mjs +251 -35
  18. package/api/build/kit/word-model.d.mts +23 -0
  19. package/api/build/kit/word-model.mjs +46 -0
  20. package/api/build/kit/words.d.mts +48 -0
  21. package/api/build/kit/words.mjs +90 -1
  22. package/api/component/__fixtures__/resolutions/modules.txt +4 -5
  23. package/api/component/list/list.d.mts +1 -1
  24. package/api/component/list/list.mjs +48 -6
  25. package/api/discover/_adapter.d.mts +1 -1
  26. package/api/discover/search/search.mjs +3 -2
  27. package/api/docs/_adapter.d.mts +26 -2
  28. package/api/docs/_adapter.mjs +219 -6
  29. package/api/docs/docs.d.mts +3 -0
  30. package/api/docs/docs.doc.mjs +7 -1
  31. package/api/docs/docs.mjs +12 -0
  32. package/api/docs/docs.type.d.mts +5 -0
  33. package/api/docs/docs.type.mjs +2 -0
  34. package/api/docs/node/node.mjs +1 -1
  35. package/api/doctor/_adapter.d.mts +27 -5
  36. package/api/doctor/_adapter.mjs +83 -10
  37. package/api/doctor/built-checks.mjs +40 -14
  38. package/api/doctor/doctor.d.mts +4 -3
  39. package/api/doctor/doctor.mjs +14 -7
  40. package/api/doctor/theme-checks.mjs +121 -17
  41. package/api/icon/_records.mjs +2 -1
  42. package/api/index.d.mts +2 -0
  43. package/api/index.mjs +2 -0
  44. package/api/integration/_adapter.d.mts +44 -0
  45. package/api/integration/_adapter.mjs +77 -1
  46. package/api/integration/authoring-checks.mjs +6 -6
  47. package/api/integration/example-scope.d.mts +14 -0
  48. package/api/integration/example-scope.mjs +44 -0
  49. package/api/integration/example-scope.type.d.mts +29 -0
  50. package/api/integration/example-scope.type.mjs +25 -0
  51. package/api/integration/integrationExampleScope.doc.d.mts +11 -0
  52. package/api/integration/integrationExampleScope.doc.mjs +59 -0
  53. package/api/integration/validate-integration.mjs +19 -1
  54. package/api/integration/validate-integration.type.d.mts +9 -0
  55. package/api/integration/validate-integration.type.mjs +1 -1
  56. package/api/integration/validateIntegration.doc.mjs +1 -1
  57. package/api/render/_adapter.mjs +591 -195
  58. package/api/render/render.doc.mjs +11 -3
  59. package/api/render/render.type.d.mts +52 -0
  60. package/api/render/render.type.mjs +31 -1
  61. package/api/search/ORDERING.md +1 -1
  62. package/api/search/_candidate-score.mjs +51 -32
  63. package/api/search/_fit-order.mjs +3 -3
  64. package/api/search/_gather-components.d.mts +3 -2
  65. package/api/search/_gather-components.mjs +12 -3
  66. package/api/search/_lexical.d.mts +5 -0
  67. package/api/search/_lexical.mjs +8 -2
  68. package/api/search/_result.d.mts +11 -9
  69. package/api/search/_result.mjs +4 -4
  70. package/api/search/_score.d.mts +6 -1
  71. package/api/search/_score.mjs +15 -3
  72. package/api/search/search-index.mjs +7 -1
  73. package/api/search/search.d.mts +1 -1
  74. package/api/theme/_adapter.mjs +12 -1
  75. package/api/theme/build/build.d.mts +60 -0
  76. package/api/theme/build/build.mjs +210 -54
  77. package/api/theme/build/font-warning.d.mts +8 -0
  78. package/api/theme/build/font-warning.mjs +19 -0
  79. package/api/theme/theme.type.d.mts +10 -0
  80. package/api/theme/theme.type.mjs +7 -1
  81. package/api/theme-edit/_adapter.d.mts +55 -0
  82. package/api/theme-edit/_adapter.mjs +673 -42
  83. package/api/theme-edit/_check.d.mts +28 -22
  84. package/api/theme-edit/_check.mjs +1858 -312
  85. package/api/theme-edit/_color.d.mts +10 -0
  86. package/api/theme-edit/_color.mjs +49 -1
  87. package/api/theme-edit/_compiler.mjs +9 -0
  88. package/api/theme-edit/_context.mjs +60 -3
  89. package/api/theme-edit/_css.mjs +21 -1
  90. package/api/theme-edit/_edit.d.mts +22 -0
  91. package/api/theme-edit/_edit.mjs +1533 -92
  92. package/api/theme-edit/_how-commands.d.mts +57 -0
  93. package/api/theme-edit/_how-commands.mjs +587 -1
  94. package/api/theme-edit/_how-question.d.mts +78 -0
  95. package/api/theme-edit/_how-question.mjs +4716 -235
  96. package/api/theme-edit/_how.mjs +134 -8
  97. package/api/theme-edit/_marks.d.mts +31 -5
  98. package/api/theme-edit/_marks.mjs +57 -5
  99. package/api/theme-edit/_normal.d.mts +219 -0
  100. package/api/theme-edit/_normal.mjs +1041 -0
  101. package/api/theme-edit/_palette.mjs +10 -4
  102. package/api/theme-edit/_parts.d.mts +8 -0
  103. package/api/theme-edit/_parts.mjs +103 -2
  104. package/api/theme-edit/_playwright-picture.d.mts +4 -0
  105. package/api/theme-edit/_playwright-picture.mjs +34 -0
  106. package/api/theme-edit/_preview-app.d.mts +7 -1
  107. package/api/theme-edit/_preview-app.mjs +394 -74
  108. package/api/theme-edit/_preview.d.mts +69 -8
  109. package/api/theme-edit/_preview.mjs +531 -108
  110. package/api/theme-edit/_settings.d.mts +243 -6
  111. package/api/theme-edit/_settings.mjs +2415 -147
  112. package/api/theme-edit/_tables.d.mts +6 -0
  113. package/api/theme-edit/_tables.mjs +44 -1
  114. package/api/theme-edit/_vet.d.mts +18 -0
  115. package/api/theme-edit/_vet.mjs +32 -0
  116. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  117. package/api/theme-edit/theme-edit.type.mjs +4 -1
  118. package/api/theme-edit/themeEditHow.doc.mjs +1 -1
  119. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  120. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  121. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -2
  122. package/api/upgrade/run/run.mjs +4 -3
  123. package/assets/codemods/integration-discovery.mjs +2 -1
  124. package/assets/codemods/run-codemod.mjs +87 -7
  125. package/assets/codemods/transforms/next/index.mjs +11 -1
  126. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  127. package/assets/component-list.generated.json +1 -0
  128. package/assets/docs/author-a-theme.doc.mjs +1 -0
  129. package/assets/docs/theme.doc.dense.mjs +2 -2
  130. package/assets/docs/theme.doc.mjs +1 -1
  131. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  132. package/assets/docs/tree/checks.doc.mjs +1 -1
  133. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  134. package/assets/docs/tree/render-pages.doc.mjs +44 -3
  135. package/assets/docs/tree/single-component.doc.mjs +4 -0
  136. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  137. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  138. package/assets/docs/use-a-theme.doc.mjs +1 -1
  139. package/assets/docs.generated.json +2 -2
  140. package/assets/self-audit.generated.json +1 -1
  141. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  142. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  143. package/assets/templates/blocks/components/Hooks/useContainerRevealHookUsage.tsx +2 -2
  144. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  145. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  146. package/assets/templates/components.generated.json +1 -1
  147. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  148. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  149. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  150. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  151. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  152. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  153. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  154. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  155. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  156. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  157. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  158. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  159. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  160. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  161. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  162. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  163. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  164. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  165. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  166. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  167. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  168. package/assets/templates/pages/ide/page.tsx +24 -2
  169. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  170. package/assets/templates/pages/library/template.doc.mjs +4 -1
  171. package/assets/templates/pages/login/template.doc.mjs +2 -1
  172. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  173. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  174. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  175. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  176. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  177. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  178. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  179. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  180. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  181. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  182. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  183. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  184. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  185. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  186. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  187. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  188. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  189. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  190. package/assets/theme.template.ts +6 -0
  191. package/authoring/debug/parse.d.mts +3 -3
  192. package/authoring/doctypes/_schema.d.mts +3 -3
  193. package/authoring/doctypes/command/command.doc.mjs +1 -1
  194. package/authoring/doctypes/component/categories.d.mts +37 -0
  195. package/authoring/doctypes/component/categories.mjs +54 -0
  196. package/authoring/doctypes/template/template.doc.mjs +1 -1
  197. package/authoring/doctypes/template/type.ts +9 -0
  198. package/authoring/example/example.doc.d.mts +12 -0
  199. package/authoring/example/example.doc.mjs +69 -0
  200. package/authoring/example/preview.d.mts +92 -0
  201. package/authoring/example/preview.mjs +994 -0
  202. package/authoring/example/type.ts +102 -0
  203. package/authoring/index.d.mts +1 -0
  204. package/authoring/index.d.ts +12 -0
  205. package/authoring/index.mjs +3 -1
  206. package/clients/cli/bin/astryx.mjs +6 -9
  207. package/clients/cli/build-id.generated.json +1 -1
  208. package/clients/cli/commands/component.doc.mjs +1 -1
  209. package/clients/cli/commands/component.mjs +22 -3
  210. package/clients/cli/commands/docs.doc.mjs +7 -1
  211. package/clients/cli/commands/docs.mjs +3 -1
  212. package/clients/cli/commands/doctor.mjs +32 -14
  213. package/clients/cli/commands/experimental/render.mjs +42 -1
  214. package/clients/cli/commands/experimental/theme.mjs +145 -16
  215. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  216. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  217. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +87 -1
  218. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +2 -2
  219. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -1
  220. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  221. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  222. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  223. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  224. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
  225. package/clients/cli/commands/search.doc.mjs +2 -1
  226. package/clients/cli/commands/search.mjs +6 -1
  227. package/clients/cli/commands/template.mjs +4 -2
  228. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  229. package/clients/cli/commands/theme.doc.mjs +1 -1
  230. package/clients/cli/commands/theme.mjs +183 -6
  231. package/clients/cli/commands/upgrade.mjs +42 -7
  232. package/clients/cli/index.mjs +1 -1
  233. package/clients/cli/lib/cli-error.mjs +2 -1
  234. package/clients/cli/lib/component-format.mjs +205 -98
  235. package/clients/cli/lib/hook-format.mjs +9 -19
  236. package/clients/cli/lib/json-shim.mjs +77 -21
  237. package/clients/cli/lib/manifest.mjs +3 -2
  238. package/clients/cli/manifest.generated.json +1 -1
  239. package/foundation/agent-docs/agent-docs.mjs +1 -1
  240. package/foundation/config/theme-state.d.mts +13 -1
  241. package/foundation/config/theme-state.mjs +47 -7
  242. package/foundation/discovery/authoring-self-docs.mjs +1 -0
  243. package/foundation/discovery/component-discovery.d.mts +11 -0
  244. package/foundation/discovery/component-discovery.mjs +56 -8
  245. package/foundation/discovery/component-replacement.mjs +130 -1
  246. package/foundation/discovery/copied-passages.mjs +2 -1
  247. package/foundation/discovery/doc-types.mjs +4 -3
  248. package/foundation/discovery/entry-name-files.d.mts +43 -0
  249. package/foundation/discovery/entry-name-files.mjs +264 -0
  250. package/foundation/discovery/hook-discovery.d.mts +8 -2
  251. package/foundation/discovery/hook-discovery.mjs +22 -8
  252. package/foundation/discovery/icon-definition-discovery.mjs +2 -1
  253. package/foundation/discovery/icon-set-checks.mjs +2 -1
  254. package/foundation/discovery/icon-set-discovery.mjs +2 -1
  255. package/foundation/discovery/integration-doc-files.mjs +2 -1
  256. package/foundation/discovery/package-exports.d.mts +10 -0
  257. package/foundation/discovery/package-exports.mjs +56 -3
  258. package/foundation/discovery/prop-drift.mjs +9 -9
  259. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  260. package/foundation/discovery/runner-allowlist.mjs +2 -0
  261. package/foundation/discovery/stored-component-list.d.mts +43 -0
  262. package/foundation/discovery/stored-component-list.mjs +90 -0
  263. package/foundation/discovery/stored-self-audit.d.mts +26 -10
  264. package/foundation/discovery/stored-self-audit.mjs +38 -18
  265. package/foundation/discovery/template-adapter.d.mts +52 -0
  266. package/foundation/discovery/template-adapter.mjs +176 -12
  267. package/foundation/discovery/template-icons.mjs +2 -1
  268. package/foundation/discovery/theme-discovery.mjs +3 -2
  269. package/foundation/discovery/theme-imports.d.mts +14 -0
  270. package/foundation/discovery/theme-imports.mjs +43 -1
  271. package/foundation/discovery/theming-targets.mjs +3 -2
  272. package/foundation/doc-compiler/import.mjs +7 -1
  273. package/foundation/doc-compiler/inputs.mjs +3 -2
  274. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  275. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  276. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  277. package/foundation/env/semver.mjs +3 -1
  278. package/foundation/fonts/font-stylesheet.d.mts +31 -0
  279. package/foundation/fonts/font-stylesheet.mjs +115 -0
  280. package/foundation/fs/cache-eviction.d.mts +21 -0
  281. package/foundation/fs/cache-eviction.mjs +73 -0
  282. package/foundation/fs/file-protection.mjs +4 -3
  283. package/foundation/fs/gather-cache.d.mts +25 -4
  284. package/foundation/fs/gather-cache.mjs +164 -55
  285. package/foundation/fs/jiti-cache.d.mts +19 -1
  286. package/foundation/fs/jiti-cache.mjs +130 -6
  287. package/foundation/fs/kept-tree.d.mts +60 -0
  288. package/foundation/fs/kept-tree.mjs +154 -0
  289. package/foundation/fs/listing-cache.d.mts +7 -0
  290. package/foundation/fs/listing-cache.mjs +10 -0
  291. package/foundation/fs/module-loader.d.mts +24 -2
  292. package/foundation/fs/module-loader.mjs +94 -21
  293. package/foundation/fs/paths.d.mts +18 -0
  294. package/foundation/fs/paths.mjs +15 -2
  295. package/foundation/identity/provider-identity.d.mts +6 -0
  296. package/foundation/identity/provider-identity.mjs +10 -1
  297. package/foundation/integrations/contribution-inventory.mjs +4 -3
  298. package/foundation/integrations/example-code.d.mts +63 -0
  299. package/foundation/integrations/example-code.mjs +416 -0
  300. package/foundation/integrations/public-imports.d.mts +9 -0
  301. package/foundation/integrations/public-imports.mjs +3 -2
  302. package/foundation/integrations/validate-contributions.mjs +17 -45
  303. package/foundation/render/embedded-files.d.mts +6 -0
  304. package/foundation/render/embedded-files.mjs +25 -5
  305. package/foundation/render/embedded-imports.d.mts +39 -7
  306. package/foundation/render/embedded-imports.mjs +66 -27
  307. package/foundation/render/file-header.d.mts +66 -0
  308. package/foundation/render/file-header.mjs +118 -0
  309. package/foundation/render/host-answer.d.mts +18 -0
  310. package/foundation/render/host-answer.mjs +46 -0
  311. package/foundation/render/host-exit.d.mts +1 -0
  312. package/foundation/render/host-exit.mjs +6 -1
  313. package/foundation/render/html-reader.d.mts +55 -2
  314. package/foundation/render/html-reader.mjs +145 -26
  315. package/foundation/render/markup-edits.d.mts +57 -0
  316. package/foundation/render/markup-edits.mjs +182 -0
  317. package/foundation/render/page-host.mjs +29 -12
  318. package/foundation/render/page-worker.mjs +121 -13
  319. package/foundation/render/rebuild.mjs +2 -1
  320. package/foundation/render/render-page.d.mts +35 -3
  321. package/foundation/render/render-page.mjs +316 -42
  322. package/foundation/render/source-block.d.mts +85 -6
  323. package/foundation/render/source-block.mjs +211 -20
  324. package/foundation/render/ts-paths.d.mts +69 -6
  325. package/foundation/render/ts-paths.mjs +326 -40
  326. package/foundation/response/error-codes.d.mts +3 -1
  327. package/foundation/response/error-codes.d.ts +1 -0
  328. package/foundation/response/error-codes.doc.mjs +12 -1
  329. package/foundation/response/error-codes.mjs +11 -1
  330. package/foundation/response/response-types.doc.mjs +2 -2
  331. package/foundation/text/compare-text.d.mts +14 -0
  332. package/foundation/text/compare-text.mjs +68 -0
  333. package/foundation/text/string-utils.d.mts +3 -2
  334. package/foundation/text/string-utils.mjs +18 -4
  335. package/foundation/text/suggest-order.d.mts +9 -0
  336. package/foundation/text/suggest-order.mjs +15 -1
  337. package/foundation/xle/expand.mjs +2 -1
  338. package/host/index.mjs +8 -6
  339. 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,109 @@ export function editorThemeRefusal(err, cwd) {
722
892
  );
723
893
  }
724
894
 
895
+ /**
896
+ * The editor themes of the app the folder `cwd` is in, by name; none when
897
+ * the folder is in no app or its themes cannot be read.
898
+ * @param {string} cwd
899
+ * @returns {string[]}
900
+ */
901
+ export function editorThemesAt(cwd) {
902
+ try {
903
+ return editedThemes(projectRoot(cwd));
904
+ } catch {
905
+ return [];
906
+ }
907
+ }
908
+
909
+ /**
910
+ * A refused compile whose first suggestion is another compile: that compile
911
+ * is run through compile's own checks before it is offered (in memory,
912
+ * nothing built), and each repair a refusal of it offers is folded into the
913
+ * one line. An --out outside the working folder is dropped, and the reason
914
+ * says why; when the rest cannot be repaired, the reason says so. A line
915
+ * that starts with `cd` runs elsewhere, so it is not checked from here.
916
+ * @param {AstryxError} err
917
+ * @param {string} cwd
918
+ * @returns {Promise<AstryxError>}
919
+ */
920
+ export async function editorFollowCompile(err, cwd) {
921
+ const list = err.suggestions ?? [];
922
+ const first = list[0];
923
+ // The build's own refusals name `theme build`; the group's words replace
924
+ // those words when the refusal is shown.
925
+ const wordsIn = (/** @type {string} */ line) =>
926
+ [' experimental theme compile ', ' theme build '].find(w =>
927
+ line.includes(w),
928
+ );
929
+ const isCompile = (/** @type {string} */ line) =>
930
+ /^cd /.test(line) || Boolean(wordsIn(line));
931
+ if (!first || /^cd /.test(first.name) || !wordsIn(first.name)) return err;
932
+ /** @param {string} line */
933
+ const read = line => {
934
+ const words = wordsIn(line);
935
+ if (!words) return null;
936
+ const args = splitCommand(line.slice(line.indexOf(words) + words.length));
937
+ if (!args) return null;
938
+ /** @type {{source?: string, out?: string, iconsSpecifier?: string, check?: boolean}} */
939
+ const r = {};
940
+ for (let i = 0; i < args.length; i += 1) {
941
+ const a = args[i];
942
+ if (a === '--check') r.check = true;
943
+ else if (a === '--out') r.out = args[(i += 1)];
944
+ else if (a === '--icons-specifier') r.iconsSpecifier = args[(i += 1)];
945
+ else if (a.startsWith('-') || r.source) return null;
946
+ else r.source = a;
947
+ }
948
+ return r.source ? r : null;
949
+ };
950
+ const COMPILE = {group: 'experimental theme', build: 'compile'};
951
+ /** @type {string | null} */
952
+ let outDropped = null;
953
+ const followed = await followLine(
954
+ first.name,
955
+ async line => {
956
+ // A line that runs elsewhere is not checked from here.
957
+ if (/^cd /.test(line)) return null;
958
+ const r = read(line);
959
+ if (!r?.source) return null;
960
+ const refused =
961
+ (await editorCompileProblem([r.source], {cwd, ...r})) ??
962
+ buildInputProblem(r.source, r, cwd);
963
+ if (refused) return refused;
964
+ // A build writes only inside the working folder: the same build
965
+ // without --out writes beside the source.
966
+ if (r.out && refusedOutput(path.resolve(cwd, r.out), cwd)) {
967
+ outDropped = r.out;
968
+ return new AstryxError(`--out ${r.out} is outside the working folder`, [
969
+ {
970
+ name: buildSuggestion(
971
+ r.source,
972
+ {check: r.check, iconsSpecifier: r.iconsSpecifier},
973
+ cwd,
974
+ COMPILE,
975
+ ),
976
+ reason: 'do this instead',
977
+ },
978
+ ]);
979
+ }
980
+ return null;
981
+ },
982
+ isCompile,
983
+ );
984
+ if (followed.line === first.name) return err;
985
+ const reason = [
986
+ outDropped && !/ --out /.test(followed.line)
987
+ ? `do this instead, without --out: ${outDropped} is outside the working folder, where a build may not write, so the outputs go beside the source`
988
+ : first.reason,
989
+ ...(followed.why ? [`it is still refused: ${followed.why}`] : []),
990
+ ].join('; ');
991
+ err.suggestions = [
992
+ {...first, name: followed.line, reason},
993
+ ...list.slice(1).filter(x => x.name !== followed.line),
994
+ ];
995
+ return err;
996
+ }
997
+
725
998
  /**
726
999
  * A refusal before `compile` runs on an editor theme's files, or null: the
727
1000
  * editor's generated settings are not a theme source, `--family` does not
@@ -757,8 +1030,119 @@ export async function editorCompileProblem(
757
1030
  from,
758
1031
  COMPILE,
759
1032
  );
1033
+ // An --out into an editor theme's app copy folder: the copy refresh would
1034
+ // write the theme's own build over it.
1035
+ if (out) {
1036
+ // Any --out in the copy folder, the folder itself too, in any case,
1037
+ // written with backslashes or through a link, as the build reads it.
1038
+ const rel = path
1039
+ .relative(
1040
+ realLocation(cwdRoot),
1041
+ realLocation(path.resolve(cwd, slashed(out))),
1042
+ )
1043
+ .split(path.sep)
1044
+ .filter(Boolean)
1045
+ .map(x => x.toLowerCase());
1046
+ if (
1047
+ rel[0] === 'src' &&
1048
+ rel[1] === 'themes' &&
1049
+ rel.length >= 3 &&
1050
+ fs.existsSync(themeFiles(cwdRoot, rel[2]).settings)
1051
+ ) {
1052
+ const t = rel[2];
1053
+ return new AstryxError(
1054
+ `--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.`,
1055
+ [
1056
+ {
1057
+ // The same compile without --out: it builds into themes/<t>/,
1058
+ // and the copy follows.
1059
+ // From a subfolder, with the cd to the app root, where an
1060
+ // editor theme compiles.
1061
+ name:
1062
+ path.resolve(cwd) === path.resolve(cwdRoot)
1063
+ ? buildSuggestion(
1064
+ files.length === 1 ? files[0] : `themes/${t}/${t}.theme.ts`,
1065
+ {check, iconsSpecifier},
1066
+ cwd,
1067
+ COMPILE,
1068
+ )
1069
+ : `cd ${quoteCommandArgument(path.relative(cwd, cwdRoot) || '.')} && ${buildSuggestion(
1070
+ files.length === 1
1071
+ ? path.relative(cwdRoot, path.resolve(cwd, files[0])) ||
1072
+ files[0]
1073
+ : `themes/${t}/${t}.theme.ts`,
1074
+ {check, iconsSpecifier},
1075
+ cwdRoot,
1076
+ COMPILE,
1077
+ )}`,
1078
+ reason: 'do this instead',
1079
+ },
1080
+ ],
1081
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1082
+ );
1083
+ }
1084
+ }
760
1085
  for (const file of files) {
761
- const at = editorThemeAt(cwdRoot, path.resolve(cwd, file));
1086
+ const abs = path.resolve(cwd, file);
1087
+ const relParts = path
1088
+ .relative(
1089
+ realLocation(cwdRoot),
1090
+ realLocation(path.resolve(cwd, slashed(file))),
1091
+ )
1092
+ .split(path.sep);
1093
+ // The app's theme module `theme add` writes: not a theme source.
1094
+ if (relParts.join('/') === 'src/astryx-themes.ts')
1095
+ return new AstryxError(
1096
+ `${file} is the app's theme module (\`theme add\` writes it), not a theme source; compile the editor theme instead.`,
1097
+ editedThemes(cwdRoot)
1098
+ .slice(0, 3)
1099
+ .map(t => ({
1100
+ name: compileLine(`themes/${t}/${t}.theme.ts`, cwdRoot),
1101
+ reason: 'do this instead',
1102
+ })),
1103
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1104
+ );
1105
+ // A file of an editor theme's app copy (src/themes/<t>/): the copy
1106
+ // is written from themes/<t>/, so that is what compiles.
1107
+ if (
1108
+ relParts[0] === 'src' &&
1109
+ relParts[1] === 'themes' &&
1110
+ (relParts.length === 3 || relParts.length === 4) &&
1111
+ fs.existsSync(themeFiles(cwdRoot, relParts[2]).settings) &&
1112
+ // A file of yours there (no editor mark, no build header) compiles
1113
+ // like any theme source.
1114
+ (relParts.length === 3 ||
1115
+ isCopyFile(path.join(cwdRoot, ...relParts)) ||
1116
+ relParts[3] === `${camel(relParts[2])}Theme.ts`)
1117
+ ) {
1118
+ const t = relParts[2];
1119
+ const own = `themes/${t}/${t}.theme.ts`;
1120
+ if (path.resolve(cwd) !== path.resolve(cwdRoot))
1121
+ return new AstryxError(
1122
+ `${t} is a theme the editor made; compile it from the app root (${path.relative(cwd, cwdRoot) || '.'}), where the editor builds it.`,
1123
+ [
1124
+ {
1125
+ name: `cd ${quoteCommandArgument(path.relative(cwd, cwdRoot) || '.')} && ${compileLine(own, cwdRoot)}`,
1126
+ reason: 'do this instead',
1127
+ },
1128
+ ],
1129
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1130
+ );
1131
+ // Its source too: a compile of the copy builds neither the theme
1132
+ // nor a current copy.
1133
+ return new AstryxError(
1134
+ `${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.`,
1135
+ [
1136
+ {name: compileLine(own, cwd), reason: 'do this instead'},
1137
+ {
1138
+ name: runLine(cwdRoot, `experimental theme editor show ${t}`),
1139
+ reason: 'writes the app copy again from the theme',
1140
+ },
1141
+ ],
1142
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
1143
+ );
1144
+ }
1145
+ const at = editorThemeAt(cwdRoot, abs);
762
1146
  if (!at || !editorOwnFile(at.slug, file)) continue;
763
1147
  const {root, slug} = at;
764
1148
  const source = `themes/${slug}/${slug}.theme.ts`;
@@ -790,6 +1174,13 @@ export async function editorCompileProblem(
790
1174
  ],
791
1175
  ERROR_CODES.ERR_INVALID_ARGUMENT,
792
1176
  );
1177
+ // A source that no longer carries the settings (another theme's name,
1178
+ // or no extends of them) is refused as set refuses it: a build would
1179
+ // write another theme's files, or drop every setting.
1180
+ {
1181
+ const refusal = themeSourceRefusal(root, slug);
1182
+ if (refusal) return refusal;
1183
+ }
793
1184
  if (family)
794
1185
  return new AstryxError(
795
1186
  `${slug} is a theme the editor made; --family builds package theme families, not editor themes. Compile it on its own.`,
@@ -874,6 +1265,9 @@ export async function editorCompileProblem(
874
1265
  if (path.basename(file) === `${slug}.theme.ts`) {
875
1266
  const pairs = await edit.editorPairRefusal(root, slug);
876
1267
  if (pairs) return pairs;
1268
+ // A token value the file gives that set refuses ships as it is.
1269
+ const values = await edit.editorOwnTokenRefusal(root, slug);
1270
+ if (values) return values;
877
1271
  }
878
1272
  }
879
1273
  }
@@ -883,7 +1277,7 @@ export async function editorCompileProblem(
883
1277
  /** The editor themes a running `compile` holds the lock of, by project and name. */
884
1278
  const COMPILE_HELD = new Set();
885
1279
 
886
- /** @type {(() => Promise<void>) | null} The release of the running compile's hold. */
1280
+ /** @type {((refused?: boolean) => Promise<void>) | null} The release of the running compile's hold; `refused`: it wrote nothing, so no app copy either. */
887
1281
  let releaseCompileHold = null;
888
1282
 
889
1283
  /**
@@ -903,13 +1297,23 @@ export async function editorThemeHold(files, {cwd}) {
903
1297
  const at = editorThemeAt(cwdRoot, path.resolve(cwd, f));
904
1298
  if (at && editorOwnFile(at.slug, f))
905
1299
  found.set(`${at.root}\0${at.slug}`, at);
1300
+ // The app copy's source (src/themes/<t>/<t>Theme.ts) of an editor
1301
+ // theme: built under the theme's lock, so it never lands between a
1302
+ // change's two writes.
1303
+ const copy = appCopyTheme(cwdRoot, path.resolve(cwd, f));
1304
+ if (copy) found.set(`${copy.root}\0${copy.slug}`, copy);
906
1305
  }
907
1306
  const themes = [...found.keys()].sort().map(k => found.get(k));
908
- /** @type {Array<() => Promise<void>>} */
1307
+ /** @type {Array<{copy: (refused: boolean) => boolean, finish: (later: boolean) => Promise<void>}>} */
909
1308
  const releases = [];
910
- const releaseAll = async () => {
1309
+ // Every theme's copy is written first, at once (a refusal ends the run
1310
+ // without waiting), then each lock goes.
1311
+ const releaseAll = async (refused = false) => {
911
1312
  if (releaseCompileHold === releaseAll) releaseCompileHold = null;
912
- for (const r of releases.splice(0).reverse()) await r();
1313
+ const items = releases.splice(0);
1314
+ const later = items.map(x => x.copy(refused));
1315
+ for (const [i, x] of [...items.entries()].reverse())
1316
+ await x.finish(later[i]);
913
1317
  };
914
1318
  releaseCompileHold = releaseAll;
915
1319
  for (const {
@@ -927,6 +1331,17 @@ export async function editorThemeHold(files, {cwd}) {
927
1331
  ready = r;
928
1332
  });
929
1333
  const key = `${root}\0${slug}`;
1334
+ // When the lock was taken, and the context a copy write needs (its
1335
+ // font stylesheets), ready before the build so the copy is written at
1336
+ // once when the run ends.
1337
+ const heldAt = Date.now() - 1;
1338
+ /** @type {any} */
1339
+ let copyCtx = null;
1340
+ try {
1341
+ copyCtx = await context(root);
1342
+ } catch {
1343
+ copyCtx = null;
1344
+ }
930
1345
  const done = withLock(root, lockName(slug), async () => {
931
1346
  COMPILE_HELD.add(key);
932
1347
  ready();
@@ -941,12 +1356,45 @@ export async function editorThemeHold(files, {cwd}) {
941
1356
  await releaseAll();
942
1357
  throw err;
943
1358
  }
944
- releases.push(async () => {
1359
+ releases.push({
945
1360
  // 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')) {
1361
+ // (src/themes/<theme>/), under the same lock; a check writes
1362
+ // nothing. Each theme's copy follows its own build: a theme this run
1363
+ // built (its build now matches its settings) gets its copy, whatever
1364
+ // a later theme did; one that was refused or failed keeps its copy.
1365
+ // True when the copy still needs its font stylesheet from the
1366
+ // theme's settings (written in finish).
1367
+ copy: (/** @type {boolean} */ refused) => {
1368
+ if (process.argv.includes('--check')) return false;
1369
+ // This run built the theme: its outputs were written since the
1370
+ // lock was taken, or the run ended well. A refused or failed
1371
+ // compile writes no copy.
1372
+ const f = themeFiles(root, slug);
1373
+ const written = [f.css, f.js].some(x => {
1374
+ try {
1375
+ return fs.statSync(x).mtimeMs >= heldAt;
1376
+ } catch {
1377
+ return false;
1378
+ }
1379
+ });
1380
+ if (!written && (refused || (process.exitCode ?? 0) !== 0))
1381
+ return false;
948
1382
  try {
949
- writeAppCopyAt(root, slug, null);
1383
+ // From what was just built, its font stylesheets from the fonts
1384
+ // the theme loads now.
1385
+ if (copyCtx) writeAppCopy(copyCtx, slug, loadTheme(root, slug));
1386
+ else writeAppCopyAt(root, slug, null);
1387
+ } catch {
1388
+ try {
1389
+ writeAppCopyAt(root, slug, null);
1390
+ } catch {
1391
+ // A theme that does not load keeps its copy as it was.
1392
+ }
1393
+ }
1394
+ return false;
1395
+ },
1396
+ finish: async () => {
1397
+ if (!process.argv.includes('--check')) {
950
1398
  // The first write of the app copy is named once (the compile's
951
1399
  // own JSON envelope is the stable command's).
952
1400
  const wrote = takeFirstAppCopy(root, slug);
@@ -954,17 +1402,169 @@ export async function editorThemeHold(files, {cwd}) {
954
1402
  humanWarn(
955
1403
  `wrote ${slug}'s app copy, a local theme \`theme add ${slug}\` takes, written again after every build: ${wrote.join(', ')}`,
956
1404
  );
957
- } catch {
958
- // A theme that does not load keeps its copy as it was.
1405
+ // A copy a file or a link keeps behind, or one that could not be
1406
+ // written, is said, as set and show say it.
1407
+ const stale = appCopyStale(root, slug).appCopyStale;
1408
+ if (stale) humanWarn(stale);
959
1409
  }
960
- }
961
- release();
962
- await done;
1410
+ release();
1411
+ await done;
1412
+ },
963
1413
  });
964
1414
  }
965
1415
  return releaseAll;
966
1416
  }
967
1417
 
1418
+ /** @param {string} p a path as typed, with Windows separators read as `/` */
1419
+ const slashed = p => String(p).replace(/\\/g, '/');
1420
+
1421
+ /**
1422
+ * Whether a file in an app copy folder is the editor's: its mark, or the
1423
+ * header the build writes. A missing file counts (it would be the copy's).
1424
+ * @param {string} file
1425
+ */
1426
+ function isCopyFile(file) {
1427
+ try {
1428
+ const head = fs.readFileSync(file, 'utf8').slice(0, 600);
1429
+ return (
1430
+ head.includes(APP_COPY_MARK) ||
1431
+ /@generated by `astryx theme build`/.test(head)
1432
+ );
1433
+ } catch {
1434
+ return true;
1435
+ }
1436
+ }
1437
+
1438
+ /**
1439
+ * The editor theme whose app copy source a file is
1440
+ * (`src/themes/<t>/<t>Theme.ts`, with themes/<t>/ an editor theme), or
1441
+ * null.
1442
+ * @param {string} root the project root
1443
+ * @param {string} file absolute
1444
+ * @returns {{root: string, slug: string} | null}
1445
+ */
1446
+ function appCopyTheme(root, file) {
1447
+ const rel = path.relative(root, file).split(path.sep);
1448
+ if (rel.length !== 4 || rel[0] !== 'src' || rel[1] !== 'themes') return null;
1449
+ const slug = rel[2];
1450
+ if (rel[3] !== `${camel(slug)}Theme.ts`) return null;
1451
+ return fs.existsSync(themeFiles(root, slug).settings) ? {root, slug} : null;
1452
+ }
1453
+
1454
+ /**
1455
+ * For `compile` of an editor theme: `appCopyStale` for its result data when
1456
+ * the app copy is behind (the same line set and show give); null for any
1457
+ * other file or a current copy.
1458
+ * @param {string} file the path as given
1459
+ * @param {string} cwd
1460
+ * @param {{check: boolean, upToDate?: boolean}} opts `upToDate`: the
1461
+ * check's own verdict on the theme's outputs
1462
+ * @returns {Promise<Record<string, unknown> | null>}
1463
+ */
1464
+ export async function editorBuildExtra(file, cwd, opts) {
1465
+ const root = projectRoot(cwd);
1466
+ const abs = path.resolve(cwd, file);
1467
+ const own = editorThemeAt(root, abs);
1468
+ const slug =
1469
+ appCopyTheme(root, abs)?.slug ??
1470
+ (own && editorOwnFile(own.slug, file) ? own.slug : null);
1471
+ if (!slug) return null;
1472
+ if (opts.check) {
1473
+ // A theme output that is stale is the check's own finding (it names
1474
+ // the file); the copy is compared only with a current build.
1475
+ if (opts.upToDate === false) return null;
1476
+ /** @type {string[] | null} */
1477
+ let fonts;
1478
+ try {
1479
+ fonts = copyFontsCss(await context(root), slug, loadTheme(root, slug));
1480
+ } catch {
1481
+ fonts = null;
1482
+ }
1483
+ const behind = appCopyBehind(root, slug, fonts);
1484
+ return behind
1485
+ ? {
1486
+ upToDate: false,
1487
+ code: 'ERR_THEME_STALE',
1488
+ // Something of yours in the way: its own line says what to move;
1489
+ // no editor command writes over it.
1490
+ appCopyStale: appCopyStale(root, slug).appCopyStale
1491
+ ? `${slug}'s app copy in src/themes/${slug}/ is not up to date: ${behind}`
1492
+ : `${slug}'s app copy in src/themes/${slug}/ is not up to date: ${behind}. ${runLine(root, `experimental theme editor show ${slug}`)} writes it again`,
1493
+ }
1494
+ : null;
1495
+ }
1496
+ const stale = appCopyStale(root, slug).appCopyStale;
1497
+ return stale ? {appCopyStale: stale} : null;
1498
+ }
1499
+
1500
+ /**
1501
+ * For `compile` of an editor theme's own source (themes/<t>/<t>.theme.ts):
1502
+ * the one color mode a light-only or dark-only theme has, which its source
1503
+ * cannot say, for the build to write into the built module. Null for any
1504
+ * other file (the app copy's source re-exports it from its build).
1505
+ * @param {string} file the path as given
1506
+ * @param {string} cwd
1507
+ * @returns {['light'] | ['dark'] | null}
1508
+ */
1509
+ export function editorColorModes(file, cwd) {
1510
+ const root = projectRoot(cwd);
1511
+ const at = editorThemeAt(root, path.resolve(cwd, file));
1512
+ if (!at || path.basename(file) !== `${at.slug}.theme.ts`) return null;
1513
+ return buildColorModes(at.root, at.slug);
1514
+ }
1515
+
1516
+ /**
1517
+ * For `compile` of an editor theme (themes/<t>/ or its app copy's source):
1518
+ * whether a font family is loaded by the app copy's font stylesheet as
1519
+ * this compile writes it (src/themes/<t>/<t>.fonts.css, from the fonts the
1520
+ * theme loads now: an @fontsource package by its name, a stylesheet by the
1521
+ * `family=` it requests), so the build's "named but not loaded" notice
1522
+ * leaves it out. Null for any other file.
1523
+ * @param {string} file the path as given
1524
+ * @param {string} cwd
1525
+ * @returns {Promise<((family: string) => boolean) | null>}
1526
+ */
1527
+ export async function editorLoadedFonts(file, cwd) {
1528
+ const root = projectRoot(cwd);
1529
+ const abs = path.resolve(cwd, file);
1530
+ const slug =
1531
+ appCopyTheme(root, abs)?.slug ??
1532
+ (editorThemeAt(root, abs) &&
1533
+ editorOwnFile(editorThemeAt(root, abs)?.slug ?? '', file)
1534
+ ? editorThemeAt(root, abs)?.slug
1535
+ : null);
1536
+ if (!slug) return null;
1537
+ // The stylesheets the copy's font file imports once this compile has
1538
+ // written it: from the fonts the theme loads now, as the copy write
1539
+ // computes them.
1540
+ /** @type {string[]} */
1541
+ let specs;
1542
+ try {
1543
+ specs = copyFontsCss(await context(root), slug, loadTheme(root, slug));
1544
+ } catch {
1545
+ return null;
1546
+ }
1547
+ const copyDir = path.join(root, 'src', 'themes', slug);
1548
+ const from = fs.existsSync(copyDir) ? copyDir : root;
1549
+ /** @type {Set<string>} */
1550
+ const loaded = new Set();
1551
+ for (const spec of specs)
1552
+ for (const f of importedFontFamilies(spec, from)) loaded.add(f);
1553
+ // A family the build names whose @fontsource package is installed: the
1554
+ // copy written after this build imports that package (copyFontsCss).
1555
+ const req = createRequire(path.join(root, 'package.json'));
1556
+ /** @param {string} family */
1557
+ const installed = family => {
1558
+ try {
1559
+ req.resolve(`${fontsourcePackageOf(family)}/package.json`);
1560
+ return true;
1561
+ } catch {
1562
+ return false;
1563
+ }
1564
+ };
1565
+ return family => loaded.has(fontKey(family)) || installed(family);
1566
+ }
1567
+
968
1568
  /**
969
1569
  * For `compile` of a file that is not there: when it is an editor theme's
970
1570
  * own source (`themes/<t>/<t>.theme.ts` beside its settings), the command
@@ -974,6 +1574,37 @@ export async function editorThemeHold(files, {cwd}) {
974
1574
  * @returns {{name: string, reason: string} | null}
975
1575
  */
976
1576
  export function editorMissingSource(file, cwd) {
1577
+ // The app copy's source of an editor theme, its folder gone: the editor
1578
+ // writes it again.
1579
+ const copy = appCopyTheme(projectRoot(cwd), path.resolve(cwd, slashed(file)));
1580
+ if (copy)
1581
+ // A copy file named with Windows separators: the theme is what
1582
+ // compiles (the copy follows); with the copy gone, show writes it.
1583
+ return fs.existsSync(path.join(copy.root, 'src', 'themes', copy.slug))
1584
+ ? {
1585
+ name: runLine(
1586
+ cwd,
1587
+ `experimental theme compile ${
1588
+ path
1589
+ .relative(
1590
+ cwd,
1591
+ path.join(
1592
+ copy.root,
1593
+ 'themes',
1594
+ copy.slug,
1595
+ `${copy.slug}.theme.ts`,
1596
+ ),
1597
+ )
1598
+ .split(path.sep)
1599
+ .join('/') || `themes/${copy.slug}/${copy.slug}.theme.ts`
1600
+ }`,
1601
+ ),
1602
+ reason: `${copy.slug}'s app copy follows every compile of the theme`,
1603
+ }
1604
+ : {
1605
+ name: runLine(cwd, `experimental theme editor show ${copy.slug}`),
1606
+ reason: `the editor writes ${copy.slug}'s app copy in src/themes/${copy.slug}/ again`,
1607
+ };
977
1608
  const at = editorThemeAt(projectRoot(cwd), path.resolve(cwd, file));
978
1609
  if (!at || path.basename(file) !== `${at.slug}.theme.ts`) return null;
979
1610
  return {