@astryxdesign/cli 0.7.16 → 0.7.17-canary.gb36275e

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 (239) hide show
  1. package/README.md +13 -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 +59 -12
  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 +42 -0
  14. package/api/build/kit/words.mjs +82 -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 +46 -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 +531 -192
  34. package/api/render/render.doc.mjs +11 -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 +47 -0
  46. package/api/theme/build/build.mjs +147 -25
  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 +48 -0
  50. package/api/theme-edit/_adapter.mjs +675 -42
  51. package/api/theme-edit/_check.d.mts +28 -22
  52. package/api/theme-edit/_check.mjs +1481 -252
  53. package/api/theme-edit/_color.d.mts +9 -0
  54. package/api/theme-edit/_color.mjs +41 -1
  55. package/api/theme-edit/_compiler.mjs +9 -0
  56. package/api/theme-edit/_context.mjs +60 -3
  57. package/api/theme-edit/_css.mjs +21 -1
  58. package/api/theme-edit/_edit.d.mts +22 -0
  59. package/api/theme-edit/_edit.mjs +1196 -88
  60. package/api/theme-edit/_how-commands.d.mts +57 -0
  61. package/api/theme-edit/_how-commands.mjs +536 -1
  62. package/api/theme-edit/_how-question.d.mts +65 -0
  63. package/api/theme-edit/_how-question.mjs +4401 -225
  64. package/api/theme-edit/_how.mjs +104 -4
  65. package/api/theme-edit/_marks.d.mts +31 -5
  66. package/api/theme-edit/_marks.mjs +57 -5
  67. package/api/theme-edit/_normal.d.mts +153 -0
  68. package/api/theme-edit/_normal.mjs +661 -0
  69. package/api/theme-edit/_palette.mjs +10 -4
  70. package/api/theme-edit/_parts.d.mts +8 -0
  71. package/api/theme-edit/_parts.mjs +103 -2
  72. package/api/theme-edit/_preview-app.d.mts +7 -1
  73. package/api/theme-edit/_preview-app.mjs +205 -17
  74. package/api/theme-edit/_preview.d.mts +64 -8
  75. package/api/theme-edit/_preview.mjs +447 -89
  76. package/api/theme-edit/_settings.d.mts +176 -5
  77. package/api/theme-edit/_settings.mjs +1936 -128
  78. package/api/theme-edit/_tables.d.mts +6 -0
  79. package/api/theme-edit/_tables.mjs +32 -1
  80. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  81. package/api/theme-edit/theme-edit.type.mjs +4 -1
  82. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  83. package/assets/codemods/run-codemod.mjs +73 -6
  84. package/assets/codemods/transforms/next/index.mjs +11 -1
  85. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  86. package/assets/docs/author-a-theme.doc.mjs +1 -0
  87. package/assets/docs/theme.doc.dense.mjs +2 -2
  88. package/assets/docs/theme.doc.mjs +1 -1
  89. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  90. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  91. package/assets/docs/tree/render-pages.doc.mjs +43 -2
  92. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  93. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  94. package/assets/docs/use-a-theme.doc.mjs +1 -1
  95. package/assets/docs.generated.json +2 -2
  96. package/assets/self-audit.generated.json +1 -1
  97. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  98. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  99. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  100. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  101. package/assets/templates/components.generated.json +1 -1
  102. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  103. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  104. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  105. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  106. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  107. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  108. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  109. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  110. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  111. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  112. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  113. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  114. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  115. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  116. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  117. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  118. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  119. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  120. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  121. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  122. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  123. package/assets/templates/pages/ide/page.tsx +24 -2
  124. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  125. package/assets/templates/pages/library/template.doc.mjs +4 -1
  126. package/assets/templates/pages/login/template.doc.mjs +2 -1
  127. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  128. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  129. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  130. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  131. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  132. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  133. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  134. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  135. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  136. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  137. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  138. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  139. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  140. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  141. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  142. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  143. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  144. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  145. package/authoring/debug/parse.d.mts +3 -3
  146. package/authoring/doctypes/_schema.d.mts +3 -3
  147. package/authoring/doctypes/command/command.doc.mjs +1 -1
  148. package/authoring/doctypes/template/template.doc.mjs +1 -1
  149. package/authoring/doctypes/template/type.ts +9 -0
  150. package/clients/cli/bin/astryx.mjs +6 -9
  151. package/clients/cli/build-id.generated.json +1 -1
  152. package/clients/cli/commands/component.doc.mjs +1 -1
  153. package/clients/cli/commands/component.mjs +20 -2
  154. package/clients/cli/commands/docs.doc.mjs +7 -1
  155. package/clients/cli/commands/docs.mjs +3 -1
  156. package/clients/cli/commands/doctor.mjs +22 -14
  157. package/clients/cli/commands/experimental/render.mjs +32 -1
  158. package/clients/cli/commands/experimental/theme.mjs +65 -14
  159. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  160. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  161. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  162. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  163. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  164. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  165. package/clients/cli/commands/search.doc.mjs +2 -1
  166. package/clients/cli/commands/search.mjs +6 -1
  167. package/clients/cli/commands/template.mjs +4 -2
  168. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  169. package/clients/cli/commands/theme.doc.mjs +1 -1
  170. package/clients/cli/commands/theme.mjs +153 -5
  171. package/clients/cli/commands/upgrade.mjs +42 -7
  172. package/clients/cli/index.mjs +1 -1
  173. package/clients/cli/lib/component-format.mjs +205 -98
  174. package/clients/cli/lib/hook-format.mjs +9 -19
  175. package/clients/cli/manifest.generated.json +1 -1
  176. package/foundation/agent-docs/agent-docs.mjs +1 -1
  177. package/foundation/config/theme-state.d.mts +13 -1
  178. package/foundation/config/theme-state.mjs +47 -7
  179. package/foundation/discovery/component-discovery.d.mts +11 -0
  180. package/foundation/discovery/component-discovery.mjs +27 -2
  181. package/foundation/discovery/component-replacement.mjs +130 -1
  182. package/foundation/discovery/entry-name-files.d.mts +43 -0
  183. package/foundation/discovery/entry-name-files.mjs +264 -0
  184. package/foundation/discovery/hook-discovery.d.mts +8 -2
  185. package/foundation/discovery/hook-discovery.mjs +20 -7
  186. package/foundation/discovery/package-exports.d.mts +10 -0
  187. package/foundation/discovery/package-exports.mjs +56 -3
  188. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  189. package/foundation/discovery/runner-allowlist.mjs +2 -0
  190. package/foundation/discovery/template-adapter.d.mts +52 -0
  191. package/foundation/discovery/template-adapter.mjs +173 -10
  192. package/foundation/discovery/theme-imports.d.mts +14 -0
  193. package/foundation/discovery/theme-imports.mjs +43 -1
  194. package/foundation/doc-compiler/import.mjs +7 -1
  195. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  196. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  197. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  198. package/foundation/fs/cache-eviction.d.mts +21 -0
  199. package/foundation/fs/cache-eviction.mjs +73 -0
  200. package/foundation/fs/gather-cache.d.mts +25 -4
  201. package/foundation/fs/gather-cache.mjs +163 -53
  202. package/foundation/fs/jiti-cache.d.mts +19 -1
  203. package/foundation/fs/jiti-cache.mjs +130 -6
  204. package/foundation/fs/kept-tree.d.mts +60 -0
  205. package/foundation/fs/kept-tree.mjs +154 -0
  206. package/foundation/fs/module-loader.d.mts +18 -2
  207. package/foundation/fs/module-loader.mjs +92 -20
  208. package/foundation/identity/provider-identity.d.mts +6 -0
  209. package/foundation/identity/provider-identity.mjs +10 -1
  210. package/foundation/render/embedded-imports.d.mts +5 -4
  211. package/foundation/render/embedded-imports.mjs +7 -16
  212. package/foundation/render/file-header.d.mts +66 -0
  213. package/foundation/render/file-header.mjs +118 -0
  214. package/foundation/render/host-answer.d.mts +18 -0
  215. package/foundation/render/host-answer.mjs +46 -0
  216. package/foundation/render/host-exit.d.mts +1 -0
  217. package/foundation/render/host-exit.mjs +6 -1
  218. package/foundation/render/html-reader.d.mts +55 -2
  219. package/foundation/render/html-reader.mjs +145 -26
  220. package/foundation/render/markup-edits.d.mts +57 -0
  221. package/foundation/render/markup-edits.mjs +182 -0
  222. package/foundation/render/page-host.mjs +29 -12
  223. package/foundation/render/page-worker.mjs +83 -13
  224. package/foundation/render/render-page.d.mts +34 -2
  225. package/foundation/render/render-page.mjs +210 -29
  226. package/foundation/render/source-block.d.mts +73 -6
  227. package/foundation/render/source-block.mjs +170 -19
  228. package/foundation/render/ts-paths.d.mts +53 -6
  229. package/foundation/render/ts-paths.mjs +286 -41
  230. package/foundation/response/error-codes.d.mts +3 -1
  231. package/foundation/response/error-codes.d.ts +1 -0
  232. package/foundation/response/error-codes.doc.mjs +12 -1
  233. package/foundation/response/error-codes.mjs +10 -0
  234. package/foundation/text/string-utils.d.mts +3 -2
  235. package/foundation/text/string-utils.mjs +16 -3
  236. package/foundation/text/suggest-order.d.mts +9 -0
  237. package/foundation/text/suggest-order.mjs +13 -0
  238. package/host/index.mjs +8 -6
  239. package/package.json +6 -6
@@ -24,6 +24,7 @@
24
24
 
25
25
  import * as fs from 'node:fs';
26
26
  import * as path from 'node:path';
27
+ import {fileURLToPath} from 'node:url';
27
28
  import {readDocView} from '../doc-compiler/read.mjs';
28
29
  import {importDocModule} from '../doc-compiler/import.mjs';
29
30
  import {readStore, storedKey} from '../doc-compiler/stored-docs.mjs';
@@ -40,8 +41,9 @@ import {
40
41
  TEMPLATE_COMPONENTS_FILE,
41
42
  } from './template-source-tree.mjs';
42
43
  import {persistedGather} from '../fs/gather-cache.mjs';
44
+ import {hasNameLine, keptTreeSummary, nameLines} from '../fs/kept-tree.mjs';
45
+ import {foldEntryName} from './entry-name-files.mjs';
43
46
  import {EXACT_CODEC} from '../fs/exact-codec.mjs';
44
- import {hostedRun} from '../runtime/package-root.mjs';
45
47
  import {babelParser} from '../runtime/lazy-dependencies.mjs';
46
48
 
47
49
  /** Identity used for core (built-in) templates in package-scoped listings. */
@@ -1251,6 +1253,99 @@ async function discoverBlocks() {
1251
1253
  return blocks;
1252
1254
  }
1253
1255
 
1256
+ /**
1257
+ * The ids of Core's templates that `keep` accepts, read from file names: each
1258
+ * page folder, and each block doc that has its same-stem source. Discovery
1259
+ * gives Core's templates exactly these ids. Only a kept block's source is
1260
+ * looked for.
1261
+ * @param {(id: string) => boolean} [keep]
1262
+ * @returns {string[]}
1263
+ */
1264
+ export function coreTemplateIds(keep = () => true) {
1265
+ const ids = listTemplates().filter(keep);
1266
+ /** @param {string} dir */
1267
+ const walk = dir => {
1268
+ let entries;
1269
+ try {
1270
+ entries = fs.readdirSync(dir, {withFileTypes: true});
1271
+ } catch {
1272
+ return;
1273
+ }
1274
+ for (const entry of entries) {
1275
+ const full = path.join(dir, entry.name);
1276
+ if (entry.isDirectory()) {
1277
+ walk(full);
1278
+ continue;
1279
+ }
1280
+ if (!TEMPLATE_SUFFIX_RE.test(entry.name)) continue;
1281
+ const suffix = matchedTemplateSuffix(entry.name);
1282
+ if (!suffix) continue;
1283
+ const basename = entry.name.slice(0, -suffix.length);
1284
+ if (keep(basename) && fs.existsSync(path.join(dir, basename + '.tsx'))) {
1285
+ ids.push(basename);
1286
+ }
1287
+ }
1288
+ };
1289
+ walk(BLOCKS_DIR);
1290
+ return ids;
1291
+ }
1292
+
1293
+ /** This module's own file: its code decides the kept summaries' shape. */
1294
+ const OWN_FILE = fileURLToPath(import.meta.url);
1295
+
1296
+ /** The file of foldEntryName, which decides the template-ids summary too. */
1297
+ const FOLD_FILE = fileURLToPath(
1298
+ new URL('./entry-name-files.mjs', import.meta.url),
1299
+ );
1300
+
1301
+ /**
1302
+ * Which template files can give an id that folds to `wanted`, read from a
1303
+ * kept summary of file names: `blocks` false means {@link coreTemplateIds}
1304
+ * gives no block id that folds to it, and `listed` says whether any of the
1305
+ * integrations ships a template doc whose id ({@link integrationTemplateIds})
1306
+ * folds to it.
1307
+ * @param {Array<{templates?: string}>} integrations
1308
+ * @param {string} wanted a folded name
1309
+ * @returns {{blocks: boolean, listed: boolean}}
1310
+ */
1311
+ export function templateFilesNaming(integrations, wanted) {
1312
+ const roots = integrations.flatMap(integration =>
1313
+ typeof integration?.templates === 'string' && integration.templates !== ''
1314
+ ? [integration.templates]
1315
+ : [],
1316
+ );
1317
+ /** @type {Array<string | string[]>} */
1318
+ const [blocks, ...listed] = keptTreeSummary(
1319
+ {
1320
+ kind: 'template-ids',
1321
+ roots: [BLOCKS_DIR, ...roots],
1322
+ code: [OWN_FILE, FOLD_FILE],
1323
+ },
1324
+ trees =>
1325
+ trees.map(({folders}, at) => {
1326
+ const folded = new Set();
1327
+ for (const {rel, entries} of folders) {
1328
+ for (const {name, isDir} of entries) {
1329
+ if (isDir) continue;
1330
+ const suffix = matchedTemplateSuffix(name);
1331
+ if (!suffix) continue;
1332
+ // Core's block ids are stems; an integration's are paths.
1333
+ const id =
1334
+ at === 0
1335
+ ? name.slice(0, -suffix.length)
1336
+ : path.join(rel, name).slice(0, -suffix.length);
1337
+ folded.add(foldEntryName(id.split(path.sep).join('/')));
1338
+ }
1339
+ }
1340
+ return nameLines(folded);
1341
+ }),
1342
+ );
1343
+ return {
1344
+ blocks: hasNameLine(blocks, wanted),
1345
+ listed: listed.some(ids => hasNameLine(ids, wanted)),
1346
+ };
1347
+ }
1348
+
1254
1349
  /**
1255
1350
  * Discover blocks from external packages that declare `astryx.blocks` in
1256
1351
  * their package.json. Same shape as discoverBlocks() output.
@@ -1580,15 +1675,10 @@ async function resolveAllSources(cwd = process.cwd()) {
1580
1675
  // a later run with every one unchanged reads the records back.
1581
1676
  return /** @type {Awaited<ReturnType<typeof resolveAllSourcesNow>>} */ (
1582
1677
  await persistedGather(
1583
- // The folder, and the copy of the CLI that answers with the
1584
- // integrations a host approves: outside a project the integrations
1585
- // beside the answering copy load too.
1586
- [
1587
- 'template-sources',
1588
- realDir(cwd),
1589
- CLI_ROOT,
1590
- hostedRun()?.integrations ?? [],
1591
- ],
1678
+ // The folder, and the copy of the CLI that answers: outside a project
1679
+ // the integrations beside the answering copy load too. A host's inputs
1680
+ // are in every entry's key (gather-cache.mjs).
1681
+ ['template-sources', realDir(cwd), CLI_ROOT],
1592
1682
  () => resolveAllSourcesNow(cwd),
1593
1683
  TEMPLATE_SOURCES_CODEC,
1594
1684
  )
@@ -1681,6 +1771,79 @@ function findIntegrationDocFiles(root) {
1681
1771
  return results;
1682
1772
  }
1683
1773
 
1774
+ /**
1775
+ * Code that lets a template doc declare a replacement: the field itself, or an
1776
+ * import, a require or a spread that can bring it in. Comments do not count.
1777
+ */
1778
+ const MAY_REPLACE_RE =
1779
+ /\breplaces\b|^\s*import\b|\bimport\s*\(|\brequire\s*\(|\.\.\.\s*[A-Za-z_$]/m;
1780
+
1781
+ /**
1782
+ * The id of each template doc an integration ships, valid or not, read from
1783
+ * file names alone.
1784
+ * @param {{templates?: string}} integration
1785
+ * @returns {string[]}
1786
+ */
1787
+ export function integrationTemplateIds(integration) {
1788
+ const root = integration?.templates;
1789
+ if (!root) return [];
1790
+ return findIntegrationDocFiles(root).flatMap(docPath => {
1791
+ const suffix = matchedTemplateSuffix(docPath);
1792
+ if (!suffix) return [];
1793
+ return [
1794
+ path
1795
+ .relative(root, docPath)
1796
+ .slice(0, -suffix.length)
1797
+ .split(path.sep)
1798
+ .join('/'),
1799
+ ];
1800
+ });
1801
+ }
1802
+
1803
+ /**
1804
+ * Whether any template doc an integration ships can declare a replacement,
1805
+ * read from its text without loading it.
1806
+ * @param {{templates?: string}} integration
1807
+ * @returns {boolean}
1808
+ */
1809
+ export function integrationMayReplaceTemplate(integration) {
1810
+ return integrationsMayReplaceTemplate([integration]);
1811
+ }
1812
+
1813
+ /**
1814
+ * Whether any template doc any of the integrations ships can declare a
1815
+ * replacement, read from a kept summary of the docs' text.
1816
+ * @param {Array<{templates?: string}>} integrations
1817
+ * @returns {boolean}
1818
+ */
1819
+ export function integrationsMayReplaceTemplate(integrations) {
1820
+ const roots = integrations.flatMap(integration =>
1821
+ typeof integration?.templates === 'string' && integration.templates !== ''
1822
+ ? [integration.templates]
1823
+ : [],
1824
+ );
1825
+ if (roots.length === 0) return false;
1826
+ return keptTreeSummary(
1827
+ {kind: 'integration-template-replaces', roots, code: [OWN_FILE]},
1828
+ trees =>
1829
+ trees.some(({folders, readText}) =>
1830
+ folders.some(({rel, entries}) =>
1831
+ entries.some(({name, isDir}) => {
1832
+ if (isDir || !matchedTemplateSuffix(name)) return false;
1833
+ try {
1834
+ const code = readText(rel === '' ? name : path.join(rel, name))
1835
+ .replace(/\/\*[\s\S]*?\*\//g, '')
1836
+ .replace(/^\s*\/\/.*$/gm, '');
1837
+ return MAY_REPLACE_RE.test(code);
1838
+ } catch {
1839
+ return true;
1840
+ }
1841
+ }),
1842
+ ),
1843
+ ),
1844
+ );
1845
+ }
1846
+
1684
1847
  /**
1685
1848
  * Discover templates contributed by configured integrations.
1686
1849
  *
@@ -1,6 +1,20 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
+ /**
5
+ * `{mode}` for a built theme module that exports one color mode
6
+ * (`export const colorModes = ["light"];`, which `theme build` writes beside
7
+ * the theme export), read without running it; nothing for a module with
8
+ * both modes, which exports none.
9
+ * @param {string | null | undefined} moduleFile
10
+ * @param {string} [exportName] the theme's export: a module that re-exports
11
+ * only the theme by name (`export {oceanTheme} from './ocean.js'`) is
12
+ * read where the theme is declared
13
+ * @returns {{mode?: 'light' | 'dark'}}
14
+ */
15
+ export function builtColorMode(moduleFile: string | null | undefined, exportName?: string): {
16
+ mode?: "light" | "dark";
17
+ };
4
18
  /**
5
19
  * Resolve a discovered theme to app imports.
6
20
  * @param {import('./theme-discovery.mjs').DiscoveredTheme} theme
@@ -15,7 +15,7 @@
15
15
  import * as fs from 'node:fs';
16
16
  import * as path from 'node:path';
17
17
  import {findInstalledPackage} from '../fs/paths.mjs';
18
- import {moduleExportsName} from './module-exports.mjs';
18
+ import {moduleExportSource, moduleExportsName} from './module-exports.mjs';
19
19
  import {inspectPackageExport} from './package-exports.mjs';
20
20
  import {themeImportPackage} from './theme-discovery.mjs';
21
21
  import {isLocalThemeOwner, projectPath} from '../config/theme-state.mjs';
@@ -101,6 +101,7 @@ function resolveLocalTheme(theme, cwd) {
101
101
  stylesheetFile,
102
102
  ...(fs.existsSync(fontStylesheetFile) ? {fontStylesheetFile} : {}),
103
103
  source: /** @type {const} */ ('local'),
104
+ ...builtColorMode(moduleFile, theme.exportName),
104
105
  };
105
106
  }
106
107
 
@@ -228,9 +229,50 @@ function resolvePackageTheme(theme, cwd, ownerThemeCount) {
228
229
  /** @type {any} */ (theme).source === 'bundled' || theme.bundled
229
230
  ? /** @type {const} */ ('bundled')
230
231
  : /** @type {const} */ ('package'),
232
+ ...builtColorMode(selected.module.target, theme.exportName),
231
233
  };
232
234
  }
233
235
 
236
+ /**
237
+ * `{mode}` for a built theme module that exports one color mode
238
+ * (`export const colorModes = ["light"];`, which `theme build` writes beside
239
+ * the theme export), read without running it; nothing for a module with
240
+ * both modes, which exports none.
241
+ * @param {string | null | undefined} moduleFile
242
+ * @param {string} [exportName] the theme's export: a module that re-exports
243
+ * only the theme by name (`export {oceanTheme} from './ocean.js'`) is
244
+ * read where the theme is declared
245
+ * @returns {{mode?: 'light' | 'dark'}}
246
+ */
247
+ export function builtColorMode(moduleFile, exportName) {
248
+ if (!moduleFile) return {};
249
+ // The file that declares it: the module, or the built module it re-exports
250
+ // (`export * from './ocean.js'`); else the built module that declares the
251
+ // theme the module re-exports by name.
252
+ const declaring =
253
+ moduleExportSource(moduleFile, 'colorModes') ??
254
+ (exportName
255
+ ? (() => {
256
+ const theme = moduleExportSource(moduleFile, exportName);
257
+ return theme && theme !== moduleFile
258
+ ? moduleExportSource(theme, 'colorModes')
259
+ : null;
260
+ })()
261
+ : null);
262
+ if (!declaring) return {};
263
+ let text;
264
+ try {
265
+ text = fs.readFileSync(declaring, 'utf8');
266
+ } catch {
267
+ return {};
268
+ }
269
+ const m =
270
+ /^export const colorModes = \[\s*["'](light|dark)["']\s*\];?\s*$/m.exec(
271
+ text,
272
+ );
273
+ return m ? {mode: /** @type {'light' | 'dark'} */ (m[1])} : {};
274
+ }
275
+
234
276
  /**
235
277
  * Resolve a discovered theme to app imports.
236
278
  * @param {import('./theme-discovery.mjs').DiscoveredTheme} theme
@@ -35,7 +35,12 @@ import {
35
35
  readInTurn,
36
36
  } from '../fs/module-loader.mjs';
37
37
  import {fileStamp, recordContentRead} from '../fs/read-record.mjs';
38
- import {readStore, storedKey, STORED_DOCS_FILE} from './stored-docs.mjs';
38
+ import {
39
+ noteStoredExport,
40
+ readStore,
41
+ storedKey,
42
+ STORED_DOCS_FILE,
43
+ } from './stored-docs.mjs';
39
44
 
40
45
  const requireFromHere = createRequire(import.meta.url);
41
46
 
@@ -132,6 +137,7 @@ function storedModule(file) {
132
137
  enumerable: true,
133
138
  writable: true,
134
139
  });
140
+ noteStoredExport(exports[name], key, name);
135
141
  }
136
142
  Object.defineProperty(module, Symbol.toStringTag, {value: 'Module'});
137
143
  Object.preventExtensions(module);
@@ -11,6 +11,7 @@
11
11
  */
12
12
 
13
13
  import {parseDoc} from '../../authoring/doctypes/parse.mjs';
14
+ import {storedParse} from './stored-docs.mjs';
14
15
 
15
16
  /**
16
17
  * @param {unknown} input
@@ -25,5 +26,7 @@ export function parseReadableDoc(input, label = 'doc') {
25
26
  if (type === 'theme') {
26
27
  throw new Error(`${label} has unsupported type ${JSON.stringify(type)}.`);
27
28
  }
28
- return parseDoc(input, label);
29
+ // A doc Astryx ships was parsed at publish (scripts/generate-stored-docs.mjs):
30
+ // its stored parse is what parsing it gives.
31
+ return storedParse(input) ?? parseDoc(input, label);
29
32
  }
@@ -21,6 +21,28 @@ export function storedKey(text: string | Buffer): string;
21
21
  export function readStore(file: string): {
22
22
  get: (key: string) => any;
23
23
  };
24
+ /**
25
+ * Note that `value` is the `name` export of the stored doc `key`.
26
+ * @param {unknown} value
27
+ * @param {string} key
28
+ * @param {string} name
29
+ */
30
+ export function noteStoredExport(value: unknown, key: string, name: string): void;
31
+ /**
32
+ * Where a stored doc's export, as the readers parse it, is stored at publish.
33
+ * @param {string} key the doc's key
34
+ * @param {string} name the export's name
35
+ * @returns {string}
36
+ */
37
+ export function storedParseKey(key: string, name: string): string;
38
+ /**
39
+ * The parse of `value`, read from the store, when `value` is a stored doc's
40
+ * export whose parse was stored at publish: a fresh value, as a parse gives.
41
+ * Undefined otherwise, or when the store cannot be read.
42
+ * @param {unknown} value
43
+ * @returns {any}
44
+ */
45
+ export function storedParse(value: unknown): any;
24
46
  /**
25
47
  * Where the exports of the doc files Astryx ships (the CLI's and Core's) are
26
48
  * stored: each doc that imports nothing, reads nothing of where it sits, and
@@ -96,3 +96,52 @@ function openStore(file) {
96
96
  return new Map();
97
97
  }
98
98
  }
99
+
100
+ /**
101
+ * Each export value of a stored doc's module (./import.mjs), with the doc's
102
+ * key and the export's name, so its parse can be answered from the store
103
+ * ({@link storedParse}).
104
+ * @type {WeakMap<object, {key: string, name: string}>}
105
+ */
106
+ const storedExports = new WeakMap();
107
+
108
+ /**
109
+ * Note that `value` is the `name` export of the stored doc `key`.
110
+ * @param {unknown} value
111
+ * @param {string} key
112
+ * @param {string} name
113
+ */
114
+ export function noteStoredExport(value, key, name) {
115
+ if (value && typeof value === 'object') storedExports.set(value, {key, name});
116
+ }
117
+
118
+ /**
119
+ * Where a stored doc's export, as the readers parse it, is stored at publish.
120
+ * @param {string} key the doc's key
121
+ * @param {string} name the export's name
122
+ * @returns {string}
123
+ */
124
+ export function storedParseKey(key, name) {
125
+ return `parsed:${name}:${key}`;
126
+ }
127
+
128
+ /**
129
+ * The parse of `value`, read from the store, when `value` is a stored doc's
130
+ * export whose parse was stored at publish: a fresh value, as a parse gives.
131
+ * Undefined otherwise, or when the store cannot be read.
132
+ * @param {unknown} value
133
+ * @returns {any}
134
+ */
135
+ export function storedParse(value) {
136
+ if (!value || typeof value !== 'object') return undefined;
137
+ const at = storedExports.get(value);
138
+ if (!at) return undefined;
139
+ try {
140
+ const parsed = readStore(STORED_DOCS_FILE).get(
141
+ storedParseKey(at.key, at.name),
142
+ );
143
+ return parsed != null && typeof parsed === 'object' ? parsed : undefined;
144
+ } catch {
145
+ return undefined;
146
+ }
147
+ }
@@ -0,0 +1,21 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * Delete the folder's counted files, least recently used first, until they
6
+ * hold at most `limit` bytes. A file used at `keepUsedSince` or later is kept,
7
+ * even while the folder holds more. Only regular files directly in the
8
+ * folder count: never a link, a folder, or anything inside one. A file
9
+ * another run removes first is left to that run.
10
+ * @param {string} dir
11
+ * @param {(name: string) => boolean} counts - whether a file name counts
12
+ * @param {{limit?: number, keepUsedSince?: number, lastUse?: (stat: fs.Stats) => number}} [options]
13
+ */
14
+ export function evictOldest(dir: string, counts: (name: string) => boolean, { limit, keepUsedSince, lastUse, }?: {
15
+ limit?: number;
16
+ keepUsedSince?: number;
17
+ lastUse?: (stat: fs.Stats) => number;
18
+ }): void;
19
+ /** The most a cache folder holds; the oldest files go first past it. */
20
+ export const CACHE_LIMIT_BYTES: number;
21
+ import * as fs from 'node:fs';
@@ -0,0 +1,73 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Keeps a per-user cache folder within its size limit: the oldest files
5
+ * go first.
6
+ * @input A cache folder, and which of its files count.
7
+ * @output `CACHE_LIMIT_BYTES`, the most a cache folder holds, and
8
+ * `evictOldest(dir, counts)`, which deletes the folder's oldest counted
9
+ * files until it holds at most that.
10
+ * @position Used by gather-cache.mjs for the kept catalogs and by
11
+ * jiti-cache.mjs for jiti's transpiled code. Each folder has its own limit
12
+ * of this size.
13
+ */
14
+
15
+ import * as fs from 'node:fs';
16
+ import * as path from 'node:path';
17
+
18
+ /** The most a cache folder holds; the oldest files go first past it. */
19
+ export const CACHE_LIMIT_BYTES = 64 * 1024 * 1024;
20
+
21
+ /**
22
+ * When a file was last used, by default its last write (a kept catalog is
23
+ * written again when it is read back).
24
+ * @param {fs.Stats} stat
25
+ * @returns {number}
26
+ */
27
+ const lastWrite = stat => stat.mtimeMs;
28
+
29
+ /**
30
+ * Delete the folder's counted files, least recently used first, until they
31
+ * hold at most `limit` bytes. A file used at `keepUsedSince` or later is kept,
32
+ * even while the folder holds more. Only regular files directly in the
33
+ * folder count: never a link, a folder, or anything inside one. A file
34
+ * another run removes first is left to that run.
35
+ * @param {string} dir
36
+ * @param {(name: string) => boolean} counts - whether a file name counts
37
+ * @param {{limit?: number, keepUsedSince?: number, lastUse?: (stat: fs.Stats) => number}} [options]
38
+ */
39
+ export function evictOldest(
40
+ dir,
41
+ counts,
42
+ {
43
+ limit = CACHE_LIMIT_BYTES,
44
+ keepUsedSince = Infinity,
45
+ lastUse = lastWrite,
46
+ } = {},
47
+ ) {
48
+ const entries = fs
49
+ .readdirSync(dir)
50
+ .filter(counts)
51
+ .map(name => {
52
+ const entry = path.join(dir, name);
53
+ try {
54
+ const stat = fs.lstatSync(entry);
55
+ if (!stat.isFile()) return null;
56
+ return {entry, size: stat.size, used: lastUse(stat)};
57
+ } catch {
58
+ return null;
59
+ }
60
+ })
61
+ .filter(e => e != null)
62
+ .sort((a, b) => a.used - b.used);
63
+ let total = entries.reduce((sum, e) => sum + e.size, 0);
64
+ for (const {entry, size, used} of entries) {
65
+ if (total <= limit || used >= keepUsedSince) break;
66
+ try {
67
+ fs.unlinkSync(entry);
68
+ total -= size;
69
+ } catch {
70
+ // Another run removed it.
71
+ }
72
+ }
73
+ }
@@ -92,6 +92,26 @@ export function persistedDerived<T, E>(key: unknown[], compute: () => T, codec:
92
92
  decode: (stored: E) => T;
93
93
  valid?: (value: T) => boolean;
94
94
  }, inputs?: string[]): T;
95
+ /**
96
+ * A summary of files and folders, made at once, kept as an entry beside the
97
+ * gathers, in or out of a recorded run: the stored value while every path
98
+ * `make` stamped has the stamp it was stored with, else the one `make` gives
99
+ * now, which is stored unless a stamp is younger than
100
+ * {@link SUMMARY_SETTLE_MS}. The folder, the build id, the Yarn Plug'n'Play
101
+ * rule, the 0600 rename write and the eviction are those of a kept gather;
102
+ * anywhere a gather is not kept, or on any read or write error, `make` runs
103
+ * as before.
104
+ * @template T
105
+ * @param {unknown[]} key - Everything else the summary depends on.
106
+ * @param {() => {value: T, stamps: [string, string][]}} make - The summary,
107
+ * with the stamp ({@link fileStamp}) of each path it depends on, each taken
108
+ * before the path was read.
109
+ * @returns {T}
110
+ */
111
+ export function persistedSummary<T>(key: unknown[], make: () => {
112
+ value: T;
113
+ stamps: [string, string][];
114
+ }): T;
95
115
  /**
96
116
  * Whether this run is under Yarn Plug'n'Play's loader (`yarn astryx` in a PnP
97
117
  * project): Node's module hooks hand PnP's resolver options it refuses, so
@@ -103,16 +123,17 @@ export function runsUnderPnp(): boolean;
103
123
  /**
104
124
  * The cache folder and how much it holds, for `doctor`: null when the CLI
105
125
  * keeps no gathers (a checkout) or has no usable folder.
106
- * @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null}
107
- * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp)
126
+ * @returns {{dir: string, bytes: number, entries: number, pnp: boolean, limitBytes: number, jitiDir: string} | null}
127
+ * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp);
128
+ * `limitBytes`: the most the folder, and jiti's folder `jitiDir`, holds
108
129
  */
109
130
  export function gatherCacheSummary(): {
110
131
  dir: string;
111
132
  bytes: number;
112
133
  entries: number;
113
134
  pnp: boolean;
135
+ limitBytes: number;
136
+ jitiDir: string;
114
137
  } | null;
115
138
  /** Where `prepack` writes the id of the package's exact contents. */
116
139
  export const BUILD_ID_FILE: string;
117
- /** The most the folder holds; the oldest entries go first past it. */
118
- export const CACHE_LIMIT_BYTES: number;