@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
@@ -73,6 +73,7 @@ import {
73
73
  } from '../../foundation/env/package-manager.mjs';
74
74
  import {sha256} from '../../foundation/render/source-block.mjs';
75
75
  import {findNodeModulesPackage} from '../../foundation/fs/paths.mjs';
76
+ import {compareText} from '../../foundation/text/compare-text.mjs';
76
77
 
77
78
  const E = ERROR_CODES;
78
79
 
@@ -588,6 +589,21 @@ function reservedFolder(cwd, dir) {
588
589
  return null;
589
590
  }
590
591
 
592
+ /**
593
+ * The compile of an editor theme as it runs from the folder the command ran
594
+ * in: an editor theme compiles from the app root, so from a subfolder the
595
+ * line goes there first.
596
+ * @param {string} cwd the app root
597
+ * @param {string} slug
598
+ */
599
+ function compileFromHere(cwd, slug) {
600
+ const line = `${P.replace(/ editor$/, '')} compile ${shown(cwd, themeFiles(cwd, slug).ts)}`;
601
+ const up = path.relative(process.cwd(), cwd);
602
+ return up && path.resolve(process.cwd()) !== path.resolve(cwd)
603
+ ? `cd ${quoteCommandArgument(up.split(path.sep).join('/'))} && ${line}`
604
+ : line;
605
+ }
606
+
591
607
  /**
592
608
  * The app's tsconfig.json compiler options (comments and trailing commas
593
609
  * allowed), or null when there is none.
@@ -999,7 +1015,12 @@ function checkPlacement(cwd, dir, where, retry) {
999
1015
  if (served && app !== served)
1000
1016
  refuse(
1001
1017
  E.ERR_INVALID_ARGUMENT,
1002
- `${where} is in ${shown(cwd, app)}/, and Next serves ${shown(cwd, served)}/ instead when both are there.`,
1018
+ // Next serves the root app/ when both are there: a preview in src/app/
1019
+ // under a root app/ is not served, and a new root app/ beside the
1020
+ // app's src/app/ would hide the whole app.
1021
+ path.resolve(app) === path.join(path.resolve(cwd), 'app')
1022
+ ? `${where} would make ${shown(cwd, app)}/ beside ${shown(cwd, served)}/, and Next serves the root app/ when both are there, so the whole app in ${shown(cwd, served)}/ would stop being served.`
1023
+ : `${where} is in ${shown(cwd, app)}/, and Next serves ${shown(cwd, served)}/ instead when both are there.`,
1003
1024
  {
1004
1025
  run: fs.existsSync(path.join(dir, PREVIEW_DATA))
1005
1026
  ? [
@@ -1098,11 +1119,30 @@ function previewDir(cwd, where, opts = {}) {
1098
1119
  checkFolderName(where, '--path', retry);
1099
1120
  /** @type {string} */
1100
1121
  let dir;
1122
+ // A --path read from the folder the command ran in; one that names a
1123
+ // preview from the project root (as every printed folder is) is read
1124
+ // from there, so a printed tip works from a subfolder too.
1125
+ const fromRoot = (() => {
1126
+ // The --path as given from the subfolder the command ran in, rebased
1127
+ // onto the project root by the caller: when that names no folder and
1128
+ // the path without the subfolder names a preview, that one.
1129
+ const sub = path.relative(cwd, process.cwd());
1130
+ if (!sub || sub.startsWith('..') || path.isAbsolute(where)) return null;
1131
+ const given = path.normalize(where);
1132
+ if (!given.startsWith(sub + path.sep)) return null;
1133
+ const there = path.resolve(cwd, given.slice(sub.length + 1));
1134
+ return !fs.existsSync(path.resolve(cwd, where)) &&
1135
+ fs.existsSync(path.join(there, PREVIEW_DATA))
1136
+ ? there
1137
+ : null;
1138
+ })();
1101
1139
  try {
1102
- dir = assertWithin(path.resolve(cwd, where), cwd, {
1103
- allowAbsolute: true,
1104
- label: '--path',
1105
- });
1140
+ dir =
1141
+ fromRoot ??
1142
+ assertWithin(path.resolve(cwd, where), cwd, {
1143
+ allowAbsolute: true,
1144
+ label: '--path',
1145
+ });
1106
1146
  } catch (e) {
1107
1147
  if (e instanceof PathSafetyError)
1108
1148
  refuse(
@@ -1164,11 +1204,11 @@ function previewDir(cwd, where, opts = {}) {
1164
1204
  const candidates = routes.map(r => path.join(r, 'theme-preview'));
1165
1205
  const known = candidates.find(d => fs.existsSync(path.join(d, PREVIEW_DATA)));
1166
1206
  if (!known && opts.retry) {
1167
- // A preview added with --path is recorded with its theme, so a later
1168
- // command finds it without the flag instead of starting a second one.
1169
- const recorded = [
1170
- ...new Set(editedThemes(cwd).flatMap(t => recordedPreviews(cwd, t))),
1171
- ].filter(d => fs.existsSync(path.join(d, PREVIEW_DATA)));
1207
+ // A preview added with --path is recorded with its theme, and one
1208
+ // renamed by hand is found in the routes folders (as list, remove and
1209
+ // see find it), so a later command uses it instead of starting a second
1210
+ // one.
1211
+ const recorded = allPreviews(cwd);
1172
1212
  if (recorded.length === 1) {
1173
1213
  inProjectOrRefuse(cwd, recorded[0], retry);
1174
1214
  if (opts.write)
@@ -1258,7 +1298,7 @@ function nextAppsUnder(cwd) {
1258
1298
  }
1259
1299
  };
1260
1300
  walk(cwd, 1);
1261
- return out.sort((a, b) => a.root.localeCompare(b.root));
1301
+ return out.sort((a, b) => compareText(a.root, b.root));
1262
1302
  }
1263
1303
 
1264
1304
  /**
@@ -1281,7 +1321,7 @@ export function addWiring(cwd, slug, mode = 'system') {
1281
1321
  : [
1282
1322
  `${slug} is ${mode}-only: give the <Theme> that theme add's wiring puts in the app root mode="${mode}" (<Theme theme={…} mode="${mode}">); the theme keeps both halves of its colors, so a toast or a lightbox Core draws in the other mode stays readable`,
1283
1323
  ]),
1284
- `every set and undo builds the theme and its app copy again, so the app takes each change; after editing ${shown(cwd, themeFiles(cwd, slug).ts)} by hand, ${P.replace(/ editor$/, '')} compile ${shown(cwd, themeFiles(cwd, slug).ts)} builds both again`,
1324
+ `every set and undo builds the theme and its app copy again, so the app takes each change; after editing ${shown(cwd, themeFiles(cwd, slug).ts)} by hand, ${compileFromHere(cwd, slug)} builds both again`,
1285
1325
  ];
1286
1326
  }
1287
1327
 
@@ -1324,7 +1364,7 @@ export function appWiring(ctx, slug, data) {
1324
1364
  return [
1325
1365
  ...(blocker
1326
1366
  ? [
1327
- `${blocker} is not the editor's, so the editor does not write the theme's app copy in src/themes/${slug}/ (and theme add does not take it); the steps by hand:`,
1367
+ `the app copy in src/themes/${slug}/ was not updated: ${blocker}; the steps by hand:`,
1328
1368
  ]
1329
1369
  : []),
1330
1370
  `in ${where}, import the theme and its stylesheets: import {Theme} from '@astryxdesign/core/theme'; import {${name}} from '${ti.module.replace(/\.js$/, '')}'; ${ti.css.map(c => `import '${c}';`).join(' ')}`,
@@ -1334,7 +1374,7 @@ export function appWiring(ctx, slug, data) {
1334
1374
  `${appModule} (written by theme add and use) stays as it is: to keep its themes too, merge them in your own file ({...themes, ${JSON.stringify(slug)}: ${name}}), never in ${appModule}, which add, use and remove write again`,
1335
1375
  ]
1336
1376
  : []),
1337
- `every set and undo builds ${shown(cwd, themeFiles(cwd, slug).js)} and ${shown(cwd, themeFiles(cwd, slug).css)} again, so the app takes each change; after editing ${shown(cwd, themeFiles(cwd, slug).ts)} by hand, ${P.replace(/ editor$/, '')} compile ${shown(cwd, themeFiles(cwd, slug).ts)} builds it again`,
1377
+ `every set and undo builds ${shown(cwd, themeFiles(cwd, slug).js)} and ${shown(cwd, themeFiles(cwd, slug).css)} again, so the app takes each change; after editing ${shown(cwd, themeFiles(cwd, slug).ts)} by hand, ${compileFromHere(cwd, slug)} builds it again`,
1338
1378
  ];
1339
1379
  }
1340
1380
 
@@ -1501,7 +1541,7 @@ function planStarterApp(cwd, where, extra, dependencies, slug = null) {
1501
1541
  if (e instanceof PathSafetyError)
1502
1542
  refuse(
1503
1543
  E.ERR_PATH_TRAVERSAL,
1504
- `${where} is outside the current folder; make the app inside the folder you run the command from.`,
1544
+ `${where} is outside the project; make the app inside the project folder.`,
1505
1545
  {run: [again(null)]},
1506
1546
  );
1507
1547
  throw e;
@@ -1634,7 +1674,7 @@ function starterPackageJson(text, dependencies) {
1634
1674
  const pkg = JSON.parse(pinned);
1635
1675
  pkg.dependencies = Object.fromEntries(
1636
1676
  Object.entries({...pkg.dependencies, ...dependencies}).sort(([a], [b]) =>
1637
- a.localeCompare(b),
1677
+ compareText(a, b),
1638
1678
  ),
1639
1679
  );
1640
1680
  return `${JSON.stringify(pkg, null, 2)}\n`;
@@ -3205,8 +3245,11 @@ function dirFlag(cwd, dir) {
3205
3245
  */
3206
3246
  export async function refreshPreviewRoute(cwd, slug) {
3207
3247
  const at = previewDir(cwd, undefined, {optional: true});
3248
+ // The same previews `see` names: the usual folder, the recorded ones,
3249
+ // and any in the routes folders that wear the theme (a folder renamed by
3250
+ // hand, or one whose record was lost).
3208
3251
  const dirs = [
3209
- ...new Set([...(at ? [at.dir] : []), ...recordedPreviews(cwd, slug)]),
3252
+ ...new Set([...(at ? [at.dir] : []), ...themePreviews(cwd, slug)]),
3210
3253
  ];
3211
3254
  let changed = false;
3212
3255
  /** @type {string[]} */
@@ -3244,7 +3287,7 @@ export async function refreshPreviewRoute(cwd, slug) {
3244
3287
  stale.push(
3245
3288
  standIn
3246
3289
  ? `${shown(cwd, file)} (changed by hand, so it is kept; the theme has no generated colors now, so ${standIn}, which the page imports, holds an empty stand-in that keeps the app building. Give the page another name if you want to keep your changes, then run ${PREVIEW} add)`
3247
- : shown(cwd, file),
3290
+ : `${shown(cwd, file)} (changed by hand, so it does not follow this change: move it out of ${shownDir(cwd, dir)}/, keeping your copy elsewhere in the app and importing nothing from the preview folder, then run ${PREVIEW} add${dirFlag(cwd, dir)} to write it again)`,
3248
3291
  );
3249
3292
  return false;
3250
3293
  }
@@ -3260,7 +3303,20 @@ export async function refreshPreviewRoute(cwd, slug) {
3260
3303
  `${shownDir(cwd, dir)}/ (${slug} has generated colors now; ${PREVIEW} add${dirFlag(cwd, dir)} adds its Colors page)`,
3261
3304
  );
3262
3305
  if (!writes.length) continue;
3263
- commitPreview(cwd, dir, data, writes, [], `${EDITOR} show ${slug}`);
3306
+ // A route that cannot be written (its permissions, a folder in its
3307
+ // place) does not follow this change: said, never a quiet ok.
3308
+ try {
3309
+ commitPreview(cwd, dir, data, writes, [], `${EDITOR} show ${slug}`);
3310
+ } catch (e) {
3311
+ const code =
3312
+ /\b(E[A-Z]{2,})\b/.exec(
3313
+ `${/** @type {any} */ (e)?.code ?? ''} ${e instanceof Error ? e.message : ''}`,
3314
+ )?.[1] ?? 'it could not be written';
3315
+ stale.push(
3316
+ `${shownDir(cwd, dir)}/ (could not be written: ${code}, so the in-app preview does not follow this change; fix its permissions, then ${PREVIEW} add${dirFlag(cwd, dir)} writes it again)`,
3317
+ );
3318
+ continue;
3319
+ }
3264
3320
  changed = true;
3265
3321
  }
3266
3322
  return {changed, stale};
@@ -3339,11 +3395,53 @@ function routesOf(cwd, at) {
3339
3395
  function previewPath(cwd, at) {
3340
3396
  const app = routesOf(cwd, at);
3341
3397
  if (!app) return null;
3398
+ // A folder Next does not serve (_private, @slot, an intercepting route):
3399
+ // no URL, as add refuses it.
3400
+ if (unservedWhy(cwd, at.dir)) return null;
3342
3401
  const segs = urlSegments(app, at.dir);
3343
3402
  if (segs.some(seg => /^\[.+\]$/.test(seg))) return null;
3344
3403
  const base = nextBasePath(cwd);
3345
3404
  if (base == null) return null;
3346
- return `${base}/${segs.map(seg => encodeURIComponent(seg)).join('/')}`;
3405
+ // A folder named %5F… is served at the decoded name (`_x`), encoded as a
3406
+ // URL segment needs it.
3407
+ const decoded = (/** @type {string} */ seg) => {
3408
+ try {
3409
+ return decodeURIComponent(seg);
3410
+ } catch {
3411
+ return seg;
3412
+ }
3413
+ };
3414
+ return `${base}/${segs.map(seg => encodeURIComponent(decoded(seg))).join('/')}`;
3415
+ }
3416
+
3417
+ /**
3418
+ * Why a preview folder found in the routes folder is not served, or null.
3419
+ * @param {string} cwd
3420
+ * @param {string} dir
3421
+ * @returns {string | null}
3422
+ */
3423
+ function unservedWhy(cwd, dir) {
3424
+ const app = routesOf(cwd, {dir, app: null});
3425
+ if (!app) return null;
3426
+ // The same rules add keeps: src/app/ while app/ is what Next serves, and
3427
+ // a next.config whose page extensions leave out page.tsx.
3428
+ const served = [path.join(cwd, 'app'), path.join(cwd, 'src', 'app')].find(
3429
+ r => fs.existsSync(r) && fs.statSync(r).isDirectory(),
3430
+ );
3431
+ if (served && path.resolve(app) !== path.resolve(served))
3432
+ return `Next serves ${shown(cwd, served)}/ instead of ${shown(cwd, app)}/ when both are there`;
3433
+ try {
3434
+ const exts = readNextConfig(cwd).pageExtensions;
3435
+ if (exts && !exts.includes('tsx'))
3436
+ return `next.config sets pageExtensions to ${exts.join(', ')}, so Next does not serve page.tsx`;
3437
+ } catch {
3438
+ // A config this cannot read is not a reason to hide the URL.
3439
+ }
3440
+ // A [param] folder is served, with a value for it; only _private, @slot
3441
+ // and intercepting folders are not.
3442
+ const segs = path.relative(app, dir).split(path.sep);
3443
+ if (!segs.some(x => /^[_@]/.test(x) || INTERCEPT.test(x))) return null;
3444
+ return unroutedSegment(app, dir);
3347
3445
  }
3348
3446
 
3349
3447
  /**
@@ -3374,27 +3472,59 @@ function pageUrls(cwd, at, ids) {
3374
3472
  * @param {'colors' | 'components' | 'overview'} focus
3375
3473
  * @param {{code: string, cause: string} | null} [notChecked] why the change
3376
3474
  * could not be rendered
3475
+ * @param {{pictureProblem?: string | null, stale?: string[]}} [extra] this
3476
+ * result's picture problem (what kept --png from taking pictures) and its
3477
+ * previewStale entries (an in-app preview changed by hand)
3377
3478
  * @returns {string}
3378
3479
  */
3379
- export function seeLine(cwd, slug, focus, notChecked = null) {
3480
+ export function seeLine(cwd, slug, focus, notChecked = null, extra = {}) {
3380
3481
  /** @type {string[]} */
3381
3482
  const urls = [];
3382
3483
  /** Previews whose URL depends on the app's routing. @type {string[]} */
3383
3484
  const unplain = [];
3384
- // The previews recorded for the theme, and any found in the routes
3385
- // folders that wear it (a folder renamed by hand), as remove finds them.
3386
- const dirs = [
3387
- ...new Set([...recordedPreviews(cwd, slug), ...previewsIn(cwd)]),
3388
- ];
3485
+ /** Previews in a folder Next does not serve. @type {string[]} */
3486
+ const unserved = [];
3487
+ /** Their folders, as --path takes them. @type {string[]} */
3488
+ const unservedDirs = [];
3489
+ /** @type {string[]} */
3490
+ const stale = extra.stale ?? [];
3491
+ /** Previews whose route was changed by hand. @type {string[]} */
3492
+ const handEdited = [];
3493
+ /** Previews whose record is damaged. @type {string[]} */
3494
+ const damaged = [];
3495
+ const dirs = themePreviews(cwd, slug);
3389
3496
  for (const dir of dirs) {
3390
3497
  /** @type {any} */
3391
3498
  let data;
3392
3499
  try {
3393
3500
  data = JSON.parse(fs.readFileSync(path.join(dir, PREVIEW_DATA), 'utf8'));
3394
- } catch {
3501
+ } catch (e) {
3502
+ // A record that is there but damaged: the preview commands refuse
3503
+ // it until it is fixed, so no add tip, the repair instead.
3504
+ if (
3505
+ fs.existsSync(path.join(dir, PREVIEW_DATA)) &&
3506
+ !(/** @type {any} */ (e)?.code)
3507
+ )
3508
+ damaged.push(shownDir(cwd, dir));
3395
3509
  continue;
3396
3510
  }
3397
3511
  if (data?.theme !== slug) continue;
3512
+ const why = unservedWhy(cwd, dir);
3513
+ if (why) {
3514
+ unserved.push(`${shownDir(cwd, dir)}/ (${why})`);
3515
+ unservedDirs.push(shownDir(cwd, dir));
3516
+ continue;
3517
+ }
3518
+ if (
3519
+ stale.some(
3520
+ f =>
3521
+ f.startsWith(`${shownDir(cwd, dir)}/`) &&
3522
+ /changed by hand|could not be written/.test(f),
3523
+ )
3524
+ ) {
3525
+ handEdited.push(shownDir(cwd, dir));
3526
+ continue;
3527
+ }
3398
3528
  const ids = Array.isArray(data?.pages)
3399
3529
  ? data.pages.map((/** @type {any} */ x) => String(x?.id))
3400
3530
  : [];
@@ -3408,16 +3538,56 @@ export function seeLine(cwd, slug, focus, notChecked = null) {
3408
3538
  );
3409
3539
  }
3410
3540
  const f = themeFiles(cwd, slug);
3541
+ // What keeps --png from taking pictures, when this result's own picture
3542
+ // run failed (a file of yours named like a picture).
3543
+ // A command that only takes pictures, whatever this one was (an undo run
3544
+ // again undoes another change; new refuses a theme that exists).
3545
+ const pngNote = extra.pictureProblem
3546
+ ? `--png took no pictures: ${extra.pictureProblem}`
3547
+ : `${EDITOR} show ${slug} --png takes pictures in ${shownDir(cwd, f.dir)}/ with no dev server (it needs a Playwright or a Chrome)`;
3411
3548
  const own = notChecked
3412
3549
  ? /could not be written/.test(notChecked.cause)
3413
3550
  ? `the change is saved, but ${notChecked.cause}; --png takes no pictures until that is fixed`
3414
3551
  : `the change is saved, but the components could not be rendered here (${notChecked.cause}), so ${shown(cwd, f.preview)} still shows the theme as it was and --png takes no pictures until that is fixed`
3415
- : `look without changing your app: open or screenshot ${shown(cwd, f.preview)} (one self-contained page, current after this change; ${EDITOR} show ${slug} also writes ${shown(cwd, f.sheet)}, every component), or run it with --png for pictures in ${shownDir(cwd, f.dir)}/ with no dev server (it needs a Playwright or a Chrome)`;
3552
+ : `look without changing your app: open or screenshot ${shown(cwd, f.preview)} (one self-contained page, current after this change; ${EDITOR} show ${slug} also writes ${shown(cwd, f.sheet)}, every component), or ${pngNote}`;
3553
+ const edited = handEdited.length
3554
+ ? `; the in-app preview in ${handEdited.slice(0, 2).join(', ')}/ ${stale.some(f => /could not be written/.test(f)) && !stale.some(f => /changed by hand/.test(f)) ? 'could not be written' : 'was changed by hand'}, so it does not follow this change (previewStale says what to move out, then ${PREVIEW} add writes it again)`
3555
+ : '';
3416
3556
  if (urls.length)
3417
- return `${own}; in the running app: open ${urls.slice(0, 2).join(' or ')}`;
3557
+ return `${own}; in the running app: open ${urls.slice(0, 2).join(' or ')}${edited}`;
3418
3558
  if (unplain.length)
3419
- return `${own}; the in-app preview is in ${unplain.slice(0, 2).join(', ')}, at a URL that depends on the app's routing`;
3420
- return `${own}; to see it inside your app, ${PREVIEW} add puts the preview pages there, each at its own URL`;
3559
+ return `${own}; the in-app preview is in ${unplain.slice(0, 2).join(', ')}, at a URL that depends on the app's routing${edited}`;
3560
+ if (unserved.length) {
3561
+ // What fixes it, by why it is not served: add's own answer for each.
3562
+ const why = unserved.join(' ');
3563
+ const fix = /Next serves .* when both are there/.test(why)
3564
+ ? `${PREVIEW} remove --path ${quoteCommandArgument(unservedDirs[0])}, then ${PREVIEW} add --theme ${slug} puts it in the folder Next serves`
3565
+ : /pageExtensions/.test(why)
3566
+ ? `add "tsx" to pageExtensions in next.config so Next serves the preview's page.tsx`
3567
+ : `move it back into a folder Next routes, or make one there with ${PREVIEW} add --theme ${slug} --path <a routed folder>`;
3568
+ return `${own}; the in-app preview is in ${unserved.slice(0, 2).join(', ')}, which the app does not serve: ${fix}${edited}`;
3569
+ }
3570
+ if (edited) return `${own}${edited}`;
3571
+ if (damaged.length)
3572
+ return `${own}; the in-app preview in ${damaged.slice(0, 2).join(', ')}/ has a damaged ${PREVIEW_DATA}, so it does not follow this change: fix it by hand, or give the folder a new name that starts with _ and run ${PREVIEW} add${editorThemeCount(cwd) > 1 ? ` --theme ${slug}` : ''}`;
3573
+ // With more than one editor theme, a bare add may wear another one.
3574
+ const many = editorThemeCount(cwd) > 1;
3575
+ return `${own}; to see it inside your app, ${PREVIEW} add${many ? ` --theme ${slug}` : ''} puts the preview pages there, each at its own URL`;
3576
+ }
3577
+
3578
+ /**
3579
+ * How many editor themes the project has.
3580
+ * @param {string} cwd
3581
+ */
3582
+ function editorThemeCount(cwd) {
3583
+ const root = themeFiles(cwd, 'x').root;
3584
+ try {
3585
+ return fs
3586
+ .readdirSync(root)
3587
+ .filter(d => fs.existsSync(themeFiles(cwd, d).settings)).length;
3588
+ } catch {
3589
+ return 0;
3590
+ }
3421
3591
  }
3422
3592
 
3423
3593
  /**
@@ -3485,9 +3655,10 @@ function skipReason(s) {
3485
3655
  * @param {string} cwd
3486
3656
  * @param {string[]} notes notes for the result
3487
3657
  * @param {string} flags the caller's other flags, for the suggestions
3658
+ * @param {string | null} [appRoot] the app whose packages a page needs
3488
3659
  * @returns {Promise<Array<{name: string, displayName: string, why: string}>>}
3489
3660
  */
3490
- async function rankedPages(words, cwd, notes, flags) {
3661
+ async function rankedPages(words, cwd, notes, flags, appRoot = null) {
3491
3662
  const kit =
3492
3663
  /** @type {import('../build/build.type.mjs').BuildKitResponse} */ (
3493
3664
  await build(words, {cwd})
@@ -3571,15 +3742,33 @@ async function rankedPages(words, cwd, notes, flags) {
3571
3742
  const label = name => listed.find(t => t.id === name)?.name ?? name;
3572
3743
  const suggest = (/** @type {string[]} */ list) =>
3573
3744
  list.slice(0, 3).map(n => `${PREVIEW} add --template ${n}${flags}`);
3574
- // With no page close, a few pages that need nothing more, and the list.
3575
- const fallback = [
3576
- ...suggest(
3577
- ['dashboard', 'settings', 'login'].filter(id =>
3578
- listed.some(t => t.id === id),
3579
- ),
3580
- ),
3581
- formatCliCommand('template --list --type page', cwd),
3582
- ];
3745
+ // Only a page this app can show is suggested: one whose packages are
3746
+ // installed and that needs no StyleX compiler, as add itself checks.
3747
+ const showable = async (/** @type {string} */ id) => {
3748
+ try {
3749
+ const shownTemplate = /** @type {any} */ (
3750
+ await template(id, {cwd, type: 'page'})
3751
+ );
3752
+ const needs = pageNeeds(appRoot ?? cwd, shownTemplate.data.source);
3753
+ return (
3754
+ !needs.missingPackages.length &&
3755
+ !needs.notInstalled.length &&
3756
+ !needs.stylexCompiler
3757
+ );
3758
+ } catch {
3759
+ return false;
3760
+ }
3761
+ };
3762
+ /** @param {string[]} ids */
3763
+ const showableOf = async ids => {
3764
+ /** @type {string[]} */
3765
+ const out = [];
3766
+ for (const id of ids) {
3767
+ if (out.length >= 3) break;
3768
+ if (await showable(id)) out.push(id);
3769
+ }
3770
+ return out;
3771
+ };
3583
3772
  // No page relates when the ranking fell back to a frame with no direct
3584
3773
  // match, and no page the search found matches three words of the request
3585
3774
  // or two fifths of them.
@@ -3604,19 +3793,32 @@ async function rankedPages(words, cwd, notes, flags) {
3604
3793
  !found.some(
3605
3794
  (/** @type {any} */ r) => !frames.has(r.name) && termsMatched(r) >= 2,
3606
3795
  )));
3607
- if (unrelated)
3796
+ if (unrelated) {
3797
+ // With no page close, a few pages this app can show, and the list.
3798
+ const offered = await showableOf(
3799
+ pages.length
3800
+ ? pages
3801
+ : [
3802
+ ...['dashboard', 'settings', 'login'],
3803
+ ...listed.map(t => t.id),
3804
+ ].filter(
3805
+ (id, i, all) =>
3806
+ all.indexOf(id) === i &&
3807
+ listed.some(t => t.id === id) &&
3808
+ !frames.has(id),
3809
+ ),
3810
+ );
3608
3811
  refuse(
3609
3812
  E.ERR_NOT_FOUND,
3610
3813
  `No page template relates to "${words}". Name a page to add it anyway.`,
3611
3814
  {
3612
- run: pages.length
3613
- ? [
3614
- ...suggest(pages),
3615
- formatCliCommand('template --list --type page', cwd),
3616
- ]
3617
- : fallback,
3815
+ run: [
3816
+ ...suggest(offered),
3817
+ formatCliCommand('template --list --type page', cwd),
3818
+ ],
3618
3819
  },
3619
3820
  );
3821
+ }
3620
3822
  if (skippedFrames.length)
3621
3823
  notes.push(
3622
3824
  `left out ${skippedFrames.join(', ')}: app frames, not pages (add one with --template <id>)`,
@@ -4287,7 +4489,7 @@ async function addPreview(description, options, box) {
4287
4489
  const flags = `${tf}${pf}`;
4288
4490
  const picks = explicit
4289
4491
  ? [await namedTemplate(cwd, /** @type {string} */ (named), flags)]
4290
- : await rankedPages(words, cwd, notes, flags);
4492
+ : await rankedPages(words, cwd, notes, flags, appRootOf(cwd, at.dir));
4291
4493
  for (const [i, pick] of picks.entries()) {
4292
4494
  if (add.length >= (explicit ? 1 : top)) break;
4293
4495
  const shownTemplate =
@@ -4491,10 +4693,17 @@ async function addPreview(description, options, box) {
4491
4693
  } catch {
4492
4694
  // A theme that does not load is reported where it is used.
4493
4695
  }
4494
- if (again)
4495
- notes.push(
4496
- `a default page's id brings back the default pages that are missing, as ${PREVIEW} add does`,
4696
+ // What happened: only the page asked for; the others come with a bare
4697
+ // add.
4698
+ if (again) {
4699
+ const stillMissing = DEFAULT_IDS.filter(
4700
+ id => !data.pages.some(p => p.id === id),
4497
4701
  );
4702
+ if (stillMissing.length)
4703
+ notes.push(
4704
+ `only ${askedId} was added; ${PREVIEW} add with no page brings back the other default pages that are missing (${stillMissing.join(', ')})`,
4705
+ );
4706
+ }
4498
4707
  notes.push(...typeScriptNotes(cwd, at.dir, added));
4499
4708
  if (
4500
4709
  askedId === COLORS_PAGE.id &&
@@ -4608,6 +4817,8 @@ export async function themeEditPreviewApp(dir, options = {}) {
4608
4817
  from.sheet,
4609
4818
  from.previewPng,
4610
4819
  from.sheetPng,
4820
+ // The record of pictures, none of which come along.
4821
+ from.pictures,
4611
4822
  ].map(f => path.basename(f)),
4612
4823
  );
4613
4824
  // The whole folder, tokens/ included, as bytes: a font or a picture
@@ -4622,6 +4833,12 @@ export async function themeEditPreviewApp(dir, options = {}) {
4622
4833
  else if (
4623
4834
  entry.isFile() &&
4624
4835
  !(rel === '' && skip.has(entry.name)) &&
4836
+ // No picture or preview page of any kind (dark pictures, section
4837
+ // sheets): the new app builds its own.
4838
+ !(
4839
+ rel === '' &&
4840
+ /^(preview|sheet)(-[a-z0-9-]+)?\.(html|png)$/.test(entry.name)
4841
+ ) &&
4625
4842
  // A temp file a stopped command left is no part of the theme.
4626
4843
  !isEditorTemp(entry.name)
4627
4844
  )
@@ -4854,7 +5071,9 @@ export async function themeEditPreviewList(options = {}) {
4854
5071
  /** @type {string[]} */
4855
5072
  const notes = [];
4856
5073
  const themeMissing = Boolean(
4857
- data?.theme && !fs.existsSync(themeFiles(cwd, data.theme).dir),
5074
+ data?.theme &&
5075
+ !fs.existsSync(themeFiles(cwd, data.theme).dir) &&
5076
+ !(at && themeElsewhere(cwd, at.dir, data.theme)),
4858
5077
  );
4859
5078
  if (at && fs.existsSync(path.join(at.dir, PENDING)))
4860
5079
  notes.push(
@@ -4943,7 +5162,92 @@ export async function themeEditPreviewList(options = {}) {
4943
5162
  * @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewRemoveResponse>}
4944
5163
  */
4945
5164
  export async function themeEditPreviewRemove(page, options = {}) {
4946
- return reportingRecovery(box => removePreview(page, options, box));
5165
+ try {
5166
+ return await reportingRecovery(box => removePreview(page, options, box));
5167
+ } catch (e) {
5168
+ // Removing a page needs the theme the route wears; removing the whole
5169
+ // preview needs none. Every refusal over a theme that does not load
5170
+ // names it, wherever the theme was read.
5171
+ const err = /** @type {any} */ (e);
5172
+ const pf =
5173
+ options.path != null
5174
+ ? ` --path ${quoteCommandArgument(options.path)}`
5175
+ : '';
5176
+ const whole = `${PREVIEW} remove${pf}`;
5177
+ if (
5178
+ page != null &&
5179
+ [E.ERR_THEME_INVALID, E.ERR_UNKNOWN_THEME, E.ERR_THEME_LOAD].includes(
5180
+ err?.code,
5181
+ ) &&
5182
+ !(err.suggestions ?? []).some((/** @type {any} */ x) => x?.name === whole)
5183
+ )
5184
+ err.suggestions = [
5185
+ {
5186
+ name: whole,
5187
+ reason:
5188
+ 'do this instead: removes the whole preview, which needs no theme',
5189
+ },
5190
+ ...(err.suggestions ?? []),
5191
+ ];
5192
+ throw e;
5193
+ }
5194
+ }
5195
+
5196
+ /**
5197
+ * The theme folder a preview's route imports when it is outside this
5198
+ * project's themes/ (a workspace app run from its own folder, its themes at
5199
+ * the workspace root), or null.
5200
+ * @param {string} cwd
5201
+ * @param {string} dir the preview folder
5202
+ * @param {string} slug
5203
+ * @returns {string | null}
5204
+ */
5205
+ function themeElsewhere(cwd, dir, slug) {
5206
+ if (fs.existsSync(themeFiles(cwd, slug).dir)) return null;
5207
+ /** @type {string} */
5208
+ let route;
5209
+ try {
5210
+ route = fs.readFileSync(path.join(dir, 'page.tsx'), 'utf8');
5211
+ } catch {
5212
+ return null;
5213
+ }
5214
+ const esc = slug.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
5215
+ const m = new RegExp(`['"]([^'"]*themes/${esc})/${esc}[^'"]*['"]`).exec(
5216
+ route,
5217
+ );
5218
+ if (!m || !m[1].startsWith('.')) return null;
5219
+ const found = path.resolve(dir, m[1]);
5220
+ return fs.existsSync(path.join(found, `${slug}.settings.json`))
5221
+ ? found
5222
+ : null;
5223
+ }
5224
+
5225
+ /**
5226
+ * The previews a theme may have: the ones recorded with it (added with
5227
+ * --path) and every preview in the routes folders (one renamed by hand, or
5228
+ * one whose record was lost). The one list `see`, a change's refresh and
5229
+ * the preview commands read; each reads a preview's record for its theme.
5230
+ * @param {string} cwd
5231
+ * @param {string} slug
5232
+ * @returns {string[]}
5233
+ */
5234
+ function themePreviews(cwd, slug) {
5235
+ return [...new Set([...recordedPreviews(cwd, slug), ...previewsIn(cwd)])];
5236
+ }
5237
+
5238
+ /**
5239
+ * Every preview in the app, of any editor theme: the previews of each, by
5240
+ * {@link themePreviews}, that are there.
5241
+ * @param {string} cwd
5242
+ * @returns {string[]}
5243
+ */
5244
+ function allPreviews(cwd) {
5245
+ return [
5246
+ ...new Set([
5247
+ ...editedThemes(cwd).flatMap(t => themePreviews(cwd, t)),
5248
+ ...previewsIn(cwd),
5249
+ ]),
5250
+ ].filter(d => fs.existsSync(path.join(d, PREVIEW_DATA)));
4947
5251
  }
4948
5252
 
4949
5253
  /**
@@ -4994,6 +5298,24 @@ async function removePreview(page, options, box) {
4994
5298
  const retry = (/** @type {string | null} */ p) =>
4995
5299
  `${PREVIEW} remove${page != null ? ` ${quoteCommandArgument(page)}` : ''}${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`;
4996
5300
  const at = previewDir(cwd, options.path, {retry});
5301
+ // A file given for the preview's folder: its folder when the file is a
5302
+ // preview's own, else the previews there are (as add refuses a file).
5303
+ if (fs.existsSync(at.dir) && !fs.statSync(at.dir).isDirectory()) {
5304
+ const parent = path.dirname(at.dir);
5305
+ const own = fs.existsSync(path.join(parent, PREVIEW_DATA));
5306
+ const others = previewsIn(cwd);
5307
+ refuse(
5308
+ E.ERR_INVALID_ARGUMENT,
5309
+ `${shown(cwd, at.dir)} is a file; a preview is a folder${own ? `, and this file is in the preview ${shownDir(cwd, parent)}/` : ''}.`,
5310
+ {
5311
+ run: own
5312
+ ? [retry(shownDir(cwd, parent))]
5313
+ : others.length
5314
+ ? others.slice(0, 3).map(d => retry(shownDir(cwd, d)))
5315
+ : [`${PREVIEW} list`],
5316
+ },
5317
+ );
5318
+ }
4997
5319
  // The pages a stopped change was removing, read before it is finished.
4998
5320
  /** @type {string[]} */
4999
5321
  let removing = [];
@@ -5179,6 +5501,20 @@ async function removePreview(page, options, box) {
5179
5501
  const wore = data.theme;
5180
5502
  /** @type {string | null} */
5181
5503
  let gone = null;
5504
+ // A theme the route imports from outside this project folder (a
5505
+ // workspace app run from its own folder) is not gone: the command is
5506
+ // refused, naming where to run it, and nothing is rewritten away.
5507
+ const away = wore ? themeElsewhere(cwd, at.dir, wore) : null;
5508
+ if (away)
5509
+ refuse(
5510
+ E.ERR_INVALID_ARGUMENT,
5511
+ `The preview wears ${wore}, whose theme folder is ${away}/, outside this project folder (${cwd}). Run the command from the folder that holds themes/${wore}/.`,
5512
+ {
5513
+ run: [
5514
+ `cd ${quoteCommandArgument(path.dirname(path.dirname(away)))} && ${PREVIEW} remove ${quoteCommandArgument(String(page))} --path ${quoteCommandArgument(path.relative(path.dirname(path.dirname(away)), at.dir))}`,
5515
+ ],
5516
+ },
5517
+ );
5182
5518
  if (wore && !fs.existsSync(themeFiles(cwd, wore).dir)) {
5183
5519
  gone = wore;
5184
5520
  data.theme = null;
@@ -5214,26 +5550,10 @@ async function removePreview(page, options, box) {
5214
5550
  let planned;
5215
5551
  /** @type {Awaited<ReturnType<typeof routeTheme>>} */
5216
5552
  let theme;
5217
- try {
5218
- theme = await routeTheme(cwd, at.dir, data, {build: false});
5219
- planned = await routeWrites(cwd, at.dir, data, theme, {gone});
5220
- } catch (e) {
5221
- const err = /** @type {any} */ (e);
5222
- if (
5223
- [E.ERR_THEME_INVALID, E.ERR_UNKNOWN_THEME, E.ERR_THEME_LOAD].includes(
5224
- err?.code,
5225
- )
5226
- )
5227
- err.suggestions = [
5228
- {
5229
- name: `${PREVIEW} remove${pf}`,
5230
- reason:
5231
- 'do this instead: removes the whole preview, which needs no theme',
5232
- },
5233
- ...(err.suggestions ?? []),
5234
- ];
5235
- throw e;
5236
- }
5553
+ // A theme that does not load is refused with the whole-preview remove
5554
+ // named (themeEditPreviewRemove adds it to every such refusal).
5555
+ theme = await routeTheme(cwd, at.dir, data, {build: false});
5556
+ planned = await routeWrites(cwd, at.dir, data, theme, {gone});
5237
5557
  const elsewhere = `${PREVIEW} remove${pf}`;
5238
5558
  /** @type {Map<string, {rel: string, text: string, keepHash?: boolean}>} */
5239
5559
  const writes = new Map(move.writes.map(w => [w.rel, w]));