@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
@@ -5,16 +5,17 @@
5
5
  * The data block for a page's files.
6
6
  * @param {string} entry the page's file name
7
7
  * @param {{path: string, bytes: Buffer}[]} files the page first
8
+ * @param {RenderRecord} [record] what the render records about itself
8
9
  * @returns {string}
9
10
  */
10
11
  export function sourceBlock(entry: string, files: {
11
12
  path: string;
12
13
  bytes: Buffer;
13
- }[]): string;
14
+ }[], record?: RenderRecord): string;
14
15
  /**
15
16
  * Read the embedded source from a rendered file.
16
17
  * @param {string} html
17
- * @returns {{found: false} | {found: true, source: EmbeddedSource} | {found: true, damaged: string}}
18
+ * @returns {{found: false} | {found: true, source: EmbeddedSource} | {found: true, damaged: string} | {found: true, newer: {format: number, cli: string | null}}}
18
19
  */
19
20
  export function readSourceBlock(html: string): {
20
21
  found: false;
@@ -24,10 +25,49 @@ export function readSourceBlock(html: string): {
24
25
  } | {
25
26
  found: true;
26
27
  damaged: string;
28
+ } | {
29
+ found: true;
30
+ newer: {
31
+ format: number;
32
+ cli: string | null;
33
+ };
27
34
  };
35
+ /**
36
+ * The markup between `<body>` and the embedded source: what a render wrote
37
+ * there, unless someone changed it since. Null when the file no longer has
38
+ * that shape.
39
+ * @param {string} html
40
+ * @returns {string | null}
41
+ */
42
+ export function renderedMarkupOf(html: string): string | null;
43
+ /**
44
+ * The title and the <style> element a render wrote in <head>, unless someone
45
+ * changed them since.
46
+ * @param {string} html
47
+ * @returns {{title: string | null, style: string | null}}
48
+ */
49
+ export function renderedHeadOf(html: string): {
50
+ title: string | null;
51
+ style: string | null;
52
+ };
53
+ /**
54
+ * The title and the runs of text a record keeps, or null when the record
55
+ * keeps none or they do not decode.
56
+ * @param {{text?: unknown}} markup
57
+ * @returns {{title: string | null, runs: string[]} | null}
58
+ */
59
+ export function recordedText(markup: {
60
+ text?: unknown;
61
+ }): {
62
+ title: string | null;
63
+ runs: string[];
64
+ } | null;
28
65
  /** The script type of the embedded-source block. */
29
66
  export const SOURCE_TYPE: "application/x-astryx-source";
30
- export function sha256(bytes: Buffer): string;
67
+ /** The render format this CLI writes, and the newest one it reads. */
68
+ export const RENDER_FORMAT: 1;
69
+ export function sha256(bytes: Buffer | string): string;
70
+ export function asParsed(text: string): string;
31
71
  export type EmbeddedFile = {
32
72
  /**
33
73
  * - Path relative to the folder that holds every embedded file, `/`-separated; the page itself first.
@@ -42,11 +82,38 @@ export type EmbeddedFile = {
42
82
  */
43
83
  base64: string;
44
84
  };
45
- export type EmbeddedSource = {
46
- version: 1;
85
+ /**
86
+ * What the render records about itself beside the files. Every field is
87
+ * optional: files from older CLIs have none of them.
88
+ */
89
+ export type RenderRecord = {
90
+ /**
91
+ * - The version of the CLI that rendered the file.
92
+ */
93
+ cli?: string | undefined;
94
+ /**
95
+ * - The page's path from its project's root, `/`-separated.
96
+ */
97
+ page?: string | undefined;
47
98
  /**
48
- * - The page's file name.
99
+ * - The title given with --title, which a render again needs too.
49
100
  */
101
+ title?: string | undefined;
102
+ /**
103
+ * - The packages the page rendered with, by name: their versions.
104
+ */
105
+ packages?: Record<string, string> | undefined;
106
+ /**
107
+ * - The page as rendered, so a reverse can tell what changed after the render: the SHA-256 of the markup in <body> and of the <style> element, and `text`, the base64 of the JSON `{title, runs}` (the title and the markup's runs of text in order). The text stays encoded so that a find-and-replace over the page's text cannot change the record too.
108
+ */
109
+ markup?: {
110
+ sha256: string;
111
+ style?: string;
112
+ text?: string;
113
+ } | undefined;
114
+ };
115
+ export type EmbeddedSource = RenderRecord & {
116
+ version: number;
50
117
  entry: string;
51
118
  files: EmbeddedFile[];
52
119
  };
@@ -7,14 +7,25 @@
7
7
  * it, and its JSON holds each file base64-encoded with its SHA-256, so no text
8
8
  * in the source (`</script>` included) can end the block early.
9
9
  *
10
+ * The block's `version` is the render format. A CLI reads every format up to
11
+ * its own. The number goes up only when a CLI that reads the current number
12
+ * would read a new file wrong; a new optional field keeps the number, so older
13
+ * CLIs keep reading newer files. A CLI that meets a higher number refuses and
14
+ * names the CLI version that wrote the file.
15
+ *
10
16
  * @position foundation/render — used by render-page.mjs and the render subject
11
17
  */
12
18
 
13
19
  import {createHash} from 'node:crypto';
20
+ import {isValidSemver} from '../env/semver.mjs';
21
+ import {decode, readTag, scanHtml} from './html-reader.mjs';
14
22
 
15
23
  /** The script type of the embedded-source block. */
16
24
  export const SOURCE_TYPE = 'application/x-astryx-source';
17
25
 
26
+ /** The render format this CLI writes, and the newest one it reads. */
27
+ export const RENDER_FORMAT = 1;
28
+
18
29
  /**
19
30
  * @typedef {object} EmbeddedFile
20
31
  * @property {string} path - Path relative to the folder that holds every embedded file, `/`-separated; the page itself first.
@@ -23,31 +34,53 @@ export const SOURCE_TYPE = 'application/x-astryx-source';
23
34
  */
24
35
 
25
36
  /**
26
- * @typedef {object} EmbeddedSource
27
- * @property {1} version
28
- * @property {string} entry - The page's file name.
29
- * @property {EmbeddedFile[]} files
37
+ * What the render records about itself beside the files. Every field is
38
+ * optional: files from older CLIs have none of them.
39
+ * @typedef {object} RenderRecord
40
+ * @property {string} [cli] - The version of the CLI that rendered the file.
41
+ * @property {string} [page] - The page's path from its project's root, `/`-separated.
42
+ * @property {string} [title] - The title given with --title, which a render again needs too.
43
+ * @property {Record<string, string>} [packages] - The packages the page rendered with, by name: their versions.
44
+ * @property {{sha256: string, style?: string, text?: string}} [markup] - The page as rendered, so a reverse can tell what changed after the render: the SHA-256 of the markup in <body> and of the <style> element, and `text`, the base64 of the JSON `{title, runs}` (the title and the markup's runs of text in order). The text stays encoded so that a find-and-replace over the page's text cannot change the record too.
45
+ */
46
+
47
+ /**
48
+ * @typedef {RenderRecord & {version: number, entry: string, files: EmbeddedFile[]}} EmbeddedSource
30
49
  */
31
50
 
32
- /** @param {Buffer} bytes */
51
+ /** @param {Buffer | string} bytes */
33
52
  export const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
34
53
 
54
+ /**
55
+ * Text as a browser's HTML parser reads it: a CR LF pair or a lone CR is a
56
+ * line feed. A copy whose line ends a mailer, an editor or git changed reads
57
+ * the same.
58
+ * @param {string} text
59
+ */
60
+ export const asParsed = text => text.replace(/\r\n?/g, '\n');
61
+
35
62
  /**
36
63
  * The data block for a page's files.
37
64
  * @param {string} entry the page's file name
38
65
  * @param {{path: string, bytes: Buffer}[]} files the page first
66
+ * @param {RenderRecord} [record] what the render records about itself
39
67
  * @returns {string}
40
68
  */
41
- export function sourceBlock(entry, files) {
69
+ export function sourceBlock(entry, files, record = {}) {
42
70
  /** @type {EmbeddedSource} */
43
71
  const payload = {
44
- version: 1,
72
+ version: RENDER_FORMAT,
45
73
  entry,
74
+ ...(record.cli ? {cli: record.cli} : {}),
75
+ ...(record.page ? {page: record.page} : {}),
76
+ ...(record.title === undefined ? {} : {title: record.title}),
77
+ ...(record.packages ? {packages: record.packages} : {}),
46
78
  files: files.map(f => ({
47
79
  path: f.path,
48
80
  sha256: sha256(f.bytes),
49
81
  base64: f.bytes.toString('base64'),
50
82
  })),
83
+ ...(record.markup ? {markup: record.markup} : {}),
51
84
  };
52
85
  const json = JSON.stringify(payload)
53
86
  .replace(/</g, '\\u003c')
@@ -56,31 +89,105 @@ export function sourceBlock(entry, files) {
56
89
  return `<script type="${SOURCE_TYPE}">${json}</script>`;
57
90
  }
58
91
 
92
+ /**
93
+ * The parts of a rendered file a reverse reads, found as a browser finds them:
94
+ * outside comments (the note is one, and it may quote a path that looks like
95
+ * a tag) and outside the text of a style, script, title or textarea element
96
+ * (a stylesheet may hold any text).
97
+ * @param {string} html
98
+ * @returns {{title: string | null, style: string | null, body: number}} the
99
+ * text of the first title and the first style element before <body>, and
100
+ * where the content of <body> starts (-1 without a <body> tag)
101
+ */
102
+ function pageParts(html) {
103
+ /** @type {string | null} */
104
+ let title = null;
105
+ /** @type {string | null} */
106
+ let style = null;
107
+ let styleStart = -1;
108
+ let titleNext = false;
109
+ for (const token of scanHtml(html)) {
110
+ if (token.kind === 'start') {
111
+ if (token.tag === 'body') return {title, style, body: token.end};
112
+ titleNext = token.tag === 'title' && title === null;
113
+ if (token.tag === 'style' && styleStart < 0) styleStart = token.start;
114
+ } else if (token.kind === 'raw' && titleNext) {
115
+ title = html.slice(token.start, token.end);
116
+ titleNext = false;
117
+ } else if (token.kind === 'end' && token.tag === 'style' && styleStart >= 0 && style === null) {
118
+ style = html.slice(styleStart, token.end);
119
+ }
120
+ }
121
+ return {title, style, body: -1};
122
+ }
123
+
124
+ /**
125
+ * Whether a script tag's attributes give it the source type. A MIME type is
126
+ * matched without regard to letter case.
127
+ * @param {Array<[string, string]>} attrs
128
+ */
129
+ const isSourceType = attrs => {
130
+ const type = attrs.find(([name]) => name === 'type')?.[1];
131
+ return type !== undefined && decode(type).trim().toLowerCase() === SOURCE_TYPE;
132
+ };
133
+
134
+ /**
135
+ * Every embedded-source block in a file's body, in order: each script
136
+ * element whose type is the source type, its tag read as a browser reads a
137
+ * tag, with its attributes in any order, quoted or not, in any letter case
138
+ * (a minifier rewrites it so). Every `<script` from the start of the body on
139
+ * is read this way, inside a comment or other text too, so a hand edit that
140
+ * leaves a comment or a textarea open before the block does not hide it. The
141
+ * note in <head> may quote a title or a path with any text, so the search
142
+ * starts at <body>.
143
+ * @param {string} html
144
+ * @returns {{index: number, json: string, closed: boolean}[]} where each
145
+ * opening tag starts, the text of the block, and whether its end tag is
146
+ * there (a file cut inside the block has none)
147
+ */
148
+ const blocksIn = html => {
149
+ /** @type {{index: number, json: string, closed: boolean}[]} */
150
+ const blocks = [];
151
+ const opening = /<script(?=[\t\n\f\r />])/gi;
152
+ opening.lastIndex = Math.max(0, pageParts(html).body);
153
+ for (let m = opening.exec(html); m; m = opening.exec(html)) {
154
+ const tag = readTag(html, m.index + 1);
155
+ if (typeof tag !== 'object' || !isSourceType(tag.attrs)) continue;
156
+ const closing = /<\/script/gi;
157
+ closing.lastIndex = tag.end;
158
+ const end = closing.exec(html)?.index;
159
+ blocks.push({index: m.index, json: html.slice(tag.end, end ?? html.length), closed: end !== undefined});
160
+ opening.lastIndex = end ?? html.length;
161
+ }
162
+ return blocks;
163
+ };
164
+
59
165
  /**
60
166
  * Read the embedded source from a rendered file.
61
167
  * @param {string} html
62
- * @returns {{found: false} | {found: true, source: EmbeddedSource} | {found: true, damaged: string}}
168
+ * @returns {{found: false} | {found: true, source: EmbeddedSource} | {found: true, damaged: string} | {found: true, newer: {format: number, cli: string | null}}}
63
169
  */
64
170
  export function readSourceBlock(html) {
65
- const all = [
66
- ...html.matchAll(
67
- new RegExp(
68
- `<script\\s+type=["']${SOURCE_TYPE.replace(/[/+.-]/g, c => `\\${c}`)}["'][^>]*>([\\s\\S]*?)</script>`,
69
- 'gi',
70
- ),
71
- ),
72
- ];
73
171
  // The block is written last in the body; a page's own markup cannot end
74
172
  // it, so the last one is the CLI's.
75
- const m = all.at(-1);
76
- if (!m) return {found: false};
173
+ const block = blocksIn(html).at(-1);
174
+ if (!block) return {found: false};
175
+ if (!block.closed) return {found: true, damaged: 'its embedded source is cut short: the file ends inside it'};
77
176
  /** @type {any} */
78
177
  let payload;
79
178
  try {
80
- payload = JSON.parse(m[1]);
179
+ payload = JSON.parse(block.json);
81
180
  } catch {
82
181
  return {found: true, damaged: 'its embedded source is not valid JSON'};
83
182
  }
183
+ // A CLI version that is not a semantic version is never a render's: the
184
+ // file was changed, and the value would reach a printed command.
185
+ if (payload?.cli !== undefined && !isValidSemver(payload.cli)) {
186
+ return {found: true, damaged: 'its embedded source names a CLI version that is not a release version'};
187
+ }
188
+ if (Number.isInteger(payload?.version) && payload.version > RENDER_FORMAT) {
189
+ return {found: true, newer: {format: payload.version, cli: isValidSemver(payload.cli) ? payload.cli : null}};
190
+ }
84
191
  if (
85
192
  payload?.version !== 1 ||
86
193
  typeof payload.entry !== 'string' ||
@@ -104,3 +211,47 @@ export function readSourceBlock(html) {
104
211
  }
105
212
  return {found: true, source: payload};
106
213
  }
214
+
215
+ /**
216
+ * The markup between `<body>` and the embedded source: what a render wrote
217
+ * there, unless someone changed it since. Null when the file no longer has
218
+ * that shape.
219
+ * @param {string} html
220
+ * @returns {string | null}
221
+ */
222
+ export function renderedMarkupOf(html) {
223
+ const block = blocksIn(html).at(-1);
224
+ if (!block) return null;
225
+ const start = pageParts(html).body;
226
+ return start < 0 || start > block.index ? null : html.slice(start, block.index);
227
+ }
228
+
229
+ /**
230
+ * The title and the <style> element a render wrote in <head>, unless someone
231
+ * changed them since.
232
+ * @param {string} html
233
+ * @returns {{title: string | null, style: string | null}}
234
+ */
235
+ export function renderedHeadOf(html) {
236
+ const {title, style} = pageParts(html);
237
+ return {title, style};
238
+ }
239
+
240
+ /**
241
+ * The title and the runs of text a record keeps, or null when the record
242
+ * keeps none or they do not decode.
243
+ * @param {{text?: unknown}} markup
244
+ * @returns {{title: string | null, runs: string[]} | null}
245
+ */
246
+ export function recordedText(markup) {
247
+ if (typeof markup.text !== 'string') return null;
248
+ try {
249
+ const parsed = JSON.parse(Buffer.from(markup.text, 'base64').toString('utf8'));
250
+ return {
251
+ title: typeof parsed?.title === 'string' ? parsed.title : null,
252
+ runs: Array.isArray(parsed?.runs) ? parsed.runs.filter((/** @type {unknown} */ r) => typeof r === 'string') : [],
253
+ };
254
+ } catch {
255
+ return null;
256
+ }
257
+ }
@@ -2,12 +2,15 @@
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
4
  /**
5
- * The path patterns of the config in `dir` (tsconfig.json, else
6
- * jsconfig.json), or none. Comments and trailing commas are allowed.
7
- * @param {string} dir
8
- * @returns {PathPattern[] | null} null when neither file is there
5
+ * The path patterns that apply to a file, which need not exist yet: from the
6
+ * nearest tsconfig.json (else jsconfig.json) in its folder or above, read
7
+ * through `extends`. When that config does not take the file as its own, a
8
+ * project it references (at any depth) that does lends its patterns, as
9
+ * TypeScript finds a file's project behind a solution-style config.
10
+ * @param {string} file absolute
11
+ * @returns {PathPattern[]}
9
12
  */
10
- export function readPathsIn(dir: string): PathPattern[] | null;
13
+ export function pathsFor(file: string): PathPattern[];
11
14
  /**
12
15
  * The pattern a specifier matches by TypeScript's rules, and what its `*`
13
16
  * matched: an exact key first, else the wildcard key with the longest prefix.
@@ -23,9 +26,10 @@ export function matchPath(patterns: PathPattern[], spec: string): {
23
26
  * The file a candidate path stands for: itself, with an extension, or its
24
27
  * folder's index; null when none exists.
25
28
  * @param {string} candidate
29
+ * @param {readonly string[]} [extensions] the extensions to try, in order (default: TypeScript's)
26
30
  * @returns {string | null}
27
31
  */
28
- export function existingFile(candidate: string): string | null;
32
+ export function existingFile(candidate: string, extensions?: readonly string[]): string | null;
29
33
  /**
30
34
  * Resolve a specifier through path patterns: the first target that names an
31
35
  * existing file wins.
@@ -35,10 +39,53 @@ export function existingFile(candidate: string): string | null;
35
39
  * pattern or no target exists
36
40
  */
37
41
  export function resolvePath(patterns: PathPattern[], spec: string): string | null;
42
+ /** The extensions the render's module loader tries for an extensionless import, in its order. */
43
+ export const LOAD_EXTENSIONS: string[];
38
44
  export function candidatesOf(match: {
39
45
  pattern: PathPattern;
40
46
  capture: string;
41
47
  }): string[];
48
+ export type ReadConfig = {
49
+ /**
50
+ * - Compiler options, the extending config's winning key by key.
51
+ */
52
+ options: Record<string, unknown>;
53
+ /**
54
+ * - The folder of the config that writes `baseUrl`.
55
+ */
56
+ baseUrlDir: string;
57
+ /**
58
+ * - The folder of the config that writes `paths`.
59
+ */
60
+ pathsDir: string;
61
+ /**
62
+ * - `files`, from the nearest config that writes it, with that config's folder.
63
+ */
64
+ files: {
65
+ list: unknown[];
66
+ dir: string;
67
+ } | null;
68
+ /**
69
+ * - `include`, likewise.
70
+ */
71
+ include: {
72
+ list: unknown[];
73
+ dir: string;
74
+ } | null;
75
+ /**
76
+ * - `exclude`, likewise.
77
+ */
78
+ exclude: {
79
+ list: unknown[];
80
+ dir: string;
81
+ } | null;
82
+ /**
83
+ * - The config's own `references`.
84
+ */
85
+ references: {
86
+ path?: unknown;
87
+ }[];
88
+ };
42
89
  export type PathPattern = {
43
90
  /**
44
91
  * - The key as written.