@astryxdesign/cli 0.7.16 → 0.7.17-canary.g9a957aa

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 (229) hide show
  1. package/README.md +12 -23
  2. package/api/build/_adapter.d.mts +5 -0
  3. package/api/build/_adapter.mjs +49 -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 +2 -1
  7. package/api/build/kit/rank.mjs +47 -9
  8. package/api/build/kit/weights.d.mts +16 -3
  9. package/api/build/kit/weights.json +1 -1
  10. package/api/build/kit/weights.mjs +238 -31
  11. package/api/build/kit/word-model.d.mts +23 -0
  12. package/api/build/kit/word-model.mjs +46 -0
  13. package/api/build/kit/words.d.mts +32 -0
  14. package/api/build/kit/words.mjs +49 -1
  15. package/api/component/__fixtures__/resolutions/modules.txt +4 -5
  16. package/api/component/list/list.d.mts +1 -1
  17. package/api/component/list/list.mjs +48 -5
  18. package/api/discover/_adapter.d.mts +1 -1
  19. package/api/docs/_adapter.d.mts +26 -2
  20. package/api/docs/_adapter.mjs +217 -5
  21. package/api/docs/docs.d.mts +3 -0
  22. package/api/docs/docs.doc.mjs +7 -1
  23. package/api/docs/docs.mjs +12 -0
  24. package/api/docs/docs.type.d.mts +5 -0
  25. package/api/docs/docs.type.mjs +2 -0
  26. package/api/docs/node/node.mjs +1 -1
  27. package/api/doctor/_adapter.d.mts +27 -5
  28. package/api/doctor/_adapter.mjs +77 -6
  29. package/api/doctor/built-checks.mjs +38 -13
  30. package/api/doctor/doctor.d.mts +4 -3
  31. package/api/doctor/doctor.mjs +14 -7
  32. package/api/doctor/theme-checks.mjs +121 -17
  33. package/api/render/_adapter.mjs +491 -186
  34. package/api/render/render.doc.mjs +10 -3
  35. package/api/render/render.type.d.mts +30 -0
  36. package/api/render/render.type.mjs +19 -0
  37. package/api/search/ORDERING.md +1 -1
  38. package/api/search/_candidate-score.mjs +51 -32
  39. package/api/search/_fit-order.mjs +3 -3
  40. package/api/search/_result.d.mts +5 -5
  41. package/api/search/_score.d.mts +6 -1
  42. package/api/search/_score.mjs +15 -3
  43. package/api/search/search.d.mts +1 -1
  44. package/api/theme/_adapter.mjs +10 -0
  45. package/api/theme/build/build.d.mts +38 -0
  46. package/api/theme/build/build.mjs +144 -24
  47. package/api/theme/theme.type.d.mts +10 -0
  48. package/api/theme/theme.type.mjs +7 -1
  49. package/api/theme-edit/_adapter.d.mts +35 -0
  50. package/api/theme-edit/_adapter.mjs +477 -41
  51. package/api/theme-edit/_check.d.mts +28 -22
  52. package/api/theme-edit/_check.mjs +1077 -200
  53. package/api/theme-edit/_compiler.mjs +9 -0
  54. package/api/theme-edit/_context.mjs +60 -3
  55. package/api/theme-edit/_css.mjs +21 -1
  56. package/api/theme-edit/_edit.d.mts +22 -0
  57. package/api/theme-edit/_edit.mjs +932 -82
  58. package/api/theme-edit/_how-commands.d.mts +49 -0
  59. package/api/theme-edit/_how-commands.mjs +386 -1
  60. package/api/theme-edit/_how-question.d.mts +56 -0
  61. package/api/theme-edit/_how-question.mjs +3509 -168
  62. package/api/theme-edit/_how.mjs +104 -4
  63. package/api/theme-edit/_marks.d.mts +31 -5
  64. package/api/theme-edit/_marks.mjs +57 -5
  65. package/api/theme-edit/_normal.d.mts +137 -0
  66. package/api/theme-edit/_normal.mjs +612 -0
  67. package/api/theme-edit/_palette.mjs +10 -4
  68. package/api/theme-edit/_parts.d.mts +4 -0
  69. package/api/theme-edit/_parts.mjs +28 -2
  70. package/api/theme-edit/_preview-app.d.mts +7 -1
  71. package/api/theme-edit/_preview-app.mjs +191 -15
  72. package/api/theme-edit/_preview.d.mts +64 -8
  73. package/api/theme-edit/_preview.mjs +401 -83
  74. package/api/theme-edit/_settings.d.mts +139 -5
  75. package/api/theme-edit/_settings.mjs +1659 -112
  76. package/api/theme-edit/_tables.mjs +17 -1
  77. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  78. package/api/theme-edit/theme-edit.type.mjs +4 -1
  79. package/api/theme-edit/themeEditPreviewApp.doc.mjs +2 -2
  80. package/assets/codemods/run-codemod.mjs +73 -6
  81. package/assets/codemods/transforms/next/index.mjs +11 -1
  82. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  83. package/assets/docs/author-a-theme.doc.mjs +1 -0
  84. package/assets/docs/theme.doc.dense.mjs +2 -2
  85. package/assets/docs/theme.doc.mjs +1 -1
  86. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  87. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  88. package/assets/docs/tree/render-pages.doc.mjs +43 -2
  89. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  90. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  91. package/assets/docs/use-a-theme.doc.mjs +1 -1
  92. package/assets/docs.generated.json +2 -2
  93. package/assets/self-audit.generated.json +1 -1
  94. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  95. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  96. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  97. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  98. package/assets/templates/components.generated.json +1 -1
  99. package/assets/templates/pages/ai-chat/template.doc.mjs +8 -4
  100. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  101. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  102. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  103. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  104. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  105. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  106. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  107. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  108. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  109. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  110. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  111. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  112. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  113. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  114. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  115. package/assets/templates/pages/editor/template.doc.mjs +1 -7
  116. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  117. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  118. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  119. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  120. package/assets/templates/pages/ide/page.tsx +24 -2
  121. package/assets/templates/pages/library/template.doc.mjs +4 -1
  122. package/assets/templates/pages/login/template.doc.mjs +2 -1
  123. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  124. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  125. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  126. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  127. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  128. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  129. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  130. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  131. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  132. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  133. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  134. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  135. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  136. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  137. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  138. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  139. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -1
  140. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  141. package/authoring/debug/parse.d.mts +3 -3
  142. package/authoring/doctypes/_schema.d.mts +3 -3
  143. package/authoring/doctypes/command/command.doc.mjs +1 -1
  144. package/authoring/doctypes/template/template.doc.mjs +1 -1
  145. package/authoring/doctypes/template/type.ts +9 -0
  146. package/clients/cli/bin/astryx.mjs +6 -9
  147. package/clients/cli/build-id.generated.json +1 -1
  148. package/clients/cli/commands/component.doc.mjs +1 -1
  149. package/clients/cli/commands/component.mjs +20 -2
  150. package/clients/cli/commands/docs.doc.mjs +7 -1
  151. package/clients/cli/commands/docs.mjs +3 -1
  152. package/clients/cli/commands/doctor.mjs +22 -14
  153. package/clients/cli/commands/experimental/render.mjs +32 -1
  154. package/clients/cli/commands/experimental/theme.mjs +27 -2
  155. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  156. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  157. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  158. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  159. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  160. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  161. package/clients/cli/commands/search.doc.mjs +2 -1
  162. package/clients/cli/commands/search.mjs +6 -1
  163. package/clients/cli/commands/template.mjs +4 -2
  164. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  165. package/clients/cli/commands/theme.doc.mjs +1 -1
  166. package/clients/cli/commands/theme.mjs +138 -3
  167. package/clients/cli/commands/upgrade.mjs +42 -7
  168. package/clients/cli/index.mjs +1 -1
  169. package/clients/cli/lib/component-format.mjs +205 -98
  170. package/clients/cli/lib/hook-format.mjs +9 -19
  171. package/clients/cli/manifest.generated.json +1 -1
  172. package/foundation/agent-docs/agent-docs.mjs +1 -1
  173. package/foundation/config/theme-state.d.mts +13 -1
  174. package/foundation/config/theme-state.mjs +47 -7
  175. package/foundation/discovery/component-discovery.d.mts +11 -0
  176. package/foundation/discovery/component-discovery.mjs +27 -2
  177. package/foundation/discovery/component-replacement.mjs +130 -1
  178. package/foundation/discovery/entry-name-files.d.mts +43 -0
  179. package/foundation/discovery/entry-name-files.mjs +264 -0
  180. package/foundation/discovery/hook-discovery.d.mts +8 -2
  181. package/foundation/discovery/hook-discovery.mjs +20 -7
  182. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  183. package/foundation/discovery/runner-allowlist.mjs +2 -0
  184. package/foundation/discovery/template-adapter.d.mts +52 -0
  185. package/foundation/discovery/template-adapter.mjs +169 -0
  186. package/foundation/discovery/theme-imports.d.mts +14 -0
  187. package/foundation/discovery/theme-imports.mjs +43 -1
  188. package/foundation/doc-compiler/import.mjs +7 -1
  189. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  190. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  191. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  192. package/foundation/fs/cache-eviction.d.mts +21 -0
  193. package/foundation/fs/cache-eviction.mjs +73 -0
  194. package/foundation/fs/gather-cache.d.mts +25 -4
  195. package/foundation/fs/gather-cache.mjs +138 -52
  196. package/foundation/fs/jiti-cache.d.mts +19 -1
  197. package/foundation/fs/jiti-cache.mjs +130 -6
  198. package/foundation/fs/kept-tree.d.mts +60 -0
  199. package/foundation/fs/kept-tree.mjs +154 -0
  200. package/foundation/fs/module-loader.d.mts +18 -2
  201. package/foundation/fs/module-loader.mjs +92 -20
  202. package/foundation/identity/provider-identity.d.mts +6 -0
  203. package/foundation/identity/provider-identity.mjs +10 -1
  204. package/foundation/render/file-header.d.mts +66 -0
  205. package/foundation/render/file-header.mjs +118 -0
  206. package/foundation/render/host-answer.d.mts +18 -0
  207. package/foundation/render/host-answer.mjs +46 -0
  208. package/foundation/render/host-exit.d.mts +1 -0
  209. package/foundation/render/host-exit.mjs +6 -1
  210. package/foundation/render/html-reader.d.mts +55 -2
  211. package/foundation/render/html-reader.mjs +145 -26
  212. package/foundation/render/markup-edits.d.mts +57 -0
  213. package/foundation/render/markup-edits.mjs +182 -0
  214. package/foundation/render/page-host.mjs +29 -12
  215. package/foundation/render/page-worker.mjs +79 -10
  216. package/foundation/render/render-page.d.mts +34 -2
  217. package/foundation/render/render-page.mjs +210 -29
  218. package/foundation/render/source-block.d.mts +73 -6
  219. package/foundation/render/source-block.mjs +170 -19
  220. package/foundation/render/ts-paths.d.mts +4 -1
  221. package/foundation/render/ts-paths.mjs +7 -3
  222. package/foundation/response/error-codes.d.mts +2 -1
  223. package/foundation/response/error-codes.doc.mjs +7 -1
  224. package/foundation/response/error-codes.mjs +7 -0
  225. package/foundation/text/string-utils.d.mts +3 -2
  226. package/foundation/text/string-utils.mjs +16 -3
  227. package/foundation/text/suggest-order.d.mts +9 -0
  228. package/foundation/text/suggest-order.mjs +13 -0
  229. package/package.json +6 -6
@@ -15,7 +15,8 @@
15
15
  import {createHash} from 'node:crypto';
16
16
  import * as fs from 'node:fs';
17
17
  import * as path from 'node:path';
18
- import {spawnSync} from 'node:child_process';
18
+ import {spawn} from 'node:child_process';
19
+ import * as os from 'node:os';
19
20
  import {fileURLToPath, pathToFileURL} from 'node:url';
20
21
  import {
21
22
  SAMPLE_PAGE,
@@ -32,7 +33,6 @@ import {
32
33
  } from './_parts.mjs';
33
34
  import {OVERVIEW_SECTIONS} from './_overview.mjs';
34
35
  import {
35
- FOLDER_JUNK,
36
36
  folderIsEmpty,
37
37
  camel,
38
38
  fontPackages,
@@ -1417,7 +1417,7 @@ const camelName = camel;
1417
1417
  * @param {string} html absolute
1418
1418
  * @param {string} png absolute
1419
1419
  * @param {'light' | 'dark'} mode
1420
- * @returns {Promise<{ok: true, file: string, cut?: boolean} | {ok: false, reason: string}>} `cut` when the picture is of the window, not the whole page
1420
+ * @returns {Promise<{ok: true, file: string, cut?: boolean} | {ok: false, reason: string, stopped?: string}>} `cut` when the picture is of the window, not the whole page
1421
1421
  */
1422
1422
  export async function screenshot(project, html, png, mode) {
1423
1423
  /** What was found and why it could not take the picture. @type {string[]} */
@@ -1462,30 +1462,52 @@ export async function screenshot(project, html, png, mode) {
1462
1462
  }
1463
1463
  const chrome = chromeBinary();
1464
1464
  if (chrome) {
1465
- const r = spawnSync(
1466
- chrome,
1467
- [
1468
- '--headless=new',
1469
- '--disable-gpu',
1470
- '--no-sandbox',
1471
- '--hide-scrollbars',
1472
- `--screenshot=${png}`,
1473
- '--window-size=1280,2400',
1474
- ...(mode === 'dark' ? ['--force-dark-mode'] : []),
1475
- pathToFileURL(html).href,
1476
- ],
1477
- {encoding: 'utf8', timeout: 60_000},
1478
- );
1465
+ // Run without blocking, so a stop (Ctrl-C, SIGTERM) reaches this
1466
+ // process while the browser runs: the browser is stopped with it, and
1467
+ // the stop is reported as a stop, never as a browser that failed.
1468
+ const r = await runBrowser(chrome, [
1469
+ '--headless=new',
1470
+ '--disable-gpu',
1471
+ '--no-sandbox',
1472
+ '--hide-scrollbars',
1473
+ `--screenshot=${png}`,
1474
+ '--window-size=1280,2400',
1475
+ ...(mode === 'dark' ? ['--force-dark-mode'] : []),
1476
+ pathToFileURL(html).href,
1477
+ ]);
1478
+ if (r.stoppedBy)
1479
+ return {
1480
+ ok: false,
1481
+ stopped: r.stoppedBy,
1482
+ reason: `the pictures were stopped (${r.stoppedBy}) before the browser finished`,
1483
+ };
1484
+ // The editor's own bound, not an outside kill: said so, with a retry.
1485
+ if (r.timedOut)
1486
+ return {
1487
+ ok: false,
1488
+ reason: `the browser (${chrome}) did not finish the picture within 60 seconds, so it was stopped; run the command again, or open the .html beside the picture to look without one`,
1489
+ };
1490
+ // A picture folder it cannot write: the folder, never the browser.
1491
+ try {
1492
+ fs.accessSync(path.dirname(png), fs.constants.W_OK);
1493
+ } catch (e) {
1494
+ return {
1495
+ ok: false,
1496
+ reason: `${shown(project.cwd, path.dirname(png))}/ cannot be written (${/** @type {any} */ (e)?.code ?? 'no access'}), so no picture was taken; fix its permissions, then run the command again`,
1497
+ };
1498
+ }
1479
1499
  // Chrome alone takes the window, not the whole page.
1480
1500
  if (r.status === 0 && fs.existsSync(png))
1481
1501
  return {ok: true, file: shown(project.cwd, png), cut: true};
1482
1502
  failed.push(
1483
1503
  `${chrome}: ${firstLine(
1484
1504
  r.error ??
1485
- (r.stderr ||
1486
- (r.signal
1487
- ? `stopped by ${r.signal}`
1488
- : `exit ${r.status}, and no picture was written`)),
1505
+ // A browser stopped by a signal says so first: its stderr opens
1506
+ // with unrelated noise (dbus) on Linux.
1507
+ (r.signal
1508
+ ? `stopped by ${r.signal}`
1509
+ : meaningfulLine(r.stderr) ||
1510
+ `exit ${r.status}, and no picture was written`),
1489
1511
  )}`,
1490
1512
  );
1491
1513
  }
@@ -1527,7 +1549,25 @@ function chromeBinary() {
1527
1549
 
1528
1550
  /** The first line of every file the editor writes into a theme's app copy. */
1529
1551
  export {APP_COPY_MARK} from './_marks.mjs';
1530
- import {APP_COPY_MARK} from './_marks.mjs';
1552
+ import {APP_COPY_MARK, appCopyMark} from './_marks.mjs';
1553
+ import {builtColorMode} from '../../foundation/discovery/theme-imports.mjs';
1554
+
1555
+ /**
1556
+ * The font stylesheets a theme's app copy imports, from the fonts the theme
1557
+ * loads now (an installed font package, the base theme's fonts).
1558
+ * @param {import('./_settings.mjs').Ctx} ctx
1559
+ * @param {string} slug
1560
+ * @param {import('./_settings.mjs').ThemeData} data
1561
+ * @returns {string[]}
1562
+ */
1563
+ export function copyFontsCss(ctx, slug, data) {
1564
+ return themeImports(
1565
+ ctx,
1566
+ slug,
1567
+ data,
1568
+ path.join(ctx.project.cwd, 'src', 'themes', slug),
1569
+ ).css.slice(1);
1570
+ }
1531
1571
 
1532
1572
  /**
1533
1573
  * The app's copy of an editor theme: a standard local theme in
@@ -1545,38 +1585,26 @@ import {APP_COPY_MARK} from './_marks.mjs';
1545
1585
  * @returns {string | null}
1546
1586
  */
1547
1587
  export function writeAppCopy(ctx, slug, data) {
1548
- return writeAppCopyAt(
1549
- ctx.project.cwd,
1550
- slug,
1551
- themeImports(
1552
- ctx,
1553
- slug,
1554
- data,
1555
- path.join(ctx.project.cwd, 'src', 'themes', slug),
1556
- ).css.slice(1),
1557
- );
1588
+ return writeAppCopyAt(ctx.project.cwd, slug, copyFontsCss(ctx, slug, data));
1558
1589
  }
1559
1590
 
1560
1591
  /**
1561
- * {@link writeAppCopy} with the font stylesheets given, or null to keep the
1562
- * copy's own (a compile, which changes no setting).
1592
+ * The files of a theme's app copy as the editor would write them now, by
1593
+ * name; null with no build to copy. Font stylesheets as given, or null for
1594
+ * the copy's own.
1563
1595
  * @param {string} cwd
1564
1596
  * @param {string} slug
1565
1597
  * @param {string[] | null} fontsCss
1566
- * @returns {string | null}
1598
+ * @returns {Record<string, string> | null}
1567
1599
  */
1568
- export function writeAppCopyAt(cwd, slug, fontsCss) {
1600
+ function appCopyFiles(cwd, slug, fontsCss) {
1569
1601
  const f = themeFiles(cwd, slug);
1570
1602
  const dir = path.join(cwd, 'src', 'themes', slug);
1571
1603
  const name = `${camel(slug)}Theme`;
1572
- const first = !fs.existsSync(path.join(dir, `${name}.doc.mjs`));
1573
1604
  if (!fs.existsSync(f.js) || !fs.existsSync(f.css)) return null;
1574
- // The copy's source, as the stable build's header names it.
1575
1605
  const source = `src/themes/${slug}/${name}.ts`;
1576
- // Only the editor's own copy is written over.
1577
- if (appCopyBlocker(cwd, slug) !== null) return null;
1578
1606
  const head = (/** @type {string} */ open, /** @type {string} */ close) =>
1579
- `${open} ${APP_COPY_MARK} after every build of themes/${slug}/; do not edit it here. Change the theme with the editor. ${close}\n`;
1607
+ `${open} ${appCopyMark(slug)} after every build of themes/${slug}/; do not edit it here. Change the theme with the editor. ${close}\n`;
1580
1608
  const keptFonts = path.join(dir, `${slug}.fonts.css`);
1581
1609
  const fonts =
1582
1610
  fontsCss ??
@@ -1598,27 +1626,112 @@ export function writeAppCopyAt(cwd, slug, fontsCss) {
1598
1626
  if (fs.existsSync(from))
1599
1627
  built[`${slug}${ext}`] = asBuilt(fs.readFileSync(from, 'utf8'));
1600
1628
  }
1629
+ // A light-only or dark-only theme's build exports its one color mode
1630
+ // (`colorModes`); the copy's source re-exports it, so a build of the copy
1631
+ // writes it too and `theme add` reads it.
1632
+ const oneMode = builtColorMode(f.js).mode;
1601
1633
  const files = {
1602
1634
  [`${name}.doc.mjs`]: `${head('//', '')}\n/** @type {import('@astryxdesign/cli/authoring').ThemeDoc} */\nexport default {\n type: 'theme',\n name: ${JSON.stringify(slug)},\n displayName: ${JSON.stringify(slug)},\n description: ${JSON.stringify(`The ${slug} theme, made with the theme editor (themes/${slug}/).`)},\n maintained: false,\n};\n`,
1603
- [`${name}.ts`]: `${head('//', '')}export {${name}} from './${slug}';\n`,
1635
+ [`${name}.ts`]: `${head('//', '')}export {${name}${oneMode ? ', colorModes' : ''}} from './${slug}';\n`,
1604
1636
  ...built,
1605
- ...(fonts.length
1606
- ? {
1607
- [`${slug}.fonts.css`]: `${head('/*', '*/')}${fonts.map(c => `@import ${JSON.stringify(c)};`).join('\n')}\n`,
1608
- }
1609
- : {}),
1637
+ // Always there, so the app theme module's import of it holds when the
1638
+ // theme's fonts change (none: a comment only).
1639
+ [`${slug}.fonts.css`]: fonts.length
1640
+ ? `${head('/*', '*/')}${fonts.map(c => `@import ${JSON.stringify(c)};`).join('\n')}\n`
1641
+ : `${head('/*', '*/')}/* No font stylesheets: the theme uses fonts the system has. */\n`,
1610
1642
  };
1611
- fs.mkdirSync(dir, {recursive: true});
1643
+ return files;
1644
+ }
1645
+
1646
+ /**
1647
+ * Why a theme's app copy is behind its build, or null when it is current:
1648
+ * something in its way (appCopyStale), a copy file missing, or a file
1649
+ * whose content is not what the editor would write now.
1650
+ * @param {string} cwd
1651
+ * @param {string} slug
1652
+ * @param {string[] | null} [fontsCss] the font stylesheets the copy should
1653
+ * import now (null: the copy's own)
1654
+ * @returns {string | null}
1655
+ */
1656
+ export function appCopyBehind(cwd, slug, fontsCss = null) {
1657
+ const stale = appCopyStale(cwd, slug).appCopyStale;
1658
+ if (stale) return stale;
1659
+ const files = appCopyFiles(cwd, slug, fontsCss);
1660
+ if (!files) return null;
1661
+ const dir = path.join(cwd, 'src', 'themes', slug);
1662
+ if (!fs.existsSync(dir))
1663
+ return `the app copy in src/themes/${slug}/ is gone; the editor writes it again`;
1612
1664
  for (const [n, text] of Object.entries(files)) {
1613
- const file = path.join(dir, n);
1614
- if (fs.existsSync(file) && fs.readFileSync(file, 'utf8') === text) continue;
1615
- writeWhole(file, text);
1665
+ /** @type {string} */
1666
+ let now;
1667
+ try {
1668
+ now = fs.readFileSync(path.join(dir, n), 'utf8');
1669
+ } catch {
1670
+ return `src/themes/${slug}/${n} is missing from the app copy`;
1671
+ }
1672
+ // Line endings as a checkout writes them (CRLF on Windows) are the
1673
+ // same content, as the outputs check reads them.
1674
+ const lf = (/** @type {string} */ x) => x.replace(/\r\n/g, '\n');
1675
+ if (lf(now) !== lf(text))
1676
+ return `src/themes/${slug}/${n} is not the current build of themes/${slug}/`;
1616
1677
  }
1617
- // A file an earlier copy had (a font stylesheet no longer needed) goes;
1618
- // system files stay.
1619
- for (const n of fs.readdirSync(dir))
1620
- if (!(n in files) && !FOLDER_JUNK.has(n))
1621
- fs.rmSync(path.join(dir, n), {force: true});
1678
+ return null;
1679
+ }
1680
+
1681
+ /**
1682
+ * {@link writeAppCopy} with the font stylesheets given, or null to keep the
1683
+ * copy's own (a compile, which changes no setting).
1684
+ * @param {string} cwd
1685
+ * @param {string} slug
1686
+ * @param {string[] | null} fontsCss
1687
+ * @returns {string | null}
1688
+ */
1689
+ export function writeAppCopyAt(cwd, slug, fontsCss) {
1690
+ const f = themeFiles(cwd, slug);
1691
+ const dir = path.join(cwd, 'src', 'themes', slug);
1692
+ const name = `${camel(slug)}Theme`;
1693
+ const first = !fs.existsSync(path.join(dir, `${name}.doc.mjs`));
1694
+ if (!fs.existsSync(f.js) || !fs.existsSync(f.css)) return null;
1695
+ // Only the editor's own copy is written over.
1696
+ if (appCopyBlocker(cwd, slug) !== null) return null;
1697
+ const files = appCopyFiles(cwd, slug, fontsCss);
1698
+ if (!files) return null;
1699
+ const failKey = `${path.resolve(cwd)}\0${slug}`;
1700
+ try {
1701
+ fs.mkdirSync(dir, {recursive: true});
1702
+ for (const [n, text] of Object.entries(files)) {
1703
+ const file = path.join(dir, n);
1704
+ if (fs.existsSync(file) && fs.readFileSync(file, 'utf8') === text)
1705
+ continue;
1706
+ writeWhole(file, text);
1707
+ }
1708
+ // A copy's own file this build no longer writes goes; anything else in
1709
+ // the folder (yours, or the system's) stays as it is.
1710
+ for (const n of fs.readdirSync(dir))
1711
+ if (appCopyNames(slug).has(n) && !(n in files))
1712
+ fs.rmSync(path.join(dir, n), {force: true});
1713
+ } catch (e) {
1714
+ // A copy that cannot be written is said, with the system's reason;
1715
+ // the change itself stands.
1716
+ const code = /** @type {any} */ (e)?.code ?? 'error';
1717
+ // The folder that failed: the copy's, or a parent it could not be
1718
+ // made in.
1719
+ const failed = /** @type {any} */ (e)?.path
1720
+ ? path.resolve(String(/** @type {any} */ (e).path))
1721
+ : dir;
1722
+ const at = fs.existsSync(failed) ? failed : path.dirname(failed);
1723
+ const rel = path.relative(cwd, at).split(path.sep).join('/');
1724
+ COPY_FAILED.set(
1725
+ failKey,
1726
+ code === 'EACCES' || code === 'EPERM' || code === 'EROFS'
1727
+ ? `${rel} is not writable (${code})`
1728
+ : code === 'ENOSPC'
1729
+ ? `the disk is full (${code})`
1730
+ : `${rel} could not be written (${code})`,
1731
+ );
1732
+ return null;
1733
+ }
1734
+ COPY_FAILED.delete(failKey);
1622
1735
  // The first write is named in the command's result; later ones are not.
1623
1736
  if (first)
1624
1737
  FIRST_COPIES.set(
@@ -1633,13 +1746,48 @@ export function writeAppCopyAt(cwd, slug, fontsCss) {
1633
1746
  }
1634
1747
 
1635
1748
  /**
1636
- * Why the editor will not write a theme's app copy, or null when it will:
1637
- * the folder is not there yet, or its descriptor carries the editor's mark
1638
- * and it holds only the copy's own files (system files such as .DS_Store
1639
- * aside). Otherwise the name of what is in the way (a file of yours), or
1640
- * the folder itself when it is not the editor's copy. Built bytes are not
1641
- * compared, so a stable rebuild of the copy leaves it the editor's. The one
1642
- * test the copy's writer and `wiring` both use.
1749
+ * The note for a copy a file of yours keeps from being written, as a
1750
+ * result field: `{appCopyStale}` when one does, else nothing.
1751
+ * @param {string} cwd
1752
+ * @param {string} slug
1753
+ * @returns {{appCopyStale?: string}}
1754
+ */
1755
+ export function appCopyStale(cwd, slug) {
1756
+ const blocker =
1757
+ appCopyBlocker(cwd, slug) ??
1758
+ COPY_FAILED.get(`${path.resolve(cwd)}\0${slug}`) ??
1759
+ null;
1760
+ return blocker
1761
+ ? {
1762
+ appCopyStale: `the app copy in src/themes/${slug}/ was not updated: ${blocker}. The app keeps the theme as it was until that is out of the way; then any set or show writes it.`,
1763
+ }
1764
+ : {};
1765
+ }
1766
+
1767
+ /**
1768
+ * The names of a theme's app copy files.
1769
+ * @param {string} slug
1770
+ * @returns {Set<string>}
1771
+ */
1772
+ function appCopyNames(slug) {
1773
+ const name = `${camel(slug)}Theme`;
1774
+ return new Set([
1775
+ `${name}.doc.mjs`,
1776
+ `${name}.ts`,
1777
+ `${slug}.fonts.css`,
1778
+ ...APP_COPY_BUILT.map(ext => `${slug}${ext}`),
1779
+ ]);
1780
+ }
1781
+
1782
+ /**
1783
+ * Why the editor will not write a theme's app copy, or null when it will.
1784
+ * Only a file of yours AT one of the copy's names stops it: a descriptor,
1785
+ * source or font stylesheet without the editor's mark, or any copy file
1786
+ * while the descriptor is not the editor's. Other files there (a note, a
1787
+ * swap file, desktop.ini) are left as they are and the copy is written
1788
+ * around them. Built bytes are not compared, so a stable rebuild of the
1789
+ * copy leaves it the editor's. The one test the copy's writer, the change
1790
+ * results and `wiring` use. Returns the file in the way.
1643
1791
  * @param {string} cwd
1644
1792
  * @param {string} slug
1645
1793
  * @returns {string | null}
@@ -1648,37 +1796,89 @@ export function appCopyBlocker(cwd, slug) {
1648
1796
  const dir = path.join(cwd, 'src', 'themes', slug);
1649
1797
  const name = `${camel(slug)}Theme`;
1650
1798
  const rel = path.relative(cwd, dir).split(path.sep).join('/');
1799
+ // Nothing is written through a link on the way from the app to the copy
1800
+ // (src/, src/themes/, the copy's folder), as for themes/.
1801
+ for (const part of ['src', 'src/themes', rel]) {
1802
+ try {
1803
+ const st = fs.lstatSync(path.join(cwd, part));
1804
+ if (st.isSymbolicLink())
1805
+ return `${part} is a link (to ${fs.readlinkSync(path.join(cwd, part))}), and the editor writes nothing through a link`;
1806
+ // A file where a folder on the way goes: nothing is half-written.
1807
+ if (!st.isDirectory())
1808
+ return part === rel
1809
+ ? `${rel} is a file, where the copy's folder goes`
1810
+ : `${part} is a file, where a folder on the way to the copy goes`;
1811
+ } catch {
1812
+ break;
1813
+ }
1814
+ }
1815
+ try {
1816
+ if (!fs.lstatSync(dir).isDirectory())
1817
+ return `${rel} is a file, where the copy's folder goes`;
1818
+ } catch {
1819
+ // Not there yet: the copy makes it.
1820
+ }
1651
1821
  if (folderIsEmpty(dir)) return null;
1652
1822
  /** @type {string[]} */
1653
1823
  let names;
1654
1824
  try {
1655
1825
  names = fs.readdirSync(dir);
1656
- } catch {
1657
- return rel;
1658
- }
1659
- /** @type {boolean} */
1660
- let marked;
1661
- try {
1662
- marked = fs
1663
- .readFileSync(path.join(dir, `${name}.doc.mjs`), 'utf8')
1664
- .includes(APP_COPY_MARK);
1665
- } catch {
1666
- marked = false;
1826
+ } catch (e) {
1827
+ return `${rel} cannot be read (${/** @type {any} */ (e)?.code ?? 'error'})`;
1667
1828
  }
1668
- if (!marked) return rel;
1669
- const own = new Set([
1670
- `${name}.doc.mjs`,
1671
- `${name}.ts`,
1672
- `${slug}.fonts.css`,
1673
- ...APP_COPY_BUILT.map(ext => `${slug}${ext}`),
1674
- ]);
1675
- const other = names.find(n => !own.has(n) && !FOLDER_JUNK.has(n));
1676
- return other ? `${rel}/${other}` : null;
1829
+ /** @param {string} n */
1830
+ const marked = n => {
1831
+ try {
1832
+ return fs.readFileSync(path.join(dir, n), 'utf8').includes(APP_COPY_MARK);
1833
+ } catch {
1834
+ return false;
1835
+ }
1836
+ };
1837
+ const own = appCopyNames(slug);
1838
+ const present = names.filter(n => own.has(n));
1839
+ // A link at a copy name is never written through or replaced.
1840
+ for (const n of present)
1841
+ try {
1842
+ const st = fs.lstatSync(path.join(dir, n));
1843
+ if (st.isSymbolicLink())
1844
+ return `${rel}/${n} is a link (to ${fs.readlinkSync(path.join(dir, n))}), at a name the copy uses, and the editor writes nothing through a link`;
1845
+ if (st.isDirectory())
1846
+ return `${rel}/${n} is a folder, at a name the copy uses for a file`;
1847
+ } catch {
1848
+ // Gone since it was listed.
1849
+ }
1850
+ // The copy is the editor's when any of its marked files carries the
1851
+ // mark (one of them deleted is written back); then only a marked name
1852
+ // without the mark is yours.
1853
+ const markedNames = [`${name}.doc.mjs`, `${name}.ts`, `${slug}.fonts.css`];
1854
+ // A build output of the copy's source carries the build's own header.
1855
+ const generated = (/** @type {string} */ n) => {
1856
+ try {
1857
+ return /@generated by `astryx theme build`/.test(
1858
+ fs.readFileSync(path.join(dir, n), 'utf8').slice(0, 600),
1859
+ );
1860
+ } catch {
1861
+ return false;
1862
+ }
1863
+ };
1864
+ const ours =
1865
+ markedNames.some(n => present.includes(n) && marked(n)) ||
1866
+ (present.length > 0 &&
1867
+ present.every(n => !markedNames.includes(n) && generated(n)));
1868
+ const foreign = ours
1869
+ ? present.find(n => markedNames.includes(n) && !marked(n))
1870
+ : present[0];
1871
+ return foreign
1872
+ ? `${rel}/${foreign} is yours, at a name the copy uses, and the editor never writes over your files`
1873
+ : null;
1677
1874
  }
1678
1875
 
1679
1876
  /** The build outputs of a theme, by their ending after `<theme>`. */
1680
1877
  const APP_COPY_BUILT = ['.css', '.css.d.ts', '.js', '.d.ts', '.variants.d.ts'];
1681
1878
 
1879
+ /** Why the last write of a theme's app copy failed, by app and theme. @type {Map<string, string>} */
1880
+ const COPY_FAILED = new Map();
1881
+
1682
1882
  /** App copies written for the first time in this process, by app and theme. @type {Map<string, string[]>} */
1683
1883
  const FIRST_COPIES = new Map();
1684
1884
 
@@ -1695,3 +1895,121 @@ export function takeFirstAppCopy(cwd, slug) {
1695
1895
  FIRST_COPIES.delete(key);
1696
1896
  return wrote;
1697
1897
  }
1898
+
1899
+ /**
1900
+ * Run a browser for a picture without blocking this process: a stop that
1901
+ * reaches it (SIGINT, SIGTERM) stops the browser too, and is reported.
1902
+ * Bounded to 60 seconds, as a picture run always was.
1903
+ * @param {string} bin
1904
+ * @param {string[]} args
1905
+ * @param {number} [bound] milliseconds before the browser is stopped
1906
+ * @returns {Promise<{status: number | null, signal: string | null, stderr: string, error?: Error, stoppedBy?: string, timedOut?: boolean}>}
1907
+ */
1908
+ export function runBrowser(bin, args, bound = 60_000) {
1909
+ return new Promise(resolve => {
1910
+ /** @type {string | undefined} */
1911
+ let stoppedBy;
1912
+ let stderr = '';
1913
+ // A profile folder of its own, removed on every way out (done, failed,
1914
+ // timed out, stopped): Chrome leaves its org.chromium.* folders in the
1915
+ // system temp folder otherwise.
1916
+ /** @type {string | null} */
1917
+ let profile = null;
1918
+ try {
1919
+ profile = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-chrome-'));
1920
+ } catch {
1921
+ // No temp folder: Chrome's own default.
1922
+ }
1923
+ const dropProfile = () => {
1924
+ if (profile)
1925
+ try {
1926
+ fs.rmSync(profile, {recursive: true, force: true});
1927
+ } catch {
1928
+ // Gone already.
1929
+ }
1930
+ };
1931
+ process.once('exit', dropProfile);
1932
+ const child = spawn(
1933
+ bin,
1934
+ [...(profile ? [`--user-data-dir=${profile}`] : []), ...args],
1935
+ {
1936
+ stdio: ['ignore', 'ignore', 'pipe'],
1937
+ env: profile ? {...process.env, TMPDIR: profile} : process.env,
1938
+ // A group of its own, so a stop takes Chrome's helper processes
1939
+ // with it, before its profile folder is removed.
1940
+ detached: process.platform !== 'win32',
1941
+ },
1942
+ );
1943
+ const killAll = () => {
1944
+ try {
1945
+ if (process.platform !== 'win32' && child.pid)
1946
+ process.kill(-child.pid, 'SIGKILL');
1947
+ else child.kill('SIGKILL');
1948
+ } catch {
1949
+ child.kill('SIGKILL');
1950
+ }
1951
+ };
1952
+ child.stderr?.setEncoding('utf8');
1953
+ child.stderr?.on('data', d => {
1954
+ if (stderr.length < 8000) stderr += d;
1955
+ });
1956
+ let timedOut = false;
1957
+ const timer = setTimeout(() => {
1958
+ timedOut = true;
1959
+ killAll();
1960
+ }, bound);
1961
+ /** @param {NodeJS.Signals} sig */
1962
+ const onStop = sig => {
1963
+ stoppedBy = sig;
1964
+ killAll();
1965
+ dropProfile();
1966
+ // Nothing else listening: the stop ends this process, as it would
1967
+ // have without this listener.
1968
+ if (process.listenerCount(sig) === 0) {
1969
+ process.removeListener('SIGINT', onStop);
1970
+ process.removeListener('SIGTERM', onStop);
1971
+ process.kill(process.pid, sig);
1972
+ }
1973
+ };
1974
+ process.once('SIGINT', onStop);
1975
+ process.once('SIGTERM', onStop);
1976
+ const done = (/** @type {any} */ out) => {
1977
+ clearTimeout(timer);
1978
+ dropProfile();
1979
+ process.removeListener('exit', dropProfile);
1980
+ process.removeListener('SIGINT', onStop);
1981
+ process.removeListener('SIGTERM', onStop);
1982
+ resolve({
1983
+ ...out,
1984
+ stderr,
1985
+ ...(stoppedBy ? {stoppedBy} : {}),
1986
+ ...(timedOut ? {timedOut} : {}),
1987
+ });
1988
+ };
1989
+ child.on('error', error => done({status: null, signal: null, error}));
1990
+ child.on('close', (status, signal) => done({status, signal}));
1991
+ });
1992
+ }
1993
+
1994
+ /**
1995
+ * The first stderr line that says something about the picture: Chrome's
1996
+ * startup warnings (dbus, GPU, sandbox notices) come first on Linux and
1997
+ * say nothing about why no picture was written.
1998
+ * @param {string} text
1999
+ */
2000
+ export function meaningfulLine(text) {
2001
+ const lines = String(text ?? '')
2002
+ .split(/\r?\n/)
2003
+ .map(l => l.trim())
2004
+ .filter(Boolean);
2005
+ return (
2006
+ lines.find(
2007
+ l =>
2008
+ !/dbus|bus\.cc|gpu_|gl_|sandbox|fontconfig|crashpad|DevTools listening|WARNING:/i.test(
2009
+ l,
2010
+ ),
2011
+ ) ??
2012
+ lines[0] ??
2013
+ ''
2014
+ );
2015
+ }