@astryxdesign/cli 0.7.16 → 0.7.17-canary.g9a957aa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -23
- package/api/build/_adapter.d.mts +5 -0
- package/api/build/_adapter.mjs +49 -34
- package/api/build/blocks/blocks.d.mts +7 -5
- package/api/build/blocks/blocks.mjs +113 -26
- package/api/build/build.doc.mjs +2 -1
- package/api/build/kit/rank.mjs +47 -9
- package/api/build/kit/weights.d.mts +16 -3
- package/api/build/kit/weights.json +1 -1
- package/api/build/kit/weights.mjs +238 -31
- package/api/build/kit/word-model.d.mts +23 -0
- package/api/build/kit/word-model.mjs +46 -0
- package/api/build/kit/words.d.mts +32 -0
- package/api/build/kit/words.mjs +49 -1
- package/api/component/__fixtures__/resolutions/modules.txt +4 -5
- package/api/component/list/list.d.mts +1 -1
- package/api/component/list/list.mjs +48 -5
- package/api/discover/_adapter.d.mts +1 -1
- package/api/docs/_adapter.d.mts +26 -2
- package/api/docs/_adapter.mjs +217 -5
- package/api/docs/docs.d.mts +3 -0
- package/api/docs/docs.doc.mjs +7 -1
- package/api/docs/docs.mjs +12 -0
- package/api/docs/docs.type.d.mts +5 -0
- package/api/docs/docs.type.mjs +2 -0
- package/api/docs/node/node.mjs +1 -1
- package/api/doctor/_adapter.d.mts +27 -5
- package/api/doctor/_adapter.mjs +77 -6
- package/api/doctor/built-checks.mjs +38 -13
- package/api/doctor/doctor.d.mts +4 -3
- package/api/doctor/doctor.mjs +14 -7
- package/api/doctor/theme-checks.mjs +121 -17
- package/api/render/_adapter.mjs +491 -186
- package/api/render/render.doc.mjs +10 -3
- package/api/render/render.type.d.mts +30 -0
- package/api/render/render.type.mjs +19 -0
- package/api/search/ORDERING.md +1 -1
- package/api/search/_candidate-score.mjs +51 -32
- package/api/search/_fit-order.mjs +3 -3
- package/api/search/_result.d.mts +5 -5
- package/api/search/_score.d.mts +6 -1
- package/api/search/_score.mjs +15 -3
- package/api/search/search.d.mts +1 -1
- package/api/theme/_adapter.mjs +10 -0
- package/api/theme/build/build.d.mts +38 -0
- package/api/theme/build/build.mjs +144 -24
- package/api/theme/theme.type.d.mts +10 -0
- package/api/theme/theme.type.mjs +7 -1
- package/api/theme-edit/_adapter.d.mts +35 -0
- package/api/theme-edit/_adapter.mjs +477 -41
- package/api/theme-edit/_check.d.mts +28 -22
- package/api/theme-edit/_check.mjs +1077 -200
- package/api/theme-edit/_compiler.mjs +9 -0
- package/api/theme-edit/_context.mjs +60 -3
- package/api/theme-edit/_css.mjs +21 -1
- package/api/theme-edit/_edit.d.mts +22 -0
- package/api/theme-edit/_edit.mjs +932 -82
- package/api/theme-edit/_how-commands.d.mts +49 -0
- package/api/theme-edit/_how-commands.mjs +386 -1
- package/api/theme-edit/_how-question.d.mts +56 -0
- package/api/theme-edit/_how-question.mjs +3509 -168
- package/api/theme-edit/_how.mjs +104 -4
- package/api/theme-edit/_marks.d.mts +31 -5
- package/api/theme-edit/_marks.mjs +57 -5
- package/api/theme-edit/_normal.d.mts +137 -0
- package/api/theme-edit/_normal.mjs +612 -0
- package/api/theme-edit/_palette.mjs +10 -4
- package/api/theme-edit/_parts.d.mts +4 -0
- package/api/theme-edit/_parts.mjs +28 -2
- package/api/theme-edit/_preview-app.d.mts +7 -1
- package/api/theme-edit/_preview-app.mjs +191 -15
- package/api/theme-edit/_preview.d.mts +64 -8
- package/api/theme-edit/_preview.mjs +401 -83
- package/api/theme-edit/_settings.d.mts +139 -5
- package/api/theme-edit/_settings.mjs +1659 -112
- package/api/theme-edit/_tables.mjs +17 -1
- package/api/theme-edit/theme-edit.type.d.mts +10 -1
- package/api/theme-edit/theme-edit.type.mjs +4 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +2 -2
- package/assets/codemods/run-codemod.mjs +73 -6
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
- package/assets/docs/author-a-theme.doc.mjs +1 -0
- package/assets/docs/theme.doc.dense.mjs +2 -2
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
- package/assets/docs/tree/cli-command.doc.mjs +2 -3
- package/assets/docs/tree/render-pages.doc.mjs +43 -2
- package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
- package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
- package/assets/docs/use-a-theme.doc.mjs +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat/template.doc.mjs +8 -4
- package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
- package/assets/templates/pages/blank/template.doc.mjs +1 -1
- package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
- package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
- package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
- package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
- package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
- package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
- package/assets/templates/pages/documentation/template.doc.mjs +4 -1
- package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
- package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
- package/assets/templates/pages/editor/template.doc.mjs +1 -7
- package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
- package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
- package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
- package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
- package/assets/templates/pages/ide/page.tsx +24 -2
- package/assets/templates/pages/library/template.doc.mjs +4 -1
- package/assets/templates/pages/login/template.doc.mjs +2 -1
- package/assets/templates/pages/login-card/template.doc.mjs +2 -1
- package/assets/templates/pages/login-split/template.doc.mjs +2 -1
- package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
- package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
- package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
- package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
- package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
- package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
- package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
- package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
- package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
- package/assets/templates/pages/table-page/template.doc.mjs +5 -4
- package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
- package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -1
- package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
- package/authoring/debug/parse.d.mts +3 -3
- package/authoring/doctypes/_schema.d.mts +3 -3
- package/authoring/doctypes/command/command.doc.mjs +1 -1
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +9 -0
- package/clients/cli/bin/astryx.mjs +6 -9
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/component.doc.mjs +1 -1
- package/clients/cli/commands/component.mjs +20 -2
- package/clients/cli/commands/docs.doc.mjs +7 -1
- package/clients/cli/commands/docs.mjs +3 -1
- package/clients/cli/commands/doctor.mjs +22 -14
- package/clients/cli/commands/experimental/render.mjs +32 -1
- package/clients/cli/commands/experimental/theme.mjs +27 -2
- package/clients/cli/commands/experimental-render.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/search.doc.mjs +2 -1
- package/clients/cli/commands/search.mjs +6 -1
- package/clients/cli/commands/template.mjs +4 -2
- package/clients/cli/commands/theme-build.doc.mjs +5 -1
- package/clients/cli/commands/theme.doc.mjs +1 -1
- package/clients/cli/commands/theme.mjs +138 -3
- package/clients/cli/commands/upgrade.mjs +42 -7
- package/clients/cli/index.mjs +1 -1
- package/clients/cli/lib/component-format.mjs +205 -98
- package/clients/cli/lib/hook-format.mjs +9 -19
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/config/theme-state.d.mts +13 -1
- package/foundation/config/theme-state.mjs +47 -7
- package/foundation/discovery/component-discovery.d.mts +11 -0
- package/foundation/discovery/component-discovery.mjs +27 -2
- package/foundation/discovery/component-replacement.mjs +130 -1
- package/foundation/discovery/entry-name-files.d.mts +43 -0
- package/foundation/discovery/entry-name-files.mjs +264 -0
- package/foundation/discovery/hook-discovery.d.mts +8 -2
- package/foundation/discovery/hook-discovery.mjs +20 -7
- package/foundation/discovery/runner-allowlist.d.mts +1 -0
- package/foundation/discovery/runner-allowlist.mjs +2 -0
- package/foundation/discovery/template-adapter.d.mts +52 -0
- package/foundation/discovery/template-adapter.mjs +169 -0
- package/foundation/discovery/theme-imports.d.mts +14 -0
- package/foundation/discovery/theme-imports.mjs +43 -1
- package/foundation/doc-compiler/import.mjs +7 -1
- package/foundation/doc-compiler/parse-readable.mjs +4 -1
- package/foundation/doc-compiler/stored-docs.d.mts +22 -0
- package/foundation/doc-compiler/stored-docs.mjs +49 -0
- package/foundation/fs/cache-eviction.d.mts +21 -0
- package/foundation/fs/cache-eviction.mjs +73 -0
- package/foundation/fs/gather-cache.d.mts +25 -4
- package/foundation/fs/gather-cache.mjs +138 -52
- package/foundation/fs/jiti-cache.d.mts +19 -1
- package/foundation/fs/jiti-cache.mjs +130 -6
- package/foundation/fs/kept-tree.d.mts +60 -0
- package/foundation/fs/kept-tree.mjs +154 -0
- package/foundation/fs/module-loader.d.mts +18 -2
- package/foundation/fs/module-loader.mjs +92 -20
- package/foundation/identity/provider-identity.d.mts +6 -0
- package/foundation/identity/provider-identity.mjs +10 -1
- package/foundation/render/file-header.d.mts +66 -0
- package/foundation/render/file-header.mjs +118 -0
- package/foundation/render/host-answer.d.mts +18 -0
- package/foundation/render/host-answer.mjs +46 -0
- package/foundation/render/host-exit.d.mts +1 -0
- package/foundation/render/host-exit.mjs +6 -1
- package/foundation/render/html-reader.d.mts +55 -2
- package/foundation/render/html-reader.mjs +145 -26
- package/foundation/render/markup-edits.d.mts +57 -0
- package/foundation/render/markup-edits.mjs +182 -0
- package/foundation/render/page-host.mjs +29 -12
- package/foundation/render/page-worker.mjs +79 -10
- package/foundation/render/render-page.d.mts +34 -2
- package/foundation/render/render-page.mjs +210 -29
- package/foundation/render/source-block.d.mts +73 -6
- package/foundation/render/source-block.mjs +170 -19
- package/foundation/render/ts-paths.d.mts +4 -1
- package/foundation/render/ts-paths.mjs +7 -3
- package/foundation/response/error-codes.d.mts +2 -1
- package/foundation/response/error-codes.doc.mjs +7 -1
- package/foundation/response/error-codes.mjs +7 -0
- package/foundation/text/string-utils.d.mts +3 -2
- package/foundation/text/string-utils.mjs +16 -3
- package/foundation/text/suggest-order.d.mts +9 -0
- package/foundation/text/suggest-order.mjs +13 -0
- package/package.json +6 -6
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {shellArg} from '../text/shell-arg.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file The plain-text notes a rendered file carries for whoever opens it
|
|
7
|
+
* next, a person or an agent with no other context: what made the file, what
|
|
8
|
+
* it carries, and the exact commands that give the page back and render it
|
|
9
|
+
* again. The note at the top sits in <head> after the charset and generator
|
|
10
|
+
* metas, so browsers and older CLIs still find both in the first bytes. The
|
|
11
|
+
* short note at the end follows the embedded source, for a reader that starts
|
|
12
|
+
* at the end of the file.
|
|
13
|
+
*
|
|
14
|
+
* The notes never spell the opening tag of the embedded block: every CLI
|
|
15
|
+
* finds the block by that tag, and the last match wins.
|
|
16
|
+
*
|
|
17
|
+
* @position foundation/render — used by render-page.mjs
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The command that runs one version of the CLI anywhere npm runs.
|
|
22
|
+
* @param {string | null} version
|
|
23
|
+
*/
|
|
24
|
+
export const cliCommand = version => `npx @astryxdesign/cli${version ? `@${version}` : ''}`;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Text that cannot end or nest an HTML comment.
|
|
29
|
+
* @param {string} text
|
|
30
|
+
*/
|
|
31
|
+
const commentSafe = text => text.replace(/--!?>/g, '- ->').replace(/<!--/g, '<!- -');
|
|
32
|
+
|
|
33
|
+
/** The most file names the note at the top lists. */
|
|
34
|
+
const MAX_LISTED = 24;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {object} FileFacts
|
|
38
|
+
* @property {string | null} cli - The CLI version that rendered the file.
|
|
39
|
+
* @property {number} format - The render format of the embedded source.
|
|
40
|
+
* @property {string} file - The rendered file's name.
|
|
41
|
+
* @property {string} page - The page's path from its project's root.
|
|
42
|
+
* @property {string} restored - Where --reverse writes the page by default, from the rendered file's folder.
|
|
43
|
+
* @property {string[]} files - The embedded files, page first.
|
|
44
|
+
* @property {Record<string, string>} packages - The packages the page rendered with, by name: their versions.
|
|
45
|
+
* @property {string[]} install - What a render outside a project must install first, as name@version.
|
|
46
|
+
* @property {string} [title] - The title given with --title.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The note at the top of a rendered file.
|
|
51
|
+
* @param {FileFacts} facts
|
|
52
|
+
* @returns {string} an HTML comment on lines of its own
|
|
53
|
+
*/
|
|
54
|
+
export function headerNote(facts) {
|
|
55
|
+
const run = cliCommand(facts.cli);
|
|
56
|
+
const file = shellArg(facts.file);
|
|
57
|
+
const shown = facts.files.slice(0, MAX_LISTED).join(', ');
|
|
58
|
+
const more = facts.files.length > MAX_LISTED ? `, and ${facts.files.length - MAX_LISTED} more` : '';
|
|
59
|
+
const versions = Object.entries(facts.packages).map(([name, version]) => `${name} ${version}`);
|
|
60
|
+
const core = facts.packages['@astryxdesign/core'];
|
|
61
|
+
// A title with < or > stays out of the note, which other tools read as
|
|
62
|
+
// HTML; step 1 prints the command with it.
|
|
63
|
+
const quotable = facts.title !== undefined && !/[<>]/.test(facts.title);
|
|
64
|
+
const title = quotable ? ` --title ${shellArg(/** @type {string} */ (facts.title))}` : '';
|
|
65
|
+
const lines = [
|
|
66
|
+
`Rendered by Astryx: astryx experimental render, @astryxdesign/cli ${facts.cli ?? '(version unknown)'}, render format ${facts.format}.`,
|
|
67
|
+
'This is one static file. It runs no script and loads nothing from the network.',
|
|
68
|
+
'It also carries the source of the page, so the page can come back exactly.',
|
|
69
|
+
'',
|
|
70
|
+
`Page: ${facts.page}`,
|
|
71
|
+
`Carried files (${facts.files.length}): ${shown}${more}`,
|
|
72
|
+
...(versions.length > 0 ? [`Rendered with: ${versions.join(', ')}`] : []),
|
|
73
|
+
'',
|
|
74
|
+
'To change the page, change its source and render it again. Do not edit this HTML:',
|
|
75
|
+
'the carried source does not get the edits you make here.',
|
|
76
|
+
'Run these commands in the folder that holds this file:',
|
|
77
|
+
` 1. Get the source back. This writes ${facts.restored} and the files it imports`,
|
|
78
|
+
` (into ${facts.restored.split('/')[0]}-2/ and so on when that folder exists; the output names it):`,
|
|
79
|
+
` ${run} experimental render --reverse ${file}`,
|
|
80
|
+
' 2. Edit the restored files.',
|
|
81
|
+
' 3. Render again. A render replaces a file that a render wrote. Step 1 prints this',
|
|
82
|
+
' command with the folder it wrote:',
|
|
83
|
+
` ${run} experimental render ${shellArg(facts.restored)} --out ${file}${title}`,
|
|
84
|
+
...(facts.title !== undefined && !quotable ? [' The page was rendered with --title; step 1 prints the command with it.'] : []),
|
|
85
|
+
`If you have the project, edit ${facts.page} there instead, and render it in the same way.`,
|
|
86
|
+
"Inside a project, the project's own CLI answers these commands. A project CLI older than",
|
|
87
|
+
`${facts.cli ?? 'the one that rendered this file'} writes the page without this note; to keep the note, update that CLI first:`,
|
|
88
|
+
` npm install --save-dev @astryxdesign/cli@${facts.cli ?? 'latest'}`,
|
|
89
|
+
...(facts.install.length > 0
|
|
90
|
+
? [
|
|
91
|
+
'Outside a project, the CLI supplies Astryx, React and StyleX. Install the other packages first:',
|
|
92
|
+
` npm install ${facts.install.join(' ')}`,
|
|
93
|
+
]
|
|
94
|
+
: []),
|
|
95
|
+
...(core
|
|
96
|
+
? [
|
|
97
|
+
`The source was written for @astryxdesign/core ${core}. In a project on a newer Core, update the`,
|
|
98
|
+
"restored code from inside that project (the CLI hands this to the project's own CLI):",
|
|
99
|
+
` ${cliCommand(null)} upgrade --from ${core} --path ${shellArg(facts.restored.split('/')[0])} --apply`,
|
|
100
|
+
]
|
|
101
|
+
: []),
|
|
102
|
+
'Later CLI versions read this file too.',
|
|
103
|
+
'If someone edits this HTML after the render, step 1 still gives back the page as rendered,',
|
|
104
|
+
'and it lists the text that changed, so you can make the same change in the source.',
|
|
105
|
+
'The source is in the script block at the end of this file. Keep it: without it,',
|
|
106
|
+
'step 1 can rebuild only the text and the layout from the markup.',
|
|
107
|
+
];
|
|
108
|
+
return `<!--\n${lines.map(line => (line ? ` ${commentSafe(line)}` : '')).join('\n')}\n-->`.replace(/<(?=\/?script)/gi, '<');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The note after the embedded source.
|
|
113
|
+
* @param {Pick<FileFacts, 'cli' | 'format' | 'file'>} facts
|
|
114
|
+
* @returns {string} an HTML comment on one line
|
|
115
|
+
*/
|
|
116
|
+
export function endNote(facts) {
|
|
117
|
+
return `<!-- Astryx render: the block above is the source of the page (render format ${facts.format}), as JSON; each file is base64 with its SHA-256. Get it back: ${commentSafe(cliCommand(facts.cli))} experimental render --reverse ${commentSafe(shellArg(facts.file))} -->`;
|
|
118
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
* @param {{
|
|
6
|
+
* send: (message: unknown, done: (error: Error | null) => void) => unknown,
|
|
7
|
+
* exit: (code: number) => void,
|
|
8
|
+
* connected: () => boolean,
|
|
9
|
+
* record?: (phase: 'no-channel' | 'sending' | 'sent' | 'send-failed') => void,
|
|
10
|
+
* }} io
|
|
11
|
+
* @returns {(message: unknown) => void}
|
|
12
|
+
*/
|
|
13
|
+
export function makeAnswer({ send, exit, connected, record }: {
|
|
14
|
+
send: (message: unknown, done: (error: Error | null) => void) => unknown;
|
|
15
|
+
exit: (code: number) => void;
|
|
16
|
+
connected: () => boolean;
|
|
17
|
+
record?: (phase: "no-channel" | "sending" | "sent" | "send-failed") => void;
|
|
18
|
+
}): (message: unknown) => void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file The render host's one answer to the CLI. The host exits 0 only after
|
|
5
|
+
* the channel accepted the answer; a send that fails exits with its own code
|
|
6
|
+
* (HOST_EXIT.sendFailed), so a lost answer never reads as a clean exit. Each
|
|
7
|
+
* phase is recorded, so the CLI can tell "sent but not received" from "ended
|
|
8
|
+
* before it answered".
|
|
9
|
+
* @input send, exit, connected and record functions (process ones in the host)
|
|
10
|
+
* @output an answer(message) function that sends at most once
|
|
11
|
+
* @position foundation/render — used by page-host.mjs
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {HOST_EXIT} from './host-exit.mjs';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {{
|
|
18
|
+
* send: (message: unknown, done: (error: Error | null) => void) => unknown,
|
|
19
|
+
* exit: (code: number) => void,
|
|
20
|
+
* connected: () => boolean,
|
|
21
|
+
* record?: (phase: 'no-channel' | 'sending' | 'sent' | 'send-failed') => void,
|
|
22
|
+
* }} io
|
|
23
|
+
* @returns {(message: unknown) => void}
|
|
24
|
+
*/
|
|
25
|
+
export function makeAnswer({send, exit, connected, record = () => {}}) {
|
|
26
|
+
let answered = false;
|
|
27
|
+
return message => {
|
|
28
|
+
if (answered) return;
|
|
29
|
+
answered = true;
|
|
30
|
+
if (!connected()) {
|
|
31
|
+
record('no-channel');
|
|
32
|
+
exit(HOST_EXIT.noChannel);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
record('sending');
|
|
36
|
+
try {
|
|
37
|
+
send(message, error => {
|
|
38
|
+
record(error ? 'send-failed' : 'sent');
|
|
39
|
+
exit(error ? HOST_EXIT.sendFailed : 0);
|
|
40
|
+
});
|
|
41
|
+
} catch {
|
|
42
|
+
record('send-failed');
|
|
43
|
+
exit(HOST_EXIT.sendFailed);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -8,4 +8,9 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
/** Exit codes for each way the host ends without sending an answer. */
|
|
11
|
-
export const HOST_EXIT = {
|
|
11
|
+
export const HOST_EXIT = {
|
|
12
|
+
disconnected: 70,
|
|
13
|
+
timeout: 71,
|
|
14
|
+
noChannel: 72,
|
|
15
|
+
sendFailed: 73,
|
|
16
|
+
};
|
|
@@ -1,6 +1,34 @@
|
|
|
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
|
+
* The tag whose name starts at `i`, read as a browser reads it: an attribute
|
|
6
|
+
* value is quoted or runs to a space or `>`, and a `/` just before `>` makes
|
|
7
|
+
* the tag self-closing.
|
|
8
|
+
* @param {string} html
|
|
9
|
+
* @param {number} i the first letter of the name
|
|
10
|
+
* @returns {{tag: string, attrs: Array<[string, string]>, selfClosing: boolean, end: number} | 'text' | 'open'}
|
|
11
|
+
* 'text' when the name is not a tag name, so the `<` is text; 'open' when
|
|
12
|
+
* no `>` ends the tag, so the rest of the document is text
|
|
13
|
+
*/
|
|
14
|
+
export function readTag(html: string, i: number): {
|
|
15
|
+
tag: string;
|
|
16
|
+
attrs: Array<[string, string]>;
|
|
17
|
+
selfClosing: boolean;
|
|
18
|
+
end: number;
|
|
19
|
+
} | "text" | "open";
|
|
20
|
+
/**
|
|
21
|
+
* Split an HTML document into its pieces, in order, as a browser does for
|
|
22
|
+
* the markup a render writes. A comment ends at the first `-->` after its
|
|
23
|
+
* `<!--` (so `<!-->` and `<!--->` are whole comments), or at the end. The
|
|
24
|
+
* content of a raw-text element up to its end tag is one `raw` piece, so a
|
|
25
|
+
* tag or a comment in a stylesheet is text. A `<` that starts no tag is
|
|
26
|
+
* text, and a tag that no `>` ends makes the rest text. Each character is
|
|
27
|
+
* read a fixed number of times, so the time grows with the document's length.
|
|
28
|
+
* @param {string} html
|
|
29
|
+
* @returns {Generator<HtmlToken>}
|
|
30
|
+
*/
|
|
31
|
+
export function scanHtml(html: string): Generator<HtmlToken>;
|
|
4
32
|
/**
|
|
5
33
|
* A small, forgiving HTML parser: enough for rendered markup.
|
|
6
34
|
* @param {string} html
|
|
@@ -12,14 +40,18 @@ export function find(n: ElementNode, tag: string): ElementNode | undefined;
|
|
|
12
40
|
/**
|
|
13
41
|
* @file A small, forgiving HTML reader for `astryx experimental render
|
|
14
42
|
* --reverse`: enough for rendered markup, plus the helpers that ask it which
|
|
15
|
-
* Astryx component an element came from.
|
|
43
|
+
* Astryx component an element came from. Its scanner also finds the parts of
|
|
44
|
+
* a rendered file that a reverse compares.
|
|
16
45
|
*
|
|
17
|
-
* @position foundation/render — used by rebuild.mjs
|
|
46
|
+
* @position foundation/render — used by rebuild.mjs, markup-edits.mjs,
|
|
47
|
+
* source-block.mjs and the render subject
|
|
18
48
|
*/
|
|
19
49
|
/** @typedef {{tag: string, attrs: Record<string, string>, children: Node[]}} ElementNode */
|
|
20
50
|
/** @typedef {ElementNode | {text: string}} Node */
|
|
21
51
|
export const VOID: Set<string>;
|
|
22
52
|
export function decode(s: string): string;
|
|
53
|
+
/** Elements whose content is raw text: a tag or a comment opener in it is text. */
|
|
54
|
+
export const RAW_TEXT: Set<string>;
|
|
23
55
|
export function isEl(n: Node): n is ElementNode;
|
|
24
56
|
/** Elements whose children React refuses or treats as a value. */
|
|
25
57
|
export const FORM_TAGS: Set<string>;
|
|
@@ -35,3 +67,24 @@ export type ElementNode = {
|
|
|
35
67
|
export type Node = ElementNode | {
|
|
36
68
|
text: string;
|
|
37
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* One piece of an HTML document, by its place in the text. `raw` is the
|
|
72
|
+
* content of a raw-text element.
|
|
73
|
+
*/
|
|
74
|
+
export type HtmlToken = {
|
|
75
|
+
kind: "text" | "raw" | "comment" | "doctype";
|
|
76
|
+
start: number;
|
|
77
|
+
end: number;
|
|
78
|
+
} | {
|
|
79
|
+
kind: "end";
|
|
80
|
+
tag: string;
|
|
81
|
+
start: number;
|
|
82
|
+
end: number;
|
|
83
|
+
} | {
|
|
84
|
+
kind: "start";
|
|
85
|
+
tag: string;
|
|
86
|
+
attrs: Array<[string, string]>;
|
|
87
|
+
selfClosing: boolean;
|
|
88
|
+
start: number;
|
|
89
|
+
end: number;
|
|
90
|
+
};
|
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* @file A small, forgiving HTML reader for `astryx experimental render
|
|
5
5
|
* --reverse`: enough for rendered markup, plus the helpers that ask it which
|
|
6
|
-
* Astryx component an element came from.
|
|
6
|
+
* Astryx component an element came from. Its scanner also finds the parts of
|
|
7
|
+
* a rendered file that a reverse compares.
|
|
7
8
|
*
|
|
8
|
-
* @position foundation/render — used by rebuild.mjs
|
|
9
|
+
* @position foundation/render — used by rebuild.mjs, markup-edits.mjs,
|
|
10
|
+
* source-block.mjs and the render subject
|
|
9
11
|
*/
|
|
10
12
|
|
|
11
13
|
/** @typedef {{tag: string, attrs: Record<string, string>, children: Node[]}} ElementNode */
|
|
@@ -27,6 +29,136 @@ export const decode = s =>
|
|
|
27
29
|
.replace(/ /g, '\u00a0')
|
|
28
30
|
.replace(/&/g, '&');
|
|
29
31
|
|
|
32
|
+
/** Elements whose content is raw text: a tag or a comment opener in it is text. */
|
|
33
|
+
export const RAW_TEXT = new Set(['script', 'style', 'textarea', 'title']);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* One piece of an HTML document, by its place in the text. `raw` is the
|
|
37
|
+
* content of a raw-text element.
|
|
38
|
+
* @typedef {{kind: 'text' | 'raw' | 'comment' | 'doctype', start: number, end: number}
|
|
39
|
+
* | {kind: 'end', tag: string, start: number, end: number}
|
|
40
|
+
* | {kind: 'start', tag: string, attrs: Array<[string, string]>, selfClosing: boolean, start: number, end: number}} HtmlToken
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const SPACE = /[\t\n\f\r ]/;
|
|
44
|
+
const LETTER = /[a-zA-Z]/;
|
|
45
|
+
const NAME_CHAR = /[\w:-]/;
|
|
46
|
+
const DOCTYPE = /<!doctype/iy;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The tag whose name starts at `i`, read as a browser reads it: an attribute
|
|
50
|
+
* value is quoted or runs to a space or `>`, and a `/` just before `>` makes
|
|
51
|
+
* the tag self-closing.
|
|
52
|
+
* @param {string} html
|
|
53
|
+
* @param {number} i the first letter of the name
|
|
54
|
+
* @returns {{tag: string, attrs: Array<[string, string]>, selfClosing: boolean, end: number} | 'text' | 'open'}
|
|
55
|
+
* 'text' when the name is not a tag name, so the `<` is text; 'open' when
|
|
56
|
+
* no `>` ends the tag, so the rest of the document is text
|
|
57
|
+
*/
|
|
58
|
+
export function readTag(html, i) {
|
|
59
|
+
const n = html.length;
|
|
60
|
+
const nameStart = i;
|
|
61
|
+
while (i < n && NAME_CHAR.test(html[i])) i++;
|
|
62
|
+
if (i >= n || !(SPACE.test(html[i]) || html[i] === '/' || html[i] === '>')) return 'text';
|
|
63
|
+
const tag = html.slice(nameStart, i).toLowerCase();
|
|
64
|
+
/** @type {Array<[string, string]>} */
|
|
65
|
+
const attrs = [];
|
|
66
|
+
let selfClosing = false;
|
|
67
|
+
while (i < n && html[i] !== '>') {
|
|
68
|
+
if (SPACE.test(html[i])) {
|
|
69
|
+
i++;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (html[i] === '/') {
|
|
73
|
+
selfClosing = html[i + 1] === '>';
|
|
74
|
+
i++;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const nameAt = i++;
|
|
78
|
+
while (i < n && !SPACE.test(html[i]) && html[i] !== '/' && html[i] !== '>' && html[i] !== '=') i++;
|
|
79
|
+
const name = html.slice(nameAt, i).toLowerCase();
|
|
80
|
+
let j = i;
|
|
81
|
+
while (j < n && SPACE.test(html[j])) j++;
|
|
82
|
+
let value = '';
|
|
83
|
+
if (html[j] === '=') {
|
|
84
|
+
j++;
|
|
85
|
+
while (j < n && SPACE.test(html[j])) j++;
|
|
86
|
+
const quote = html[j];
|
|
87
|
+
if (quote === '"' || quote === "'") {
|
|
88
|
+
const close = html.indexOf(quote, j + 1);
|
|
89
|
+
if (close < 0) return 'open';
|
|
90
|
+
value = html.slice(j + 1, close);
|
|
91
|
+
i = close + 1;
|
|
92
|
+
} else {
|
|
93
|
+
const from = j;
|
|
94
|
+
while (j < n && !SPACE.test(html[j]) && html[j] !== '>') j++;
|
|
95
|
+
value = html.slice(from, j);
|
|
96
|
+
i = j;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
attrs.push([name, value]);
|
|
100
|
+
}
|
|
101
|
+
return i >= n ? 'open' : {tag, attrs, selfClosing, end: i + 1};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Split an HTML document into its pieces, in order, as a browser does for
|
|
106
|
+
* the markup a render writes. A comment ends at the first `-->` after its
|
|
107
|
+
* `<!--` (so `<!-->` and `<!--->` are whole comments), or at the end. The
|
|
108
|
+
* content of a raw-text element up to its end tag is one `raw` piece, so a
|
|
109
|
+
* tag or a comment in a stylesheet is text. A `<` that starts no tag is
|
|
110
|
+
* text, and a tag that no `>` ends makes the rest text. Each character is
|
|
111
|
+
* read a fixed number of times, so the time grows with the document's length.
|
|
112
|
+
* @param {string} html
|
|
113
|
+
* @returns {Generator<HtmlToken>}
|
|
114
|
+
*/
|
|
115
|
+
export function* scanHtml(html) {
|
|
116
|
+
const n = html.length;
|
|
117
|
+
let at = 0;
|
|
118
|
+
let text = 0;
|
|
119
|
+
while (at < n) {
|
|
120
|
+
const lt = html.indexOf('<', at);
|
|
121
|
+
if (lt < 0) break;
|
|
122
|
+
const next = html[lt + 1] ?? '';
|
|
123
|
+
/** @type {HtmlToken | null} */
|
|
124
|
+
let token = null;
|
|
125
|
+
DOCTYPE.lastIndex = lt;
|
|
126
|
+
if (html.startsWith('<!--', lt)) {
|
|
127
|
+
const close = html.indexOf('-->', lt + 2);
|
|
128
|
+
token = {kind: 'comment', start: lt, end: close < 0 ? n : close + 3};
|
|
129
|
+
} else if (DOCTYPE.test(html)) {
|
|
130
|
+
const close = html.indexOf('>', lt);
|
|
131
|
+
if (close < 0) break;
|
|
132
|
+
token = {kind: 'doctype', start: lt, end: close + 1};
|
|
133
|
+
} else if (LETTER.test(next) || (next === '/' && LETTER.test(html[lt + 2] ?? ''))) {
|
|
134
|
+
const closing = next === '/';
|
|
135
|
+
const read = readTag(html, lt + (closing ? 2 : 1));
|
|
136
|
+
if (read === 'open') break;
|
|
137
|
+
if (read !== 'text') {
|
|
138
|
+
token = closing
|
|
139
|
+
? {kind: 'end', tag: read.tag, start: lt, end: read.end}
|
|
140
|
+
: {kind: 'start', tag: read.tag, attrs: read.attrs, selfClosing: read.selfClosing, start: lt, end: read.end};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (!token) {
|
|
144
|
+
at = lt + 1;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (lt > text) yield {kind: 'text', start: text, end: lt};
|
|
148
|
+
yield token;
|
|
149
|
+
at = text = token.end;
|
|
150
|
+
// A self-closing raw-text element (an SVG `<title />`) holds nothing.
|
|
151
|
+
if (token.kind === 'start' && RAW_TEXT.has(token.tag) && !token.selfClosing) {
|
|
152
|
+
const closing = new RegExp(`</${token.tag}`, 'gi');
|
|
153
|
+
closing.lastIndex = at;
|
|
154
|
+
const stop = closing.exec(html)?.index ?? n;
|
|
155
|
+
yield {kind: 'raw', start: at, end: stop};
|
|
156
|
+
at = text = stop;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (n > text) yield {kind: 'text', start: text, end: n};
|
|
160
|
+
}
|
|
161
|
+
|
|
30
162
|
/**
|
|
31
163
|
* A small, forgiving HTML parser: enough for rendered markup.
|
|
32
164
|
* @param {string} html
|
|
@@ -37,36 +169,23 @@ export function parseHtml(html) {
|
|
|
37
169
|
const root = {tag: '#root', attrs: {}, children: []};
|
|
38
170
|
/** @type {ElementNode[]} */
|
|
39
171
|
const stack = [root];
|
|
40
|
-
const
|
|
41
|
-
/** @type {RegExpExecArray | null} */
|
|
42
|
-
let m;
|
|
43
|
-
while ((m = re.exec(html))) {
|
|
172
|
+
for (const token of scanHtml(html)) {
|
|
44
173
|
const top = stack[stack.length - 1];
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
174
|
+
if (token.kind === 'text') {
|
|
175
|
+
top.children.push({text: decode(html.slice(token.start, token.end))});
|
|
176
|
+
} else if (token.kind === 'raw') {
|
|
177
|
+
top.children.push({text: html.slice(token.start, token.end)});
|
|
178
|
+
} else if (token.kind === 'end') {
|
|
179
|
+
const at = stack.map(n => n.tag).lastIndexOf(token.tag);
|
|
48
180
|
if (at > 0) stack.length = at;
|
|
49
|
-
} else if (
|
|
50
|
-
const tag = m[2].toLowerCase();
|
|
181
|
+
} else if (token.kind === 'start') {
|
|
51
182
|
/** @type {Record<string, string>} */
|
|
52
183
|
const attrs = {};
|
|
53
|
-
for (const
|
|
54
|
-
attrs[a[1].toLowerCase()] = decode(a[2] ?? a[3] ?? a[4] ?? '');
|
|
55
|
-
}
|
|
184
|
+
for (const [name, value] of token.attrs) attrs[name] = decode(value);
|
|
56
185
|
/** @type {ElementNode} */
|
|
57
|
-
const node = {tag, attrs, children: []};
|
|
186
|
+
const node = {tag: token.tag, attrs, children: []};
|
|
58
187
|
top.children.push(node);
|
|
59
|
-
if (
|
|
60
|
-
const end = html.toLowerCase().indexOf(`</${tag}`, re.lastIndex);
|
|
61
|
-
const stop = end < 0 ? html.length : end;
|
|
62
|
-
node.children.push({text: html.slice(re.lastIndex, stop)});
|
|
63
|
-
re.lastIndex = stop;
|
|
64
|
-
stack.push(node);
|
|
65
|
-
} else if (!VOID.has(tag) && !m[4]) {
|
|
66
|
-
stack.push(node);
|
|
67
|
-
}
|
|
68
|
-
} else if (m[5]) {
|
|
69
|
-
top.children.push({text: decode(m[5])});
|
|
188
|
+
if (!token.selfClosing && (RAW_TEXT.has(token.tag) || !VOID.has(token.tag))) stack.push(node);
|
|
70
189
|
}
|
|
71
190
|
}
|
|
72
191
|
return root;
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
* The text of rendered markup, as the runs a reader sees: each text node with
|
|
6
|
+
* its white space collapsed, empty ones left out, scripts and styles skipped.
|
|
7
|
+
* @param {string} markup
|
|
8
|
+
* @returns {string[]}
|
|
9
|
+
*/
|
|
10
|
+
export function textRuns(markup: string): string[];
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} TextEdit
|
|
13
|
+
* @property {string | null} was - The text as rendered; null for text added after the render.
|
|
14
|
+
* @property {string | null} now - The text the file holds now; null for text removed.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* The runs of text that differ between the markup as rendered and the markup
|
|
18
|
+
* now, paired where one replaced another, whole (a caller that shows them
|
|
19
|
+
* cuts them with {@link clip}).
|
|
20
|
+
* @param {string[]} before
|
|
21
|
+
* @param {string[]} after
|
|
22
|
+
* @returns {{edits: TextEdit[], more: number}} at most MAX_EDITS changes, and how many more there are
|
|
23
|
+
*/
|
|
24
|
+
export function textEdits(before: string[], after: string[]): {
|
|
25
|
+
edits: TextEdit[];
|
|
26
|
+
more: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Where a run of text sits in the restored source: the file and line where
|
|
30
|
+
* it starts, when it appears exactly once across the files. A run is text
|
|
31
|
+
* with its white space collapsed, and in source the same text may wrap
|
|
32
|
+
* across lines (JSX joins them with one space), so the run is found in each
|
|
33
|
+
* module's text collapsed the same way.
|
|
34
|
+
* @param {string} text a whole run of text
|
|
35
|
+
* @param {{file: string, text: string}[]} files the restored modules
|
|
36
|
+
* @returns {{file: string, line: number} | null}
|
|
37
|
+
*/
|
|
38
|
+
export function locate(text: string, files: {
|
|
39
|
+
file: string;
|
|
40
|
+
text: string;
|
|
41
|
+
}[]): {
|
|
42
|
+
file: string;
|
|
43
|
+
line: number;
|
|
44
|
+
} | null;
|
|
45
|
+
/** The most changes a reverse lists. */
|
|
46
|
+
export const MAX_EDITS: 50;
|
|
47
|
+
export function clip(text: string): string;
|
|
48
|
+
export type TextEdit = {
|
|
49
|
+
/**
|
|
50
|
+
* - The text as rendered; null for text added after the render.
|
|
51
|
+
*/
|
|
52
|
+
was: string | null;
|
|
53
|
+
/**
|
|
54
|
+
* - The text the file holds now; null for text removed.
|
|
55
|
+
*/
|
|
56
|
+
now: string | null;
|
|
57
|
+
};
|