@astryxdesign/cli 0.7.15 → 0.7.16-canary.g3aa244b
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 +5 -3
- package/api/build/_adapter.d.mts +1 -0
- package/api/build/blocks/blocks.d.mts +1 -0
- package/api/build/blocks/blocks.mjs +18 -1
- package/api/component/_adapter.d.mts +1 -1
- package/api/component/_adapter.mjs +28 -1
- package/api/doctor/_adapter.d.mts +35 -0
- package/api/doctor/_adapter.mjs +69 -0
- package/api/doctor/built-checks.mjs +1 -1
- package/api/doctor/doctor.d.mts +18 -0
- package/api/doctor/doctor.doc.mjs +1 -1
- package/api/doctor/doctor.mjs +104 -4
- package/api/doctor/theme-checks.mjs +6 -10
- package/api/integration/add-contribution.mjs +3 -3
- package/api/integration/pack-check.mjs +14 -8
- package/api/integration/validate-integration.mjs +1 -1
- package/api/search/_adapter.mjs +15 -16
- package/api/search/_entries.d.mts +2 -1
- package/api/search/_entries.mjs +28 -1
- package/api/swizzle/_moduleGraph.mjs +1 -1
- package/api/theme/_adapter.d.mts +5 -2
- package/api/theme/_adapter.mjs +43 -12
- package/api/theme/_fork.mjs +1 -1
- package/api/theme/build/build.d.mts +116 -32
- package/api/theme/build/build.mjs +449 -102
- package/api/theme/eject/eject.mjs +22 -1
- package/api/theme/template/template.d.mts +19 -0
- package/api/theme/template/template.mjs +102 -25
- package/api/theme/theme.type.d.mts +5 -1
- package/api/theme/theme.type.mjs +2 -1
- package/api/theme/themeBuild.doc.mjs +3 -3
- package/api/theme/themeEject.doc.mjs +1 -1
- package/api/theme/themeTemplate.doc.mjs +9 -3
- package/api/theme-edit/_adapter.d.mts +32 -2
- package/api/theme-edit/_adapter.mjs +264 -40
- package/api/theme-edit/_check.d.mts +93 -4
- package/api/theme-edit/_check.mjs +1727 -223
- package/api/theme-edit/_color.d.mts +2 -8
- package/api/theme-edit/_color.mjs +292 -219
- package/api/theme-edit/_compiler.d.mts +1 -0
- package/api/theme-edit/_compiler.mjs +20 -1
- package/api/theme-edit/_context.d.mts +31 -1
- package/api/theme-edit/_context.mjs +445 -41
- package/api/theme-edit/_css.d.mts +60 -1
- package/api/theme-edit/_css.mjs +404 -28
- package/api/theme-edit/_edit.d.mts +25 -0
- package/api/theme-edit/_edit.mjs +1532 -149
- package/api/theme-edit/_how-commands.d.mts +11 -26
- package/api/theme-edit/_how-commands.mjs +30 -2
- package/api/theme-edit/_how-question.d.mts +7 -0
- package/api/theme-edit/_how-question.mjs +1304 -176
- package/api/theme-edit/_how.mjs +144 -30
- package/api/theme-edit/_overview.mjs +2 -0
- package/api/theme-edit/_palette.d.mts +38 -0
- package/api/theme-edit/_palette.mjs +305 -71
- package/api/theme-edit/_parts.d.mts +33 -0
- package/api/theme-edit/_parts.mjs +138 -9
- package/api/theme-edit/_png.d.mts +18 -0
- package/api/theme-edit/_png.mjs +95 -14
- package/api/theme-edit/_preview-app.d.mts +36 -1
- package/api/theme-edit/_preview-app.mjs +1415 -96
- package/api/theme-edit/_preview.d.mts +64 -3
- package/api/theme-edit/_preview.mjs +514 -31
- package/api/theme-edit/_project.d.mts +11 -0
- package/api/theme-edit/_project.mjs +46 -12
- package/api/theme-edit/_settings.d.mts +278 -5
- package/api/theme-edit/_settings.mjs +2741 -195
- package/api/theme-edit/_tables.d.mts +24 -0
- package/api/theme-edit/_tables.mjs +211 -149
- package/api/theme-edit/_vet.d.mts +42 -0
- package/api/theme-edit/_vet.mjs +144 -0
- package/api/theme-edit/theme-edit.type.d.mts +20 -2
- package/api/theme-edit/theme-edit.type.mjs +6 -2
- package/api/theme-edit/themeEditNew.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
- package/api/theme-edit/themeEditShow.doc.mjs +11 -3
- package/api/upgrade/registry/registry.mjs +6 -1
- package/assets/codemods/parse.mjs +2 -2
- package/assets/docs/tree/single-component.doc.mjs +11 -2
- package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
- package/assets/docs.generated.json +2 -2
- package/assets/preview-app/next.config.mjs +7 -1
- package/assets/preview-app/package.json +1 -0
- package/assets/preview-app/src/app/globals.css +1 -0
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/dashboard/page.tsx +1 -1
- package/authoring/doctypes/_schema.d.mts +52 -5
- package/authoring/doctypes/_schema.mjs +62 -7
- package/authoring/doctypes/base/type.ts +10 -0
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/authoring/doctypes/function/function.doc.mjs +6 -0
- package/authoring/doctypes/function/type.ts +3 -0
- package/authoring/doctypes/legacy.mjs +5 -1
- package/authoring/index.d.ts +1 -0
- package/authoring/shadcn/source-variants.d.mts +1 -1
- package/authoring/shadcn/source-variants.mjs +32 -10
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/doctor.doc.mjs +2 -2
- package/clients/cli/commands/experimental/theme.mjs +239 -45
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-template.doc.mjs +2 -2
- package/clients/cli/commands/theme-build.doc.mjs +2 -2
- package/clients/cli/commands/theme-eject.doc.mjs +1 -1
- package/clients/cli/commands/theme-template.doc.mjs +3 -1
- package/clients/cli/commands/theme.mjs +190 -46
- package/clients/cli/formatters/index.mjs +7 -4
- package/clients/cli/lib/cli-error.mjs +9 -3
- package/clients/cli/lib/component-format.mjs +27 -9
- package/clients/cli/lib/json-shim.mjs +72 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/clients/cli/run.mjs +49 -0
- package/foundation/agent-docs/agent-docs-target.d.mts +2 -6
- package/foundation/agent-docs/agent-docs-target.mjs +9 -17
- package/foundation/agent-docs/agent-docs.mjs +3 -2
- package/foundation/config/config-writer.mjs +2 -2
- package/foundation/config/project.mjs +60 -14
- package/foundation/discovery/component-discovery.mjs +5 -17
- package/foundation/discovery/icon-set-writer.mjs +1 -1
- package/foundation/discovery/module-exports.mjs +1 -15
- package/foundation/discovery/template-adapter.mjs +44 -19
- package/foundation/discovery/template-source-tree.mjs +3 -16
- package/foundation/discovery/theme-discovery.d.mts +5 -1
- package/foundation/discovery/theme-discovery.mjs +61 -43
- package/foundation/fs/exact-codec.d.mts +35 -0
- package/foundation/fs/exact-codec.mjs +148 -0
- package/foundation/fs/gather-cache.d.mts +15 -5
- package/foundation/fs/gather-cache.mjs +129 -31
- package/foundation/fs/paths.d.mts +39 -0
- package/foundation/fs/paths.mjs +44 -11
- package/foundation/fs/read-record.mjs +11 -1
- package/foundation/integrations/contribution-fixes.mjs +1 -16
- package/foundation/integrations/may-have-integrations.mjs +3 -2
- package/foundation/integrations/public-imports.mjs +1 -1
- package/foundation/render/embedded-imports.mjs +1 -1
- package/foundation/render/host-exit.d.mts +8 -0
- package/foundation/render/host-exit.mjs +11 -0
- package/foundation/render/page-host.mjs +56 -10
- package/foundation/render/page-worker.mjs +14 -0
- package/foundation/render/render-page.mjs +37 -2
- package/foundation/response/error-codes.d.mts +2 -1
- package/foundation/response/error-codes.d.ts +1 -0
- package/foundation/response/error-codes.doc.mjs +5 -0
- package/foundation/response/error-codes.mjs +3 -0
- package/foundation/response/json.d.mts +15 -0
- package/foundation/response/json.mjs +52 -1
- package/foundation/response/response-types.doc.mjs +1 -1
- package/foundation/runtime/lazy-dependencies.d.mts +14 -0
- package/foundation/runtime/lazy-dependencies.mjs +82 -0
- package/foundation/xle/browser.d.mts +2 -2
- package/package.json +7 -6
|
@@ -17,47 +17,23 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import * as fs from 'node:fs';
|
|
20
|
-
import {createRequire} from 'node:module';
|
|
21
20
|
import * as path from 'node:path';
|
|
22
21
|
import {lowerDoc} from '../doc-compiler/compile.mjs';
|
|
23
22
|
import {packageSource} from '../doc-compiler/source.mjs';
|
|
24
|
-
import {CLI_ROOT} from '../fs/paths.mjs';
|
|
23
|
+
import {CLI_ROOT, realDir} from '../fs/paths.mjs';
|
|
24
|
+
import {persistedGather} from '../fs/gather-cache.mjs';
|
|
25
|
+
import {EXACT_CODEC} from '../fs/exact-codec.mjs';
|
|
25
26
|
import {listDirEntries} from '../fs/listing-cache.mjs';
|
|
26
27
|
import {formatCliCommand} from '../env/package-manager.mjs';
|
|
27
28
|
import {LOCAL_THEME_ROOT} from '../config/theme-state.mjs';
|
|
28
29
|
import {assertWithin, PathSafetyError} from '../fs/path-safety.mjs';
|
|
29
30
|
import {THEME_MODULE_EXTENSIONS} from './theme-module-extensions.mjs';
|
|
31
|
+
import {babelParser, jscodeshift} from '../runtime/lazy-dependencies.mjs';
|
|
30
32
|
|
|
31
33
|
export const BUNDLED_THEME_PACKAGE = '@astryxdesign/cli';
|
|
32
34
|
export const THEMES_DIR = path.join(CLI_ROOT, 'assets', 'templates', 'themes');
|
|
33
35
|
export const THEME_DOC_SUFFIX = '.doc.mjs';
|
|
34
36
|
|
|
35
|
-
const require = createRequire(import.meta.url);
|
|
36
|
-
|
|
37
|
-
/** @type {typeof import('@babel/parser') | undefined} */
|
|
38
|
-
let babelParser;
|
|
39
|
-
/** @type {any} */
|
|
40
|
-
let jscodeshiftApi;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The descriptor parser, loaded when the first descriptor is read.
|
|
44
|
-
* @returns {typeof import('@babel/parser')}
|
|
45
|
-
*/
|
|
46
|
-
function descriptorParser() {
|
|
47
|
-
babelParser ??= require('@babel/parser');
|
|
48
|
-
return /** @type {typeof import('@babel/parser')} */ (babelParser);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The theme source parser, loaded when the first integration theme source is
|
|
53
|
-
* checked. Bundled themes never need it.
|
|
54
|
-
* @returns {any}
|
|
55
|
-
*/
|
|
56
|
-
function sourceParser() {
|
|
57
|
-
jscodeshiftApi ??= require('jscodeshift');
|
|
58
|
-
return jscodeshiftApi;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
37
|
/**
|
|
62
38
|
* @typedef {object} DiscoveredTheme
|
|
63
39
|
* @property {string} slug
|
|
@@ -169,9 +145,11 @@ class ThemeRuntimeExportError extends Error {}
|
|
|
169
145
|
* Every string specifier a module imports, re-exports, or imports dynamically.
|
|
170
146
|
* @param {string} file
|
|
171
147
|
* @param {any} jscodeshift
|
|
148
|
+
* @param {{withRequire?: boolean}} [options] `withRequire`: also
|
|
149
|
+
* `require('./x')` and TypeScript `import x = require('./x')`
|
|
172
150
|
* @returns {string[]}
|
|
173
151
|
*/
|
|
174
|
-
function moduleSpecifiers(file, jscodeshift) {
|
|
152
|
+
function moduleSpecifiers(file, jscodeshift, {withRequire = false} = {}) {
|
|
175
153
|
const parser = /\.(?:ts|tsx|mts|cts)$/u.test(file) ? 'tsx' : 'babel';
|
|
176
154
|
const j = jscodeshift.withParser(parser);
|
|
177
155
|
const root = j(fs.readFileSync(file, 'utf-8'));
|
|
@@ -207,6 +185,23 @@ function moduleSpecifiers(file, jscodeshift) {
|
|
|
207
185
|
specifiers.push(importPath.node.source.value);
|
|
208
186
|
}
|
|
209
187
|
});
|
|
188
|
+
if (withRequire) {
|
|
189
|
+
// CommonJS `require('./x')` and TypeScript `import x = require('./x')`.
|
|
190
|
+
root.find(j.CallExpression).forEach((/** @type {any} */ callPath) => {
|
|
191
|
+
const {callee, arguments: args} = callPath.node;
|
|
192
|
+
if (
|
|
193
|
+
callee?.type === 'Identifier' &&
|
|
194
|
+
callee.name === 'require' &&
|
|
195
|
+
typeof args?.[0]?.value === 'string'
|
|
196
|
+
)
|
|
197
|
+
specifiers.push(args[0].value);
|
|
198
|
+
});
|
|
199
|
+
root.find(j.TSImportEqualsDeclaration).forEach((/** @type {any} */ p) => {
|
|
200
|
+
const ref = p.node.moduleReference;
|
|
201
|
+
const value = ref?.expression?.value ?? ref?.argument?.value;
|
|
202
|
+
if (typeof value === 'string') specifiers.push(value);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
210
205
|
|
|
211
206
|
return specifiers;
|
|
212
207
|
}
|
|
@@ -214,10 +209,12 @@ function moduleSpecifiers(file, jscodeshift) {
|
|
|
214
209
|
/**
|
|
215
210
|
* Every module a theme source file imports or re-exports, as written.
|
|
216
211
|
* @param {string} file
|
|
212
|
+
* @param {{withRequire?: boolean}} [options] `withRequire`: also
|
|
213
|
+
* `require('./x')` and TypeScript `import x = require('./x')`.
|
|
217
214
|
* @returns {string[]}
|
|
218
215
|
*/
|
|
219
|
-
export function themeFileImports(file) {
|
|
220
|
-
return moduleSpecifiers(file,
|
|
216
|
+
export function themeFileImports(file, options = {}) {
|
|
217
|
+
return moduleSpecifiers(file, jscodeshift(), options);
|
|
221
218
|
}
|
|
222
219
|
|
|
223
220
|
/**
|
|
@@ -709,7 +706,7 @@ function readDescriptorSource(source, label, typeName, data = false) {
|
|
|
709
706
|
/** @type {any} */
|
|
710
707
|
let ast;
|
|
711
708
|
try {
|
|
712
|
-
ast =
|
|
709
|
+
ast = babelParser().parse(source, {
|
|
713
710
|
sourceType: 'module',
|
|
714
711
|
tokens: true,
|
|
715
712
|
createParenthesizedExpressions: true,
|
|
@@ -1514,11 +1511,11 @@ export function discoverThemeDirectory(
|
|
|
1514
1511
|
)
|
|
1515
1512
|
.filter(file => file !== docPath),
|
|
1516
1513
|
);
|
|
1517
|
-
const
|
|
1514
|
+
const codeshift = jscodeshift();
|
|
1518
1515
|
try {
|
|
1519
1516
|
validateThemeModuleGraph(
|
|
1520
1517
|
entryPath,
|
|
1521
|
-
|
|
1518
|
+
codeshift,
|
|
1522
1519
|
sourceDir,
|
|
1523
1520
|
allowedFiles,
|
|
1524
1521
|
owner,
|
|
@@ -1529,7 +1526,7 @@ export function discoverThemeDirectory(
|
|
|
1529
1526
|
!moduleExportsName(
|
|
1530
1527
|
entryPath,
|
|
1531
1528
|
exportName,
|
|
1532
|
-
|
|
1529
|
+
codeshift,
|
|
1533
1530
|
sourceDir,
|
|
1534
1531
|
allowedFiles,
|
|
1535
1532
|
)
|
|
@@ -1660,7 +1657,7 @@ export function unreadThemeFolders(themeRoot, {packageDir, packageName} = {}) {
|
|
|
1660
1657
|
for (const file of modules) {
|
|
1661
1658
|
let specifiers;
|
|
1662
1659
|
try {
|
|
1663
|
-
specifiers = moduleSpecifiers(file,
|
|
1660
|
+
specifiers = moduleSpecifiers(file, jscodeshift());
|
|
1664
1661
|
} catch {
|
|
1665
1662
|
continue;
|
|
1666
1663
|
}
|
|
@@ -1745,18 +1742,39 @@ export function discoverLocalThemes(
|
|
|
1745
1742
|
}));
|
|
1746
1743
|
}
|
|
1747
1744
|
|
|
1745
|
+
/** An integration's discovered themes, kept exactly, with the shape they need. */
|
|
1746
|
+
const THEMES_CODEC = {
|
|
1747
|
+
...EXACT_CODEC,
|
|
1748
|
+
valid: (/** @type {any} */ themes) =>
|
|
1749
|
+
Array.isArray(themes) &&
|
|
1750
|
+
themes.every(
|
|
1751
|
+
theme =>
|
|
1752
|
+
theme != null &&
|
|
1753
|
+
typeof theme.slug === 'string' &&
|
|
1754
|
+
typeof theme.exportName === 'string',
|
|
1755
|
+
),
|
|
1756
|
+
};
|
|
1757
|
+
|
|
1748
1758
|
/**
|
|
1749
1759
|
* @param {import('../integrations/integrations.mjs').LoadedIntegration} integration
|
|
1750
1760
|
* @returns {Promise<DiscoveredTheme[]>}
|
|
1751
1761
|
*/
|
|
1752
1762
|
export async function discoverIntegrationThemes(integration) {
|
|
1753
1763
|
if (!integration.themes) return [];
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1764
|
+
const themesDir = integration.themes;
|
|
1765
|
+
// Kept between runs while every file and module it read is unchanged; the
|
|
1766
|
+
// live fields below are added after, as on a fresh read.
|
|
1767
|
+
const found = /** @type {DiscoveredTheme[]} */ (
|
|
1768
|
+
await persistedGather(
|
|
1769
|
+
['integration-themes', realDir(themesDir), integration.name ?? null],
|
|
1770
|
+
async () => discoverThemeDirectory(themesDir, integration.name),
|
|
1771
|
+
THEMES_CODEC,
|
|
1772
|
+
)
|
|
1761
1773
|
);
|
|
1774
|
+
return found.map(theme => ({
|
|
1775
|
+
...theme,
|
|
1776
|
+
packageDir: integration.__packageDir,
|
|
1777
|
+
packageExports: integration.__packageExports,
|
|
1778
|
+
source: /** @type {const} */ ('package'),
|
|
1779
|
+
}));
|
|
1762
1780
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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 `value` is plain data that JSON, with `undefined` stood in for,
|
|
6
|
+
* carries exactly: finite numbers but -0, strings, booleans, null, arrays
|
|
7
|
+
* with no holes or other properties, and objects of Object's own prototype
|
|
8
|
+
* whose properties are all enumerable, plain, and string-keyed, with no
|
|
9
|
+
* object reached twice. `undefined` counts only inside an object or array.
|
|
10
|
+
* @param {unknown} value
|
|
11
|
+
* @param {Set<object>} [seen]
|
|
12
|
+
* @param {boolean} [inside]
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
export function isPlain(value: unknown, seen?: Set<object>, inside?: boolean): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Whether two plain values are the same, key order included: what
|
|
18
|
+
* isDeepStrictEqual does not check.
|
|
19
|
+
* @param {unknown} a
|
|
20
|
+
* @param {unknown} b
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
export function sameExactly(a: unknown, b: unknown): boolean;
|
|
24
|
+
export namespace EXACT_CODEC {
|
|
25
|
+
/**
|
|
26
|
+
* @param {unknown} value
|
|
27
|
+
* @returns {unknown}
|
|
28
|
+
*/
|
|
29
|
+
function encode(value: unknown): unknown;
|
|
30
|
+
/**
|
|
31
|
+
* @param {unknown} stored
|
|
32
|
+
* @returns {any}
|
|
33
|
+
*/
|
|
34
|
+
function decode(stored: unknown): any;
|
|
35
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file A codec for kept values (foundation/fs/gather-cache.mjs) that reads
|
|
5
|
+
* back exactly what it was given, or refuses to encode it.
|
|
6
|
+
* @input Plain data: objects, arrays, strings, numbers, booleans, null, and
|
|
7
|
+
* `undefined` as a property value or array item, which JSON drops.
|
|
8
|
+
* @output `EXACT_CODEC`: `encode` turns such a value into JSON-safe data, with
|
|
9
|
+
* each `undefined` as a reserved string (and a real string that begins with
|
|
10
|
+
* that string's first character escaped); `decode` restores it in place,
|
|
11
|
+
* key order kept. `encode` throws, keeping nothing, unless the value is
|
|
12
|
+
* plain data (isPlain) and reads back the same, key order included
|
|
13
|
+
* (sameExactly): so a function, a Map, a Date, a class instance, an Error,
|
|
14
|
+
* a symbol key, an accessor or hidden property, an array hole, -0, NaN, an
|
|
15
|
+
* infinity, or an object reached twice (shared or in a cycle) is never
|
|
16
|
+
* kept, and the value is worked out as without the cache.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The first character of every reserved string: no plain text holds it. */
|
|
20
|
+
const MARK = '\u0000';
|
|
21
|
+
|
|
22
|
+
/** What stands for `undefined`. */
|
|
23
|
+
const UNDEFINED = `${MARK}u`;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} key
|
|
27
|
+
* @param {unknown} value
|
|
28
|
+
* @returns {unknown}
|
|
29
|
+
*/
|
|
30
|
+
function replace(key, value) {
|
|
31
|
+
if (value === undefined) return UNDEFINED;
|
|
32
|
+
if (typeof value === 'string' && value.startsWith(MARK)) return MARK + value;
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Put back what `replace` stood in for, in place.
|
|
38
|
+
* @param {unknown} value
|
|
39
|
+
* @returns {unknown}
|
|
40
|
+
*/
|
|
41
|
+
function restore(value) {
|
|
42
|
+
if (typeof value === 'string' && value.startsWith(MARK)) {
|
|
43
|
+
return value === UNDEFINED ? undefined : value.slice(1);
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(value)) {
|
|
46
|
+
for (let i = 0; i < value.length; i++) value[i] = restore(value[i]);
|
|
47
|
+
} else if (value !== null && typeof value === 'object') {
|
|
48
|
+
const object = /** @type {Record<string, unknown>} */ (value);
|
|
49
|
+
for (const key of Object.keys(object)) object[key] = restore(object[key]);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whether `value` is plain data that JSON, with `undefined` stood in for,
|
|
56
|
+
* carries exactly: finite numbers but -0, strings, booleans, null, arrays
|
|
57
|
+
* with no holes or other properties, and objects of Object's own prototype
|
|
58
|
+
* whose properties are all enumerable, plain, and string-keyed, with no
|
|
59
|
+
* object reached twice. `undefined` counts only inside an object or array.
|
|
60
|
+
* @param {unknown} value
|
|
61
|
+
* @param {Set<object>} [seen]
|
|
62
|
+
* @param {boolean} [inside]
|
|
63
|
+
* @returns {boolean}
|
|
64
|
+
*/
|
|
65
|
+
export function isPlain(value, seen = new Set(), inside = false) {
|
|
66
|
+
if (value === undefined) return inside;
|
|
67
|
+
if (value === null) return true;
|
|
68
|
+
switch (typeof value) {
|
|
69
|
+
case 'string':
|
|
70
|
+
case 'boolean':
|
|
71
|
+
return true;
|
|
72
|
+
case 'number':
|
|
73
|
+
return Number.isFinite(value) && !Object.is(value, -0);
|
|
74
|
+
case 'object':
|
|
75
|
+
break;
|
|
76
|
+
default:
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const object = /** @type {object} */ (value);
|
|
80
|
+
if (seen.has(object)) return false;
|
|
81
|
+
seen.add(object);
|
|
82
|
+
const keys = Reflect.ownKeys(object);
|
|
83
|
+
if (Array.isArray(object)) {
|
|
84
|
+
if (Object.getPrototypeOf(object) !== Array.prototype) return false;
|
|
85
|
+
// Its indexes and `length`, nothing else, and no holes.
|
|
86
|
+
if (keys.length !== object.length + 1) return false;
|
|
87
|
+
for (let i = 0; i < object.length; i++) {
|
|
88
|
+
if (!Object.hasOwn(object, i)) return false;
|
|
89
|
+
const d = Object.getOwnPropertyDescriptor(object, i);
|
|
90
|
+
if (!d || !('value' in d) || !d.enumerable) return false;
|
|
91
|
+
if (!isPlain(d.value, seen, true)) return false;
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
if (Object.getPrototypeOf(object) !== Object.prototype) return false;
|
|
96
|
+
for (const key of keys) {
|
|
97
|
+
if (typeof key !== 'string') return false;
|
|
98
|
+
const d = Object.getOwnPropertyDescriptor(object, key);
|
|
99
|
+
if (!d || !('value' in d) || !d.enumerable) return false;
|
|
100
|
+
if (!isPlain(d.value, seen, true)) return false;
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Whether two plain values are the same, key order included: what
|
|
107
|
+
* isDeepStrictEqual does not check.
|
|
108
|
+
* @param {unknown} a
|
|
109
|
+
* @param {unknown} b
|
|
110
|
+
* @returns {boolean}
|
|
111
|
+
*/
|
|
112
|
+
export function sameExactly(a, b) {
|
|
113
|
+
if (Object.is(a, b)) return true;
|
|
114
|
+
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) return false;
|
|
115
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
116
|
+
const ka = Object.keys(a);
|
|
117
|
+
const kb = Object.keys(b);
|
|
118
|
+
if (ka.length !== kb.length) return false;
|
|
119
|
+
for (let i = 0; i < ka.length; i++) {
|
|
120
|
+
if (ka[i] !== kb[i]) return false;
|
|
121
|
+
const x = /** @type {Record<string, unknown>} */ (a)[ka[i]];
|
|
122
|
+
const y = /** @type {Record<string, unknown>} */ (b)[kb[i]];
|
|
123
|
+
if (!sameExactly(x, y)) return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const EXACT_CODEC = {
|
|
129
|
+
/**
|
|
130
|
+
* @param {unknown} value
|
|
131
|
+
* @returns {unknown}
|
|
132
|
+
*/
|
|
133
|
+
encode(value) {
|
|
134
|
+
if (!isPlain(value)) throw new Error('not plain data');
|
|
135
|
+
const text = JSON.stringify(value, replace);
|
|
136
|
+
if (!sameExactly(restore(JSON.parse(text)), value)) {
|
|
137
|
+
throw new Error('does not read back exactly');
|
|
138
|
+
}
|
|
139
|
+
return JSON.parse(text);
|
|
140
|
+
},
|
|
141
|
+
/**
|
|
142
|
+
* @param {unknown} stored
|
|
143
|
+
* @returns {any}
|
|
144
|
+
*/
|
|
145
|
+
decode(stored) {
|
|
146
|
+
return restore(stored);
|
|
147
|
+
},
|
|
148
|
+
};
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* The running recorded run's reads and variables,
|
|
5
|
+
* The running recorded run's reads and variables, or null outside one: for
|
|
6
|
+
* tests, and for a caller that keeps a value only in a recorded run (doctor's
|
|
7
|
+
* kept checks).
|
|
6
8
|
* @returns {{reads: Map<string, string>, env: Map<string, string | null>} | null}
|
|
7
9
|
*/
|
|
8
10
|
export function recordedRun(): {
|
|
@@ -39,9 +41,14 @@ export function recordRunFor<T>(argv: string[], fn: () => Promise<T>): Promise<T
|
|
|
39
41
|
* tests; `recordRunFor` decides which runs are recorded.
|
|
40
42
|
* @template T
|
|
41
43
|
* @param {() => Promise<T>} fn
|
|
44
|
+
* @param {{armedThroughout?: boolean}} [options] - `armedThroughout`: record
|
|
45
|
+
* modules and variables until the run ends, not only until each gather
|
|
46
|
+
* ends, for a run whose later gathers read modules loaded in between.
|
|
42
47
|
* @returns {Promise<T>}
|
|
43
48
|
*/
|
|
44
|
-
export function recordRun<T>(fn: () => Promise<T
|
|
49
|
+
export function recordRun<T>(fn: () => Promise<T>, { armedThroughout }?: {
|
|
50
|
+
armedThroughout?: boolean;
|
|
51
|
+
}): Promise<T>;
|
|
45
52
|
/**
|
|
46
53
|
* The gather `load` returns, or the one a run with the same key stored while
|
|
47
54
|
* every input it read is unchanged (see the file header).
|
|
@@ -49,8 +56,9 @@ export function recordRun<T>(fn: () => Promise<T>): Promise<T>;
|
|
|
49
56
|
* @param {unknown[]} key - What else decides the gather: its folder (a real
|
|
50
57
|
* path) and options.
|
|
51
58
|
* @param {() => Promise<T>} load
|
|
52
|
-
* @param {{encode: (value: T) => E, decode: (stored: E) => T}} codec - A
|
|
53
|
-
* plain-JSON form of the value and back
|
|
59
|
+
* @param {{encode: (value: T) => E, decode: (stored: E) => T, valid?: (value: T) => boolean}} codec - A
|
|
60
|
+
* plain-JSON form of the value and back, and optionally the shape a decoded
|
|
61
|
+
* value must have to be used (anything else is a miss).
|
|
54
62
|
* @param {(value: T) => boolean} [keep] whether a fresh gather may be
|
|
55
63
|
* stored: one that depends on something its stamps cannot see (a retry
|
|
56
64
|
* that waits on time) is used for this run only
|
|
@@ -59,6 +67,7 @@ export function recordRun<T>(fn: () => Promise<T>): Promise<T>;
|
|
|
59
67
|
export function persistedGather<T, E>(key: unknown[], load: () => Promise<T>, codec: {
|
|
60
68
|
encode: (value: T) => E;
|
|
61
69
|
decode: (stored: E) => T;
|
|
70
|
+
valid?: (value: T) => boolean;
|
|
62
71
|
}, keep?: (value: T) => boolean): Promise<T>;
|
|
63
72
|
/**
|
|
64
73
|
* A value derived from what a kept gather holds, kept beside the gather: it
|
|
@@ -74,13 +83,14 @@ export function persistedGather<T, E>(key: unknown[], load: () => Promise<T>, co
|
|
|
74
83
|
* @template T, E
|
|
75
84
|
* @param {unknown[]} key - Everything the value is computed from.
|
|
76
85
|
* @param {() => T} compute
|
|
77
|
-
* @param {{encode: (value: T) => E, decode: (stored: E) => T}} codec
|
|
86
|
+
* @param {{encode: (value: T) => E, decode: (stored: E) => T, valid?: (value: T) => boolean}} codec
|
|
78
87
|
* @param {string[]} [inputs] - Files the computation reads, by path.
|
|
79
88
|
* @returns {T}
|
|
80
89
|
*/
|
|
81
90
|
export function persistedDerived<T, E>(key: unknown[], compute: () => T, codec: {
|
|
82
91
|
encode: (value: T) => E;
|
|
83
92
|
decode: (stored: E) => T;
|
|
93
|
+
valid?: (value: T) => boolean;
|
|
84
94
|
}, inputs?: string[]): T;
|
|
85
95
|
/**
|
|
86
96
|
* Whether this run is under Yarn Plug'n'Play's loader (`yarn astryx` in a PnP
|