@astryxdesign/cli 0.7.4 → 0.7.5-canary.04434e7
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 +19 -19
- package/api/component/_adapter.mjs +16 -1
- package/api/component/component.test.mjs +31 -0
- package/api/discover/_adapter.d.mts +1 -1
- package/api/docs/_adapter.d.mts +5 -2
- package/api/docs/_adapter.mjs +24 -16
- package/api/docs/docs.test.mjs +18 -0
- package/api/docs/integrationDocs.test.mjs +2 -2
- package/api/experimental.d.mts +5 -0
- package/api/experimental.mjs +15 -0
- package/api/hook/list/list.d.mts +1 -1
- package/api/icon/_adapter.d.mts +3 -2
- package/api/icon/_adapter.mjs +16 -3
- package/api/icon/_authoring.d.mts +5 -1
- package/api/icon/_authoring.mjs +478 -63
- package/api/icon/_options.d.mts +11 -0
- package/api/icon/_options.mjs +39 -4
- package/api/icon/_records.d.mts +19 -0
- package/api/icon/_records.mjs +25 -0
- package/api/icon/add/add.d.mts +3 -3
- package/api/icon/add/add.mjs +227 -14
- package/api/icon/all-or-nothing.test.mjs +283 -0
- package/api/icon/detail/detail.mjs +2 -13
- package/api/icon/error-codes.test.mjs +14 -7
- package/api/icon/icon.d.mts +2 -2
- package/api/icon/icon.doc.mjs +30 -2
- package/api/icon/icon.mjs +57 -8
- package/api/icon/icon.test.mjs +133 -1
- package/api/icon/icon.type.d.mts +126 -1
- package/api/icon/icon.type.mjs +67 -3
- package/api/icon/iconAdd.doc.mjs +22 -6
- package/api/icon/iconMap.doc.mjs +24 -8
- package/api/icon/iconNewDefinition.doc.mjs +2 -2
- package/api/icon/iconNewSet.doc.mjs +30 -2
- package/api/icon/iconUse.doc.mjs +4 -1
- package/api/icon/map/map.d.mts +4 -4
- package/api/icon/map/map.mjs +134 -9
- package/api/icon/new-definition/new-definition.mjs +6 -2
- package/api/icon/new-set/new-set.mjs +2 -2
- package/api/icon/new-set/new-set.test.mjs +60 -10
- package/api/icon/option-pairs.test.mjs +368 -0
- package/api/icon/search/search.d.mts +10 -0
- package/api/icon/search/search.mjs +55 -14
- package/api/icon/use/use.mjs +2 -13
- package/api/integration/add-helpers.d.mts +15 -1
- package/api/integration/add-helpers.mjs +1 -1
- package/api/integration/pack-check.lifecycle-output.test.mjs +24 -1
- package/api/integration/pack-check.mjs +10 -4
- package/api/integration/pack-check.test.mjs +5 -1
- package/api/integration/validate-integration.type.d.mts +3 -2
- package/api/integration/validate-integration.type.mjs +3 -2
- package/api/render/_adapter.d.mts +28 -0
- package/api/render/_adapter.mjs +540 -0
- package/api/render/render.d.mts +15 -0
- package/api/render/render.doc.d.mts +9 -0
- package/api/render/render.doc.mjs +112 -0
- package/api/render/render.mjs +113 -0
- package/api/render/render.test.mjs +1155 -0
- package/api/render/render.type.d.mts +74 -0
- package/api/render/render.type.mjs +64 -0
- package/api/search/_adapter.test.mjs +21 -2
- package/api/search/_result.d.mts +5 -5
- package/api/search/search.d.mts +1 -1
- package/api/template/template.mjs +3 -1
- package/api/template/template.type.d.mts +2 -1
- package/api/template/template.type.mjs +1 -1
- package/api/template/unknown-name.test.mjs +63 -0
- package/api/upgrade/run/failure-receipt.test.mjs +155 -0
- package/api/upgrade/run/run.mjs +170 -8
- package/api/upgrade/upgrade.doc.mjs +4 -4
- package/api/upgrade/upgrade.mjs +19 -4
- package/api/upgrade/upgrade.type.d.mts +12 -1
- package/api/upgrade/upgrade.type.mjs +16 -3
- package/assets/codemods/__tests__/runner.symlinks.test.mjs +205 -0
- package/assets/codemods/integration-runner.mjs +1 -1
- package/assets/codemods/run-codemod.mjs +17 -3
- package/assets/codemods/runner.mjs +81 -8
- package/assets/codemods/transform-prop.mjs +99 -49
- package/assets/codemods/transform-prop.test.mjs +36 -1
- package/assets/codemods/transforms/v0.7.0/__tests__/migrate-deprecated-theme-surface.test.mjs +207 -6
- package/assets/codemods/transforms/v0.7.0/__tests__/validate-data-token-ownership.test.mjs +123 -70
- package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface-v0.6-data.mjs +6 -1
- package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +200 -32
- package/assets/codemods/transforms/v0.7.0/validate-data-token-ownership.mjs +19 -77
- package/assets/discover/official-packages.json +4 -0
- package/assets/docs/tree/icon-definitions.doc.mjs +2 -2
- package/assets/docs/tree/icon-set-files.doc.mjs +1 -1
- package/assets/docs/tree/icon-sets.doc.mjs +50 -20
- package/assets/docs/tree/render-pages.doc.mjs +94 -0
- package/assets/docs/tree/styling-advanced.doc.mjs +1 -1
- package/assets/icons/definitions/activity.doc.mjs +1 -1
- package/assets/icons/definitions/alignCenter.doc.mjs +9 -0
- package/assets/icons/definitions/alignEnd.doc.mjs +10 -0
- package/assets/icons/definitions/alignStart.doc.mjs +10 -0
- package/assets/icons/definitions/archive.doc.mjs +1 -1
- package/assets/icons/definitions/arrowsUpDown.doc.mjs +1 -1
- package/assets/icons/definitions/attach.doc.mjs +1 -1
- package/assets/icons/definitions/audio.doc.mjs +1 -1
- package/assets/icons/definitions/cart.doc.mjs +1 -1
- package/assets/icons/definitions/checkDouble.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsLeft.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsRight.doc.mjs +1 -1
- package/assets/icons/definitions/close.doc.mjs +1 -1
- package/assets/icons/definitions/collapse.doc.mjs +1 -1
- package/assets/icons/definitions/compose.doc.mjs +1 -1
- package/assets/icons/definitions/conversation.doc.mjs +1 -1
- package/assets/icons/definitions/copy.doc.mjs +1 -1
- package/assets/icons/definitions/cpu.doc.mjs +9 -0
- package/assets/icons/definitions/database.doc.mjs +1 -1
- package/assets/icons/definitions/desktop.doc.mjs +1 -1
- package/assets/icons/definitions/discount.doc.mjs +1 -1
- package/assets/icons/definitions/download.doc.mjs +1 -1
- package/assets/icons/definitions/emoji.doc.mjs +1 -1
- package/assets/icons/definitions/error.doc.mjs +1 -1
- package/assets/icons/definitions/expand.doc.mjs +1 -1
- package/assets/icons/definitions/experiment.doc.mjs +1 -1
- package/assets/icons/definitions/externalLink.doc.mjs +7 -1
- package/assets/icons/definitions/eyeSlash.doc.mjs +1 -1
- package/assets/icons/definitions/flipHorizontal.doc.mjs +9 -0
- package/assets/icons/definitions/flipVertical.doc.mjs +9 -0
- package/assets/icons/definitions/folderAdd.doc.mjs +1 -1
- package/assets/icons/definitions/gitBranch.doc.mjs +9 -0
- package/assets/icons/definitions/gitFork.doc.mjs +9 -0
- package/assets/icons/definitions/info.doc.mjs +1 -1
- package/assets/icons/definitions/location.doc.mjs +1 -1
- package/assets/icons/definitions/mention.doc.mjs +1 -1
- package/assets/icons/definitions/menu.doc.mjs +1 -1
- package/assets/icons/definitions/microphone.doc.mjs +1 -1
- package/assets/icons/definitions/mobile.doc.mjs +1 -1
- package/assets/icons/definitions/money.doc.mjs +1 -1
- package/assets/icons/definitions/moreHorizontal.doc.mjs +1 -1
- package/assets/icons/definitions/notificationActive.doc.mjs +1 -1
- package/assets/icons/definitions/notificationOff.doc.mjs +1 -1
- package/assets/icons/definitions/panelLeft.doc.mjs +10 -0
- package/assets/icons/definitions/panelRight.doc.mjs +10 -0
- package/assets/icons/definitions/raiseHand.doc.mjs +1 -1
- package/assets/icons/definitions/redo.doc.mjs +1 -1
- package/assets/icons/definitions/refresh.doc.mjs +1 -1
- package/assets/icons/definitions/rotate.doc.mjs +9 -0
- package/assets/icons/definitions/scale.doc.mjs +10 -0
- package/assets/icons/definitions/search.doc.mjs +1 -1
- package/assets/icons/definitions/send.doc.mjs +1 -1
- package/assets/icons/definitions/settings.doc.mjs +1 -1
- package/assets/icons/definitions/star.doc.mjs +1 -1
- package/assets/icons/definitions/stop.doc.mjs +1 -1
- package/assets/icons/definitions/success.doc.mjs +1 -1
- package/assets/icons/definitions/textCase.doc.mjs +9 -0
- package/assets/icons/definitions/thumbsDown.doc.mjs +1 -1
- package/assets/icons/definitions/thumbsUp.doc.mjs +1 -1
- package/assets/icons/definitions/trendDown.doc.mjs +7 -1
- package/assets/icons/definitions/trendUp.doc.mjs +1 -1
- package/assets/icons/definitions/undo.doc.mjs +1 -1
- package/assets/icons/definitions/upload.doc.mjs +1 -1
- package/assets/icons/definitions/userAdd.doc.mjs +1 -1
- package/assets/icons/definitions/warning.doc.mjs +1 -1
- package/assets/templates/blocks/components/Tokenizer/TokenizerClear.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerCreatable.tsx +30 -30
- package/assets/templates/blocks/components/Tokenizer/TokenizerEndContent.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerIcon.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerMaxEntries.tsx +17 -16
- package/assets/templates/blocks/components/Tokenizer/TokenizerOverflow.tsx +29 -29
- package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +9 -8
- package/assets/templates/blocks/components/Tokenizer/TokenizerStates.tsx +39 -41
- package/assets/templates/pages/canvas-editor/template.doc.mjs +9 -0
- package/assets/templates/pages/form-wizard-inline/template.doc.mjs +6 -1
- package/assets/templates/pages/table-comparison/template.doc.mjs +1 -0
- package/assets/templates/pages/table-tree/template.doc.mjs +3 -0
- package/authoring/debug/parse.d.mts +2 -2
- package/authoring/doctypes/icon-definition/icon-definition.doc.mjs +4 -4
- package/authoring/doctypes/icon-set/icon-set-data.doc.mjs +6 -0
- package/authoring/doctypes/icon-set/parse.d.mts +1 -0
- package/authoring/doctypes/icon-set/parse.mjs +1 -0
- package/authoring/doctypes/icon-set/type.ts +5 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +2 -1
- package/clients/cli/commands/doctor.mjs +16 -17
- package/clients/cli/commands/experimental/icon.mjs +167 -35
- package/clients/cli/commands/experimental/index.mjs +2 -0
- package/clients/cli/commands/experimental/render.mjs +113 -0
- package/clients/cli/commands/experimental-icon-add.doc.mjs +14 -4
- package/clients/cli/commands/experimental-icon-map.doc.mjs +12 -2
- package/clients/cli/commands/experimental-icon-new-definition.doc.mjs +1 -1
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +28 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +18 -2
- package/clients/cli/commands/experimental-render.doc.mjs +60 -0
- package/clients/cli/commands/experimental.doc.mjs +4 -2
- package/clients/cli/commands/icon-authoring.test.mjs +1823 -13
- package/clients/cli/commands/icon-options-wiring.test.mjs +158 -0
- package/clients/cli/commands/icon.test.mjs +1 -1
- package/clients/cli/commands/template.mjs +4 -1
- package/clients/cli/commands/text-json-parity.test.mjs +46 -0
- package/clients/cli/commands/upgrade.doc.mjs +17 -0
- package/clients/cli/commands/upgrade.file-protection.test.mjs +29 -0
- package/clients/cli/commands/upgrade.flag-help.test.mjs +37 -0
- package/clients/cli/commands/upgrade.integration-policy.test.mjs +103 -0
- package/clients/cli/commands/upgrade.mjs +9 -2
- package/clients/cli/index.mjs +1 -0
- package/foundation/discovery/component-discovery.d.mts +2 -1
- package/foundation/discovery/component-discovery.export-names.test.mjs +41 -0
- package/foundation/discovery/component-discovery.mjs +13 -7
- package/foundation/discovery/icon-set-checks.d.mts +43 -9
- package/foundation/discovery/icon-set-checks.mjs +296 -21
- package/foundation/discovery/icon-set-checks.test.mjs +125 -12
- package/foundation/discovery/icon-set-writer.d.mts +192 -3
- package/foundation/discovery/icon-set-writer.mjs +608 -34
- package/foundation/discovery/icon-set-writer.test.mjs +26 -0
- package/foundation/discovery/module-exports.mjs +82 -25
- package/foundation/discovery/module-exports.test.mjs +38 -1
- package/foundation/discovery/package-exports.d.mts +13 -0
- package/foundation/discovery/package-exports.mjs +40 -1
- package/foundation/discovery/template-icons.d.mts +6 -0
- package/foundation/discovery/template-icons.mjs +98 -10
- package/foundation/discovery/template-icons.test.mjs +103 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +20 -5
- package/foundation/doc-compiler/import.mjs +81 -8
- package/foundation/doc-compiler/read.d.mts +4 -3
- package/foundation/doc-compiler/read.mjs +15 -10
- package/foundation/doc-compiler/read.test.mjs +59 -1
- package/foundation/doc-compiler/tree.mjs +3 -1
- package/foundation/fs/module-loader.d.mts +3 -2
- package/foundation/fs/module-loader.mjs +3 -2
- package/foundation/fs/publish-file-eperm-mutation.test.mjs +2 -0
- package/foundation/fs/read-record.d.mts +27 -0
- package/foundation/fs/read-record.mjs +83 -15
- package/foundation/fs/read-record.test.mjs +88 -1
- package/foundation/render/assets.d.mts +31 -0
- package/foundation/render/assets.mjs +102 -0
- package/foundation/render/component-map.d.mts +52 -0
- package/foundation/render/component-map.mjs +141 -0
- package/foundation/render/css-cut.d.mts +29 -0
- package/foundation/render/css-cut.mjs +144 -0
- package/foundation/render/embedded-files.d.mts +28 -0
- package/foundation/render/embedded-files.mjs +108 -0
- package/foundation/render/embedded-imports.d.mts +61 -0
- package/foundation/render/embedded-imports.mjs +315 -0
- package/foundation/render/html-reader.d.mts +37 -0
- package/foundation/render/html-reader.mjs +121 -0
- package/foundation/render/page-worker.d.mts +7 -0
- package/foundation/render/page-worker.mjs +519 -0
- package/foundation/render/rebuild.d.mts +19 -0
- package/foundation/render/rebuild.mjs +269 -0
- package/foundation/render/render-page.d.mts +85 -0
- package/foundation/render/render-page.mjs +612 -0
- package/foundation/render/render.test.mjs +117 -0
- package/foundation/render/source-block.d.mts +52 -0
- package/foundation/render/source-block.mjs +106 -0
- package/foundation/response/base.d.ts +7 -0
- package/foundation/response/error-codes.d.mts +10 -1
- package/foundation/response/error-codes.doc.mjs +59 -2
- package/foundation/response/error-codes.mjs +62 -1
- package/foundation/response/json.d.mts +10 -3
- package/foundation/response/json.mjs +16 -4
- package/foundation/response/response-types.doc.mjs +38 -1
- package/foundation/text/string-utils.mjs +33 -0
- package/foundation/text/string-utils.test.mjs +17 -0
- package/foundation/text/suggest-order.d.mts +17 -3
- package/foundation/text/suggest-order.mjs +58 -13
- package/foundation/text/suggest-order.test.mjs +53 -0
- package/package.json +14 -9
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Local files as data: URIs for `astryx experimental render`, so the
|
|
5
|
+
* rendered page needs nothing beside it.
|
|
6
|
+
*
|
|
7
|
+
* @position foundation/render — used by page-worker.mjs and render-page.mjs
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import * as fs from 'node:fs';
|
|
11
|
+
import * as path from 'node:path';
|
|
12
|
+
|
|
13
|
+
/** The largest single file inlined into a rendered page. */
|
|
14
|
+
export const MAX_ASSET_BYTES = 8 * 1024 * 1024;
|
|
15
|
+
|
|
16
|
+
/** @type {Record<string, string>} */
|
|
17
|
+
const MIME = {
|
|
18
|
+
png: 'image/png',
|
|
19
|
+
jpg: 'image/jpeg',
|
|
20
|
+
jpeg: 'image/jpeg',
|
|
21
|
+
gif: 'image/gif',
|
|
22
|
+
webp: 'image/webp',
|
|
23
|
+
avif: 'image/avif',
|
|
24
|
+
svg: 'image/svg+xml',
|
|
25
|
+
ico: 'image/x-icon',
|
|
26
|
+
bmp: 'image/bmp',
|
|
27
|
+
woff: 'font/woff',
|
|
28
|
+
woff2: 'font/woff2',
|
|
29
|
+
ttf: 'font/ttf',
|
|
30
|
+
otf: 'font/otf',
|
|
31
|
+
mp4: 'video/mp4',
|
|
32
|
+
webm: 'video/webm',
|
|
33
|
+
mp3: 'audio/mpeg',
|
|
34
|
+
wav: 'audio/wav',
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** @param {string} file */
|
|
38
|
+
export const isAssetPath = file =>
|
|
39
|
+
Object.hasOwn(MIME, path.extname(file).slice(1).toLowerCase());
|
|
40
|
+
|
|
41
|
+
/** An error carrying a render error code. */
|
|
42
|
+
export class RenderAssetError extends Error {
|
|
43
|
+
/**
|
|
44
|
+
* @param {string} code
|
|
45
|
+
* @param {string} message
|
|
46
|
+
* @param {Record<string, unknown>} [detail]
|
|
47
|
+
*/
|
|
48
|
+
constructor(code, message, detail) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.code = code;
|
|
51
|
+
this.detail = detail;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Read a local file as a data: URI.
|
|
57
|
+
* @param {string} file absolute path
|
|
58
|
+
* @returns {string}
|
|
59
|
+
*/
|
|
60
|
+
export function assetDataUri(file) {
|
|
61
|
+
/** @type {fs.Stats} */
|
|
62
|
+
let stat;
|
|
63
|
+
try {
|
|
64
|
+
stat = fs.statSync(file);
|
|
65
|
+
} catch {
|
|
66
|
+
throw new RenderAssetError(
|
|
67
|
+
'ERR_FILE_NOT_FOUND',
|
|
68
|
+
`The page uses ${file}, which does not exist`,
|
|
69
|
+
{module: file},
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (stat.size > MAX_ASSET_BYTES) {
|
|
73
|
+
throw new RenderAssetError(
|
|
74
|
+
'ERR_RENDER_ASSET_TOO_LARGE',
|
|
75
|
+
`${path.basename(file)} is ${(stat.size / 1048576).toFixed(1)} MB; a rendered page inlines files up to ${MAX_ASSET_BYTES / 1048576} MB each. Use a smaller copy of it.`,
|
|
76
|
+
{file, bytes: stat.size, limit: MAX_ASSET_BYTES},
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const mime = MIME[path.extname(file).slice(1).toLowerCase()] ?? 'application/octet-stream';
|
|
80
|
+
return `data:${mime};base64,${fs.readFileSync(file).toString('base64')}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Whether bytes are a known image, font or media format, whatever the file's
|
|
85
|
+
* extension says (a JPEG saved as `.png` is still an inert image): their
|
|
86
|
+
* leading bytes match one (an SVG is XML with an <svg> element). A carried
|
|
87
|
+
* asset is carried, and written back, only when they do.
|
|
88
|
+
* @param {Buffer} bytes
|
|
89
|
+
*/
|
|
90
|
+
export function isAssetBytes(bytes) {
|
|
91
|
+
const at = (/** @type {number} */ i, /** @type {string} */ text) =>
|
|
92
|
+
bytes.subarray(i, i + text.length).toString('latin1') === text;
|
|
93
|
+
const b = (/** @type {number[]} */ ...xs) => xs.every((x, i) => bytes[i] === x);
|
|
94
|
+
if (at(0, '\x89PNG\r\n\x1a\n') || b(0xff, 0xd8, 0xff) || at(0, 'GIF87a') || at(0, 'GIF89a')) return true;
|
|
95
|
+
if (at(0, 'RIFF') && (at(8, 'WEBP') || at(8, 'WAVE'))) return true;
|
|
96
|
+
if (at(4, 'ftyp')) return true; // AVIF, MP4
|
|
97
|
+
if (at(0, 'BM') || b(0, 0, 1, 0)) return true; // BMP, ICO
|
|
98
|
+
if (at(0, 'wOFF') || at(0, 'wOF2') || at(0, 'OTTO') || at(0, 'true') || b(0, 1, 0, 0)) return true;
|
|
99
|
+
if (b(0x1a, 0x45, 0xdf, 0xa3) || at(0, 'ID3') || (bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0)) return true;
|
|
100
|
+
const head = bytes.subarray(0, 4096).toString('utf8').replace(/^\uFEFF/, '').trimStart();
|
|
101
|
+
return head.startsWith('<') && /<svg[\s>]/i.test(head);
|
|
102
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
* @typedef {object} ComponentEntry
|
|
6
|
+
* @property {string} name - Export name.
|
|
7
|
+
* @property {string} module - Import specifier.
|
|
8
|
+
* @property {Set<string>} props - Prop names its declaration lists.
|
|
9
|
+
* @property {Set<string>} numeric - Props whose type includes `number`.
|
|
10
|
+
* @property {Map<string, string>} required - Props its declaration requires, with their declared types.
|
|
11
|
+
* @property {boolean} children - Whether it takes children.
|
|
12
|
+
* @property {boolean} label - Whether it takes `label`.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Map each theme target (the `astryx-<name>` class) to the components that
|
|
16
|
+
* render it, by reading an installed package's built modules.
|
|
17
|
+
* @param {string} pkg e.g. `@astryxdesign/core`
|
|
18
|
+
* @param {string} fromDir where to resolve it from
|
|
19
|
+
* @returns {Map<string, ComponentEntry[]>}
|
|
20
|
+
*/
|
|
21
|
+
export function componentMap(pkg: string, fromDir: string): Map<string, ComponentEntry[]>;
|
|
22
|
+
export function pascal(s: string): string;
|
|
23
|
+
export type ComponentEntry = {
|
|
24
|
+
/**
|
|
25
|
+
* - Export name.
|
|
26
|
+
*/
|
|
27
|
+
name: string;
|
|
28
|
+
/**
|
|
29
|
+
* - Import specifier.
|
|
30
|
+
*/
|
|
31
|
+
module: string;
|
|
32
|
+
/**
|
|
33
|
+
* - Prop names its declaration lists.
|
|
34
|
+
*/
|
|
35
|
+
props: Set<string>;
|
|
36
|
+
/**
|
|
37
|
+
* - Props whose type includes `number`.
|
|
38
|
+
*/
|
|
39
|
+
numeric: Set<string>;
|
|
40
|
+
/**
|
|
41
|
+
* - Props its declaration requires, with their declared types.
|
|
42
|
+
*/
|
|
43
|
+
required: Map<string, string>;
|
|
44
|
+
/**
|
|
45
|
+
* - Whether it takes children.
|
|
46
|
+
*/
|
|
47
|
+
children: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* - Whether it takes `label`.
|
|
50
|
+
*/
|
|
51
|
+
label: boolean;
|
|
52
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Which installed component draws each Astryx theme target (the
|
|
5
|
+
* `astryx-<name>` class), read from a package's built modules and
|
|
6
|
+
* declarations, for `astryx experimental render --reverse`.
|
|
7
|
+
*
|
|
8
|
+
* @position foundation/render — used by rebuild.mjs
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import * as fs from 'node:fs';
|
|
12
|
+
import * as path from 'node:path';
|
|
13
|
+
import {createRequire} from 'node:module';
|
|
14
|
+
|
|
15
|
+
/** @param {string} s */
|
|
16
|
+
export const pascal = s => s.replace(/(^|-)([a-z0-9])/g, (_, __, c) => c.toUpperCase());
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {object} ComponentEntry
|
|
20
|
+
* @property {string} name - Export name.
|
|
21
|
+
* @property {string} module - Import specifier.
|
|
22
|
+
* @property {Set<string>} props - Prop names its declaration lists.
|
|
23
|
+
* @property {Set<string>} numeric - Props whose type includes `number`.
|
|
24
|
+
* @property {Map<string, string>} required - Props its declaration requires, with their declared types.
|
|
25
|
+
* @property {boolean} children - Whether it takes children.
|
|
26
|
+
* @property {boolean} label - Whether it takes `label`.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Map each theme target (the `astryx-<name>` class) to the components that
|
|
31
|
+
* render it, by reading an installed package's built modules.
|
|
32
|
+
* @param {string} pkg e.g. `@astryxdesign/core`
|
|
33
|
+
* @param {string} fromDir where to resolve it from
|
|
34
|
+
* @returns {Map<string, ComponentEntry[]>}
|
|
35
|
+
*/
|
|
36
|
+
export function componentMap(pkg, fromDir) {
|
|
37
|
+
/** @type {Map<string, ComponentEntry[]>} */
|
|
38
|
+
const map = new Map();
|
|
39
|
+
/** @type {string} */
|
|
40
|
+
let dist;
|
|
41
|
+
try {
|
|
42
|
+
const req = createRequire(path.join(fromDir, 'noop.js'));
|
|
43
|
+
let d = path.dirname(req.resolve(pkg));
|
|
44
|
+
while (!fs.existsSync(path.join(d, 'package.json')) && path.dirname(d) !== d) d = path.dirname(d);
|
|
45
|
+
dist = path.join(d, 'dist');
|
|
46
|
+
} catch {
|
|
47
|
+
return map;
|
|
48
|
+
}
|
|
49
|
+
if (!fs.existsSync(dist)) return map;
|
|
50
|
+
// Where each name can be imported from: the package's public subpaths.
|
|
51
|
+
/** @type {Map<string, string[]>} */
|
|
52
|
+
const exported = new Map();
|
|
53
|
+
try {
|
|
54
|
+
const pj = JSON.parse(fs.readFileSync(path.join(dist, '..', 'package.json'), 'utf8'));
|
|
55
|
+
for (const [key, value] of Object.entries(pj.exports ?? {})) {
|
|
56
|
+
const target = typeof value === 'string' ? value : /** @type {any} */ (value)?.default ?? /** @type {any} */ (value)?.import;
|
|
57
|
+
if (key === '.' || typeof target !== 'string' || !target.endsWith('.js')) continue;
|
|
58
|
+
const file = path.join(dist, '..', target);
|
|
59
|
+
if (!fs.existsSync(file)) continue;
|
|
60
|
+
const js = fs.readFileSync(file, 'utf8');
|
|
61
|
+
/** @type {string[]} */
|
|
62
|
+
const names = [];
|
|
63
|
+
for (const m of js.matchAll(/export\s*\{([^}]*)\}/g)) {
|
|
64
|
+
for (const part of m[1].split(',')) {
|
|
65
|
+
const name = part.trim().split(/\s+as\s+/).pop()?.trim();
|
|
66
|
+
if (name) names.push(name);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
for (const m of js.matchAll(/export\s+(?:function|const|class)\s+([A-Za-z0-9_]+)/g)) names.push(m[1]);
|
|
70
|
+
for (const name of names) {
|
|
71
|
+
const list = exported.get(name) ?? [];
|
|
72
|
+
list.push(`${pkg}/${key.slice(2)}`);
|
|
73
|
+
exported.set(name, list);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
// No readable exports map: fall back to folder names.
|
|
78
|
+
}
|
|
79
|
+
/** @param {string} name @param {string} folder */
|
|
80
|
+
const moduleFor = (name, folder) => {
|
|
81
|
+
const list = exported.get(name);
|
|
82
|
+
if (!list) return null;
|
|
83
|
+
return list.find(m => m.endsWith(`/${name}`)) ?? list.find(m => m.endsWith(`/${folder}`)) ?? list[0];
|
|
84
|
+
};
|
|
85
|
+
for (const dirent of fs.readdirSync(dist, {withFileTypes: true})) {
|
|
86
|
+
if (!dirent.isDirectory()) continue;
|
|
87
|
+
const folder = path.join(dist, dirent.name);
|
|
88
|
+
for (const file of fs.readdirSync(folder)) {
|
|
89
|
+
if (!file.endsWith('.js')) continue;
|
|
90
|
+
const js = fs.readFileSync(path.join(folder, file), 'utf8');
|
|
91
|
+
const targets = [...js.matchAll(/themeProps\(\s*['"]([a-z0-9-]+)['"]/g)].map(t => t[1]);
|
|
92
|
+
if (targets.length === 0) continue;
|
|
93
|
+
const names = [...js.matchAll(/^([A-Z][A-Za-z0-9]*)\.displayName = ['"]([A-Za-z0-9.]+)['"]/gm)].map(n => n[1]);
|
|
94
|
+
if (names.length === 0) continue;
|
|
95
|
+
const dts = path.join(folder, file.replace(/\.js$/, '.d.ts'));
|
|
96
|
+
const decl = fs.existsSync(dts) ? fs.readFileSync(dts, 'utf8') : '';
|
|
97
|
+
for (const target of new Set(targets)) {
|
|
98
|
+
for (const name of names) {
|
|
99
|
+
/** @type {Set<string>} */
|
|
100
|
+
const props = new Set();
|
|
101
|
+
/** @type {Set<string>} */
|
|
102
|
+
const numeric = new Set();
|
|
103
|
+
/** @type {Map<string, string>} */
|
|
104
|
+
const required = new Map();
|
|
105
|
+
const body = new RegExp(`interface ${name}Props[^{]*\\{([\\s\\S]*?)\\n\\}`).exec(decl)?.[1] ?? '';
|
|
106
|
+
for (const p of body.matchAll(/^\s{4}(?:readonly\s+)?([a-zA-Z][\w]*)\??:\s*([^;]*)/gm)) {
|
|
107
|
+
props.add(p[1]);
|
|
108
|
+
if (!/^\s{4}(?:readonly\s+)?[a-zA-Z]\w*\?/.test(p[0])) required.set(p[1], p[2].trim());
|
|
109
|
+
if (/\bnumber\b|\b\d+\b/.test(p[2])) numeric.add(p[1]);
|
|
110
|
+
}
|
|
111
|
+
const module = moduleFor(name, dirent.name);
|
|
112
|
+
if (!module) continue;
|
|
113
|
+
const entry = {
|
|
114
|
+
name,
|
|
115
|
+
module,
|
|
116
|
+
props,
|
|
117
|
+
numeric,
|
|
118
|
+
required,
|
|
119
|
+
children: props.has('children') || /extends[^{]*(PropsWithChildren|HTMLAttributes|ComponentProps)/.test(decl),
|
|
120
|
+
label: props.has('label'),
|
|
121
|
+
};
|
|
122
|
+
const list = map.get(target) ?? [];
|
|
123
|
+
if (!list.some(e => e.name === name)) list.push(entry);
|
|
124
|
+
// A stack with a fixed direction reads best as VStack / HStack.
|
|
125
|
+
if (target === 'stack' && name === 'Stack') {
|
|
126
|
+
for (const alias of ['VStack', 'HStack']) {
|
|
127
|
+
const aliasModule = moduleFor(alias, dirent.name);
|
|
128
|
+
if (!aliasModule || list.some(e => e.name === alias)) continue;
|
|
129
|
+
const aliasProps = new Set(props);
|
|
130
|
+
aliasProps.delete('direction');
|
|
131
|
+
list.push({...entry, name: alias, module: aliasModule, props: aliasProps});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
map.set(target, list);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return map;
|
|
140
|
+
}
|
|
141
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
* Every class name in a page's markup.
|
|
6
|
+
* @param {string} markup
|
|
7
|
+
* @returns {Set<string>}
|
|
8
|
+
*/
|
|
9
|
+
export function classesIn(markup: string): Set<string>;
|
|
10
|
+
/**
|
|
11
|
+
* The classes a selector needs, ignoring any inside `:not()`, `:is()`,
|
|
12
|
+
* `:where()` or `:has()`.
|
|
13
|
+
* @param {string} selector
|
|
14
|
+
* @returns {string[]}
|
|
15
|
+
*/
|
|
16
|
+
export function requiredClasses(selector: string): string[];
|
|
17
|
+
/**
|
|
18
|
+
* Cut a stylesheet to the rules the page uses.
|
|
19
|
+
* @param {string} css
|
|
20
|
+
* @param {Set<string>} used class names in the page
|
|
21
|
+
* @returns {string}
|
|
22
|
+
*/
|
|
23
|
+
export function cutCss(css: string, used: Set<string>): string;
|
|
24
|
+
/**
|
|
25
|
+
* The first `@layer a, b, c;` order statement in a stylesheet, if any.
|
|
26
|
+
* @param {string} css
|
|
27
|
+
* @returns {string}
|
|
28
|
+
*/
|
|
29
|
+
export function layerOrderOf(css: string): string;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Cut a stylesheet down to the rules a rendered page uses, for
|
|
5
|
+
* `astryx experimental render`. A rule stays when every class in one of its
|
|
6
|
+
* selectors appears in the page; rules without classes (`:root` tokens,
|
|
7
|
+
* element resets) always stay. `@property`,
|
|
8
|
+
* `@font-face` and the `@keyframes` a kept rule names stay too; `@layer`
|
|
9
|
+
* order statements are dropped for the caller to write once, first
|
|
10
|
+
* (see layerOrderOf).
|
|
11
|
+
*
|
|
12
|
+
* @position foundation/render — used by render-page.mjs
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import postcss from 'postcss';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Every class name in a page's markup.
|
|
19
|
+
* @param {string} markup
|
|
20
|
+
* @returns {Set<string>}
|
|
21
|
+
*/
|
|
22
|
+
export function classesIn(markup) {
|
|
23
|
+
/** @type {Set<string>} */
|
|
24
|
+
const used = new Set();
|
|
25
|
+
for (const m of markup.matchAll(/\sclass="([^"]*)"/g)) {
|
|
26
|
+
for (const c of m[1].split(/\s+/)) if (c) used.add(c);
|
|
27
|
+
}
|
|
28
|
+
return used;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Unescape a CSS identifier as written in a selector. @param {string} s */
|
|
32
|
+
const unescapeIdent = s =>
|
|
33
|
+
s
|
|
34
|
+
.replace(/\\([0-9a-fA-F]{1,6})\s?/g, (_, h) =>
|
|
35
|
+
String.fromCodePoint(parseInt(h, 16)),
|
|
36
|
+
)
|
|
37
|
+
.replace(/\\(.)/g, '$1');
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The classes a selector needs, ignoring any inside `:not()`, `:is()`,
|
|
41
|
+
* `:where()` or `:has()`.
|
|
42
|
+
* @param {string} selector
|
|
43
|
+
* @returns {string[]}
|
|
44
|
+
*/
|
|
45
|
+
export function requiredClasses(selector) {
|
|
46
|
+
let s = selector;
|
|
47
|
+
// Drop :not(), :is(), :where() and :has() groups (they can nest one level
|
|
48
|
+
// in practice): what is inside need not all appear, so a rule is never cut
|
|
49
|
+
// for classes in them.
|
|
50
|
+
for (let i = 0; i < 3 && /:(not|is|where|has)\(/.test(s); i++) {
|
|
51
|
+
s = s.replace(/:(?:not|is|where|has)\((?:[^()]|\([^()]*\))*\)/g, '');
|
|
52
|
+
}
|
|
53
|
+
return [...s.matchAll(/\.((?:\\.|[A-Za-z0-9_-])+)/g)].map(m => unescapeIdent(m[1]));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Split a selector list on top-level commas.
|
|
58
|
+
* @param {string} list
|
|
59
|
+
* @returns {string[]}
|
|
60
|
+
*/
|
|
61
|
+
function splitSelectors(list) {
|
|
62
|
+
/** @type {string[]} */
|
|
63
|
+
const out = [];
|
|
64
|
+
let depth = 0;
|
|
65
|
+
let start = 0;
|
|
66
|
+
for (let i = 0; i < list.length; i++) {
|
|
67
|
+
const ch = list[i];
|
|
68
|
+
if (ch === '\\') {
|
|
69
|
+
i++;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (ch === '(' || ch === '[') depth++;
|
|
73
|
+
else if (ch === ')' || ch === ']') depth--;
|
|
74
|
+
else if (ch === ',' && depth === 0) {
|
|
75
|
+
out.push(list.slice(start, i));
|
|
76
|
+
start = i + 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
out.push(list.slice(start));
|
|
80
|
+
return out.map(s => s.trim()).filter(Boolean);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Cut a stylesheet to the rules the page uses.
|
|
85
|
+
* @param {string} css
|
|
86
|
+
* @param {Set<string>} used class names in the page
|
|
87
|
+
* @returns {string}
|
|
88
|
+
*/
|
|
89
|
+
export function cutCss(css, used) {
|
|
90
|
+
const rootNode = postcss.parse(css);
|
|
91
|
+
rootNode.walkComments(c => {
|
|
92
|
+
c.remove();
|
|
93
|
+
});
|
|
94
|
+
// Layer order statements are written once, first, by the caller.
|
|
95
|
+
rootNode.walkAtRules('layer', at => {
|
|
96
|
+
if (!at.nodes) at.remove();
|
|
97
|
+
});
|
|
98
|
+
rootNode.walkRules(rule => {
|
|
99
|
+
const parent = /** @type {any} */ (rule.parent);
|
|
100
|
+
if (parent?.type === 'atrule' && /keyframes$/i.test(parent.name)) return;
|
|
101
|
+
const kept = splitSelectors(rule.selector).filter(sel =>
|
|
102
|
+
requiredClasses(sel).every(c => used.has(c)),
|
|
103
|
+
);
|
|
104
|
+
if (kept.length === 0) rule.remove();
|
|
105
|
+
else if (kept.length !== splitSelectors(rule.selector).length) {
|
|
106
|
+
rule.selector = kept.join(',');
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
// Drop the grouping at-rules left empty, innermost first.
|
|
110
|
+
let changed = true;
|
|
111
|
+
while (changed) {
|
|
112
|
+
changed = false;
|
|
113
|
+
rootNode.walkAtRules(at => {
|
|
114
|
+
if (
|
|
115
|
+
/^(media|supports|container|layer|scope|starting-style)$/i.test(at.name) &&
|
|
116
|
+
at.nodes &&
|
|
117
|
+
at.nodes.length === 0
|
|
118
|
+
) {
|
|
119
|
+
at.remove();
|
|
120
|
+
changed = true;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
// Keep only the keyframes a remaining rule names.
|
|
125
|
+
/** @type {Set<string>} */
|
|
126
|
+
const named = new Set();
|
|
127
|
+
rootNode.walkDecls(/^(animation|animation-name)$/i, d => {
|
|
128
|
+
for (const w of d.value.split(/[\s,]+/)) named.add(w);
|
|
129
|
+
});
|
|
130
|
+
rootNode.walkAtRules(/keyframes$/i, at => {
|
|
131
|
+
if (!named.has(at.params.trim())) at.remove();
|
|
132
|
+
});
|
|
133
|
+
return rootNode.toString().replace(/\n\s*\n/g, '\n');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The first `@layer a, b, c;` order statement in a stylesheet, if any.
|
|
138
|
+
* @param {string} css
|
|
139
|
+
* @returns {string}
|
|
140
|
+
*/
|
|
141
|
+
export function layerOrderOf(css) {
|
|
142
|
+
const bare = css.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
143
|
+
return /@layer\s+[\w-]+(?:\s*,\s*[\w-]+)*\s*;/.exec(bare)?.[0] ?? '';
|
|
144
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* Whether a test, story or benchmark runner would run the file with nothing
|
|
6
|
+
* importing it, by the union of their default patterns: Jest and Vitest
|
|
7
|
+
* (`*.test.*`, `*.spec.*`, `test.*`, `spec.*`, `__tests__/`), Vitest bench
|
|
8
|
+
* (`*.bench.*`, `*.benchmark.*`), Storybook (`*.stories.*`, `*.story.*`),
|
|
9
|
+
* Cypress (`*.cy.*`), Bun (`*_test.*`, `*_spec.*`), `node --test`
|
|
10
|
+
* (`*-test.*`, `test/`) and AVA (`tests/`), and `test-*` for .js and .ts.
|
|
11
|
+
* @param {string[]} segments the path where the file lands, from the project
|
|
12
|
+
* root (the folder runners search), `/`-split
|
|
13
|
+
*/
|
|
14
|
+
export function runnerPicksUp(segments: string[]): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Why a path may not be carried by a rendered page, or null when it may.
|
|
17
|
+
* @param {string} p a `/`-separated path relative to the embedded tree's root
|
|
18
|
+
* @param {{page?: boolean}} [options] `page`: the path is the page itself
|
|
19
|
+
* @returns {{code: 'ERR_PATH_TRAVERSAL' | 'ERR_RENDER_SOURCE_DAMAGED', reason: string} | null}
|
|
20
|
+
*/
|
|
21
|
+
export function embeddedPathProblem(p: string, options?: {
|
|
22
|
+
page?: boolean;
|
|
23
|
+
}): {
|
|
24
|
+
code: "ERR_PATH_TRAVERSAL" | "ERR_RENDER_SOURCE_DAMAGED";
|
|
25
|
+
reason: string;
|
|
26
|
+
} | null;
|
|
27
|
+
/** The file extensions a page module may have. */
|
|
28
|
+
export const PAGE_EXTENSIONS: string[];
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Which files a rendered page may carry, for `astryx experimental
|
|
5
|
+
* render`. Rendering embeds only these, and `--reverse` writes only these: a
|
|
6
|
+
* file someone sends can name nothing else. A page is a module; beside it go
|
|
7
|
+
* only the stylesheets, JSON and images it imports. Never a dotfile or a
|
|
8
|
+
* dot-folder (such as `.git/`), never `node_modules`, never a package
|
|
9
|
+
* manifest, lockfile or tool configuration (`package.json`, `tsconfig.json`,
|
|
10
|
+
* `vite.config.ts`, `astryx.config.mjs`, ...), and never a path that climbs
|
|
11
|
+
* (`..`) or starts at the root.
|
|
12
|
+
*
|
|
13
|
+
* @position foundation/render — used by render-page.mjs and the render subject
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {isAssetPath} from './assets.mjs';
|
|
17
|
+
|
|
18
|
+
/** The file extensions a page module may have. */
|
|
19
|
+
export const PAGE_EXTENSIONS = ['.tsx', '.jsx', '.ts', '.js', '.mjs'];
|
|
20
|
+
|
|
21
|
+
/** JSON files that configure a project or a tool rather than hold page data. */
|
|
22
|
+
const CONFIG_JSON =
|
|
23
|
+
/^(package|package-lock|composer|deno|turbo|vercel|nx|lerna|app|bower|now|netlify|firebase|renovate|project|workspace|angular|biome|components)\.jsonc?$|config|(^|[._-])rc\.jsonc?$|(^|[._-])lock\.jsonc?$/i;
|
|
24
|
+
|
|
25
|
+
/** Tool configuration modules: `vite.config.ts`, `astryx.config.mjs`, `next.config.js`, `.eslintrc.js`, ... */
|
|
26
|
+
const CONFIG_MODULE = /(^|\.)config\.[cm]?[jt]sx?$|^[a-z0-9-]+rc\.[cm]?[jt]s$/i;
|
|
27
|
+
|
|
28
|
+
/** Any script module. */
|
|
29
|
+
const SCRIPT = /\.(c|m)?[jt]sx?$/i;
|
|
30
|
+
/** Script modules `node --test` and AVA match by a `test-*` name (no JSX). */
|
|
31
|
+
const PLAIN_SCRIPT = /\.(c|m)?[jt]s$/i;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether a test, story or benchmark runner would run the file with nothing
|
|
35
|
+
* importing it, by the union of their default patterns: Jest and Vitest
|
|
36
|
+
* (`*.test.*`, `*.spec.*`, `test.*`, `spec.*`, `__tests__/`), Vitest bench
|
|
37
|
+
* (`*.bench.*`, `*.benchmark.*`), Storybook (`*.stories.*`, `*.story.*`),
|
|
38
|
+
* Cypress (`*.cy.*`), Bun (`*_test.*`, `*_spec.*`), `node --test`
|
|
39
|
+
* (`*-test.*`, `test/`) and AVA (`tests/`), and `test-*` for .js and .ts.
|
|
40
|
+
* @param {string[]} segments the path where the file lands, from the project
|
|
41
|
+
* root (the folder runners search), `/`-split
|
|
42
|
+
*/
|
|
43
|
+
export function runnerPicksUp(segments) {
|
|
44
|
+
const base = segments[segments.length - 1];
|
|
45
|
+
if (!SCRIPT.test(base)) return false;
|
|
46
|
+
const stem = base.replace(/\.[^.]+$/, '').toLowerCase();
|
|
47
|
+
const folders = segments.slice(0, -1).map(seg => seg.toLowerCase());
|
|
48
|
+
// Jest's `?(*.)+(spec|test)` repeats: `spectest.js`, `test.spec.js`.
|
|
49
|
+
if (/(^|[._-])(test|spec)+$/.test(stem)) return true;
|
|
50
|
+
if (/\.(bench|benchmark|stories|story|cy)$/.test(stem)) return true;
|
|
51
|
+
if (folders.some(seg => seg === '__tests__' || seg === 'test' || seg === 'tests')) return true;
|
|
52
|
+
// `test-*` only for the .js and .ts families: no default runner picks up
|
|
53
|
+
// `test-results.tsx`.
|
|
54
|
+
if (PLAIN_SCRIPT.test(base) && /^test[-_]/.test(stem)) return true;
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @param {string} p */
|
|
59
|
+
const extOf = p => {
|
|
60
|
+
const base = p.slice(p.lastIndexOf('/') + 1);
|
|
61
|
+
const dot = base.lastIndexOf('.');
|
|
62
|
+
return dot > 0 ? base.slice(dot).toLowerCase() : '';
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Why a path may not be carried by a rendered page, or null when it may.
|
|
67
|
+
* @param {string} p a `/`-separated path relative to the embedded tree's root
|
|
68
|
+
* @param {{page?: boolean}} [options] `page`: the path is the page itself
|
|
69
|
+
* @returns {{code: 'ERR_PATH_TRAVERSAL' | 'ERR_RENDER_SOURCE_DAMAGED', reason: string} | null}
|
|
70
|
+
*/
|
|
71
|
+
export function embeddedPathProblem(p, options = {}) {
|
|
72
|
+
if (typeof p !== 'string' || p.length === 0 || p.includes('\0')) {
|
|
73
|
+
return {code: 'ERR_RENDER_SOURCE_DAMAGED', reason: 'an empty or invalid path'};
|
|
74
|
+
}
|
|
75
|
+
if (p.startsWith('/') || /^[a-zA-Z]:/.test(p) || p.includes('\\')) {
|
|
76
|
+
return {code: 'ERR_PATH_TRAVERSAL', reason: `an absolute path (${p})`};
|
|
77
|
+
}
|
|
78
|
+
const segments = p.split('/');
|
|
79
|
+
if (segments.some(s => s === '..' || s === '.' || s === '')) {
|
|
80
|
+
return {code: 'ERR_PATH_TRAVERSAL', reason: `a path outside its own tree (${p})`};
|
|
81
|
+
}
|
|
82
|
+
if (segments.some(s => s.startsWith('.'))) {
|
|
83
|
+
return {code: 'ERR_RENDER_SOURCE_DAMAGED', reason: `a dotfile or dot-folder (${p})`};
|
|
84
|
+
}
|
|
85
|
+
if (segments.includes('node_modules')) {
|
|
86
|
+
return {code: 'ERR_RENDER_SOURCE_DAMAGED', reason: `a file inside node_modules (${p})`};
|
|
87
|
+
}
|
|
88
|
+
const base = segments[segments.length - 1];
|
|
89
|
+
const ext = extOf(p);
|
|
90
|
+
// The page is judged by where it lands (its name in the file is only a
|
|
91
|
+
// default), so only its kind counts here.
|
|
92
|
+
if (options.page) {
|
|
93
|
+
if (!PAGE_EXTENSIONS.includes(ext)) {
|
|
94
|
+
return {code: 'ERR_RENDER_SOURCE_DAMAGED', reason: `a page that is not a page module (${p})`};
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
// A file a test, story or benchmark runner picks up by its name or folder
|
|
99
|
+
// runs without anyone importing it.
|
|
100
|
+
if (runnerPicksUp(segments)) {
|
|
101
|
+
return {code: 'ERR_RENDER_SOURCE_DAMAGED', reason: `a test, spec, story or benchmark file (${p})`};
|
|
102
|
+
}
|
|
103
|
+
if (((ext === '.json' || ext === '.jsonc') && CONFIG_JSON.test(base)) || CONFIG_MODULE.test(base)) {
|
|
104
|
+
return {code: 'ERR_RENDER_SOURCE_DAMAGED', reason: `a project or tool configuration file (${p})`};
|
|
105
|
+
}
|
|
106
|
+
if (PAGE_EXTENSIONS.includes(ext) || ext === '.css' || ext === '.json' || isAssetPath(p)) return null;
|
|
107
|
+
return {code: 'ERR_RENDER_SOURCE_DAMAGED', reason: `a kind of file a render never embeds (${p})`};
|
|
108
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
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 string specifiers one module imports.
|
|
6
|
+
* @param {string} source
|
|
7
|
+
* @param {string} file for the parser's TypeScript/JSX choice
|
|
8
|
+
* @returns {string[]}
|
|
9
|
+
*/
|
|
10
|
+
export function importSpecifiers(source: string, file: string): string[];
|
|
11
|
+
/**
|
|
12
|
+
* Relative asset paths in a piece of text: a `./`/`../` path to an image or
|
|
13
|
+
* font, bare or inside `url(...)`.
|
|
14
|
+
* @param {string} text
|
|
15
|
+
* @returns {string[]}
|
|
16
|
+
*/
|
|
17
|
+
export function assetRefsIn(text: string): string[];
|
|
18
|
+
/**
|
|
19
|
+
* Path aliases (`compilerOptions.paths`) of the project that holds `dir`, as
|
|
20
|
+
* absolute prefixes: `"@/*": ["./src/*"]` becomes `["@/", "<root>/src/"]`.
|
|
21
|
+
* @param {string} dir
|
|
22
|
+
* @returns {[string, string][]}
|
|
23
|
+
*/
|
|
24
|
+
export function receivingAliases(dir: string): [string, string][];
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {object} Reach
|
|
27
|
+
* @property {boolean} relative - Reached by a relative import (or it is the
|
|
28
|
+
* page), so it goes where the page's layout puts it.
|
|
29
|
+
* @property {Set<string>} aliased - Absolute paths a receiving-project alias
|
|
30
|
+
* import names for it, so it also goes there.
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* The embedded files the page reaches through its imports.
|
|
34
|
+
* @param {{path: string, base64: string}[]} files the embedded files, page
|
|
35
|
+
* first; every path appears once (callers refuse duplicates first)
|
|
36
|
+
* @param {{aliases?: [string, string][]}} [options] the receiving project's
|
|
37
|
+
* path aliases, absolute (see receivingAliases)
|
|
38
|
+
* @returns {{reached: Map<string, Reach>, missing: string[]}} the reached
|
|
39
|
+
* tree paths, and the relative imports the file does not carry
|
|
40
|
+
*/
|
|
41
|
+
export function reachableFiles(files: {
|
|
42
|
+
path: string;
|
|
43
|
+
base64: string;
|
|
44
|
+
}[], { aliases }?: {
|
|
45
|
+
aliases?: [string, string][];
|
|
46
|
+
}): {
|
|
47
|
+
reached: Map<string, Reach>;
|
|
48
|
+
missing: string[];
|
|
49
|
+
};
|
|
50
|
+
export type Reach = {
|
|
51
|
+
/**
|
|
52
|
+
* - Reached by a relative import (or it is the
|
|
53
|
+
* page), so it goes where the page's layout puts it.
|
|
54
|
+
*/
|
|
55
|
+
relative: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* - Absolute paths a receiving-project alias
|
|
58
|
+
* import names for it, so it also goes there.
|
|
59
|
+
*/
|
|
60
|
+
aliased: Set<string>;
|
|
61
|
+
};
|