@astryxdesign/cli 0.6.0 → 0.6.1
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/CHANGELOG.md +48 -1
- package/README.md +76 -67
- package/api/build/build.test.mjs +37 -0
- package/api/build/build.type.mjs +1 -1
- package/api/build/kit/kit.mjs +31 -1
- package/api/component/_adapter.mjs +14 -28
- package/api/component/integration-import-agreement.test.mjs +318 -0
- package/api/doctor/doctor.d.mts +32 -3
- package/api/doctor/doctor.doc.mjs +2 -1
- package/api/doctor/doctor.mjs +97 -4
- package/api/doctor/doctor.test.mjs +90 -1
- package/api/gap-report/gap-report-worker.d.mts +4 -0
- package/api/gap-report/gap-report-worker.mjs +73 -0
- package/api/gap-report/gap-report.d.mts +81 -0
- package/api/gap-report/gap-report.doc.d.mts +9 -0
- package/api/gap-report/gap-report.doc.mjs +127 -0
- package/api/gap-report/gap-report.mjs +707 -0
- package/api/gap-report/gap-report.test.mjs +1210 -0
- package/api/gap-report/gap-report.type.d.mts +88 -0
- package/api/gap-report/gap-report.type.mjs +69 -0
- package/api/index.d.mts +8 -1
- package/api/index.mjs +24 -1
- package/api/integration/add-contribution.d.mts +44 -0
- package/api/integration/add-contribution.mjs +1160 -0
- package/api/integration/add-contribution.test.mjs +733 -0
- package/api/integration/add-helpers.d.mts +80 -0
- package/api/integration/add-helpers.mjs +333 -0
- package/api/integration/add-helpers.test.mjs +161 -0
- package/api/integration/add-theme.d.mts +10 -0
- package/api/integration/add-theme.mjs +332 -0
- package/api/integration/add-theme.test.mjs +194 -0
- package/api/integration/integration-authoring.type.d.mts +55 -0
- package/api/integration/integration-authoring.type.mjs +76 -0
- package/api/integration/integration-block-exports.test.mjs +4 -0
- package/api/integration/integrationAdd.doc.d.mts +5 -0
- package/api/integration/integrationAdd.doc.mjs +100 -0
- package/api/integration/integrationAddAgentDoc.doc.d.mts +5 -0
- package/api/integration/integrationAddAgentDoc.doc.mjs +49 -0
- package/api/integration/integrationAddCodemod.doc.d.mts +5 -0
- package/api/integration/integrationAddCodemod.doc.mjs +55 -0
- package/api/integration/integrationAddComponent.doc.d.mts +5 -0
- package/api/integration/integrationAddComponent.doc.mjs +49 -0
- package/api/integration/integrationAddDoc.doc.d.mts +5 -0
- package/api/integration/integrationAddDoc.doc.mjs +59 -0
- package/api/integration/integrationAddTemplate.doc.d.mts +5 -0
- package/api/integration/integrationAddTemplate.doc.mjs +55 -0
- package/api/integration/integrationAddTheme.doc.d.mts +5 -0
- package/api/integration/integrationAddTheme.doc.mjs +85 -0
- package/api/integration/integrationPackCheck.doc.d.mts +5 -0
- package/api/integration/integrationPackCheck.doc.mjs +35 -0
- package/api/integration/pack-check.d.mts +27 -0
- package/api/integration/pack-check.mjs +795 -0
- package/api/integration/pack-check.test.mjs +569 -0
- package/api/integration/pack-check.type.d.mts +45 -0
- package/api/integration/pack-check.type.mjs +56 -0
- package/api/integration/public-api.test.mjs +21 -0
- package/api/integration/validate-integration.mjs +221 -11
- package/api/integration/validate-integration.test.mjs +189 -5
- package/api/integration/validateIntegration.doc.mjs +2 -2
- package/api/json/index.ts +1 -0
- package/api/search/search.mjs +19 -1
- package/api/theme/_adapter.d.mts +26 -28
- package/api/theme/_adapter.mjs +101 -38
- package/api/theme/add/add.d.mts +2 -1
- package/api/theme/add/add.mjs +56 -20
- package/api/theme/add/add.test.mjs +1 -0
- package/api/theme/build/build.d.mts +25 -0
- package/api/theme/build/build.mjs +101 -70
- package/api/theme/build/build.test.mjs +77 -44
- package/api/theme/integration-themes.test.mjs +183 -0
- package/api/theme/list/list.d.mts +10 -2
- package/api/theme/list/list.mjs +31 -10
- package/api/theme/list/list.test.mjs +102 -23
- package/api/theme/listThemes.doc.d.mts +1 -5
- package/api/theme/listThemes.doc.mjs +6 -13
- package/api/theme/targets/targets.test.mjs +22 -8
- package/api/theme/theme.d.mts +1 -1
- package/api/theme/theme.mjs +1 -1
- package/api/theme/theme.type.d.mts +9 -0
- package/api/theme/theme.type.mjs +3 -1
- package/api/theme/themeAdd.doc.d.mts +1 -4
- package/api/theme/themeAdd.doc.mjs +31 -31
- package/api/theme/themeBuild.doc.mjs +1 -1
- package/api/theme/themeList.doc.d.mts +1 -4
- package/api/theme/themeList.doc.mjs +7 -15
- package/api/theme/themeListAvailable.doc.d.mts +8 -0
- package/api/theme/themeListAvailable.doc.mjs +57 -0
- package/api/theme/themeTargets.doc.mjs +21 -7
- package/api/upgrade/_adapter.d.mts +2 -0
- package/api/upgrade/_adapter.mjs +1 -0
- package/api/upgrade/registry/registry.d.mts +40 -0
- package/api/upgrade/registry/registry.mjs +1154 -0
- package/api/upgrade/registry/registry.test.mjs +826 -0
- package/api/upgrade/run/run.mjs +49 -8
- package/api/upgrade/status/status.d.mts +6 -4
- package/api/upgrade/status/status.mjs +26 -8
- package/api/upgrade/upgrade.d.mts +2 -2
- package/api/upgrade/upgrade.doc.mjs +33 -9
- package/api/upgrade/upgrade.mjs +34 -7
- package/api/upgrade/upgrade.test.mjs +198 -8
- package/api/upgrade/upgrade.type.d.mts +90 -1
- package/api/upgrade/upgrade.type.mjs +51 -2
- package/assets/docs/cli-integrations.doc.mjs +152 -28
- package/assets/docs/getting-started.doc.mjs +1 -1
- package/assets/docs/{shadcn-compatibility.doc.mjs → shadcn-compatibility.doc.draft.mjs} +23 -13
- package/assets/docs/styling.doc.mjs +3 -3
- package/assets/docs/theme.doc.dense.mjs +2 -2
- package/assets/docs/theme.doc.mjs +23 -13
- package/assets/docs/theme.doc.zh.mjs +1 -1
- package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadCustomSearch.doc.mjs +2 -1
- package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadCustomSearch.tsx +38 -12
- package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadShowcase.doc.mjs +15 -0
- package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadShowcase.tsx +87 -0
- package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.doc.mjs +23 -0
- package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.tsx +75 -0
- package/assets/templates/blocks/components/BreadcrumbItem/BreadcrumbItemShowcase.tsx +5 -19
- package/assets/templates/blocks/components/Center/CenterHorizontal.doc.mjs +3 -2
- package/assets/templates/blocks/components/Center/CenterHorizontal.tsx +4 -5
- package/assets/templates/blocks/components/Toast/ToastAction.tsx +2 -1
- package/assets/templates/blocks/components/Toast/ToastDeduplication.tsx +2 -1
- package/assets/templates/blocks/components/Toast/ToastDismiss.tsx +2 -1
- package/assets/templates/blocks/components/Toast/ToastShowcase.tsx +2 -1
- package/assets/templates/blocks/components/Toast/ToastStacking.tsx +2 -1
- package/assets/templates/blocks/components/Toast/ToastTypes.tsx +2 -1
- package/assets/templates/pages/dashboard/template.doc.mjs +2 -2
- package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +3 -2
- package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +3 -2
- package/assets/templates/pages/dashboard-comparison/template.doc.mjs +3 -3
- package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -2
- package/assets/templates/pages/dashboard-progress/template.doc.mjs +2 -2
- package/assets/templates/pages/dashboard-scorecard/template.doc.mjs +2 -2
- package/assets/templates/themes/butter/butterTheme.ts +3 -3
- package/assets/templates/themes/neutral/neutralTheme.ts +2 -2
- package/assets/templates/themes/stone/stoneTheme.ts +3 -3
- package/assets/theme.template.ts +4 -7
- package/authoring/config/config.doc.mjs +9 -1
- package/authoring/config/parse.d.mts +2 -0
- package/authoring/config/parse.mjs +28 -3
- package/authoring/config/parse.test.mjs +25 -3
- package/authoring/config/type.ts +3 -0
- package/authoring/doctypes/_schema.d.mts +3 -0
- package/authoring/doctypes/_schema.mjs +6 -1
- package/authoring/doctypes/base/type.ts +6 -6
- package/authoring/doctypes/component/component.doc.mjs +6 -0
- package/authoring/doctypes/component/type.ts +2 -0
- package/authoring/gap-report/parse.d.mts +63 -0
- package/authoring/gap-report/parse.mjs +121 -0
- package/authoring/gap-report/parse.test.mjs +172 -0
- package/authoring/gap-report/type.ts +107 -0
- package/authoring/index.d.mts +1 -0
- package/authoring/index.d.ts +12 -0
- package/authoring/index.mjs +4 -0
- package/authoring/integration/integration.doc.mjs +14 -2
- package/authoring/integration/parse.test.mjs +9 -4
- package/authoring/integration/schema.d.mts +2 -0
- package/authoring/integration/schema.mjs +1 -0
- package/authoring/integration/type.ts +23 -6
- package/authoring/shadcn/receipt.d.mts +219 -0
- package/authoring/shadcn/receipt.mjs +248 -0
- package/authoring/shadcn/receipt.test.mjs +187 -0
- package/authoring/shadcn/source-variants.d.mts +28 -0
- package/authoring/shadcn/source-variants.mjs +176 -0
- package/authoring/shadcn/source-variants.test.mjs +120 -0
- package/clients/cli/commands/build-theme.mjs +54 -19
- package/clients/cli/commands/build-theme.variants.test.mjs +15 -12
- package/clients/cli/commands/component/index.mjs +1 -1
- package/clients/cli/commands/docs.test.mjs +1 -0
- package/clients/cli/commands/doctor-integration.doc.mjs +1 -1
- package/clients/cli/commands/gap-report.doc.mjs +73 -0
- package/clients/cli/commands/gap-report.mjs +114 -0
- package/clients/cli/commands/gap-report.test.mjs +198 -0
- package/clients/cli/commands/import-hint-correctness.test.mjs +88 -0
- package/clients/cli/commands/integration-add.doc.mjs +88 -0
- package/clients/cli/commands/integration-authoring.test.mjs +177 -0
- package/clients/cli/commands/integration-pack.doc.mjs +34 -0
- package/clients/cli/commands/integration-real-world.test.mjs +618 -0
- package/clients/cli/commands/integration.doc.mjs +28 -0
- package/clients/cli/commands/integration.mjs +153 -0
- package/clients/cli/commands/theme-add.doc.mjs +11 -6
- package/clients/cli/commands/theme-list.doc.mjs +21 -5
- package/clients/cli/commands/theme-targets.behavior.test.mjs +19 -2
- package/clients/cli/commands/theme.doc.mjs +2 -2
- package/clients/cli/commands/upgrade.doc.mjs +17 -4
- package/clients/cli/commands/upgrade.mjs +12 -1
- package/clients/cli/commands/upgrade.test.mjs +41 -0
- package/clients/cli/index.mjs +5 -0
- package/clients/cli/lib/component-format.mjs +42 -26
- package/clients/cli/lib/component-format.test.mjs +34 -3
- package/clients/cli/lib/manifest.mjs +12 -0
- package/foundation/config/project-themes.test.mjs +127 -0
- package/foundation/config/project.d.mts +23 -4
- package/foundation/config/project.mjs +96 -9
- package/foundation/config/project.test.mjs +132 -6
- package/foundation/discovery/component-discovery.d.mts +36 -0
- package/foundation/discovery/component-discovery.mjs +96 -0
- package/foundation/discovery/theme-discovery.d.mts +39 -0
- package/foundation/discovery/theme-discovery.mjs +650 -0
- package/foundation/discovery/theme-discovery.test.mjs +375 -0
- package/foundation/discovery/theming-targets.d.mts +16 -0
- package/foundation/discovery/theming-targets.mjs +39 -3
- package/foundation/discovery/theming-targets.test.mjs +139 -0
- package/foundation/integrations/autolink.d.mts +96 -0
- package/foundation/integrations/autolink.mjs +265 -0
- package/foundation/integrations/autolink.test.mjs +430 -0
- package/foundation/integrations/contribution-inventory.d.mts +152 -0
- package/foundation/integrations/contribution-inventory.mjs +497 -0
- package/foundation/integrations/contribution-inventory.test.mjs +361 -0
- package/foundation/integrations/integrations.d.mts +73 -9
- package/foundation/integrations/integrations.mjs +158 -8
- package/foundation/integrations/integrations.test.mjs +152 -29
- package/foundation/integrations/manifest-writer.d.mts +56 -0
- package/foundation/integrations/manifest-writer.mjs +328 -0
- package/foundation/integrations/manifest-writer.test.mjs +229 -0
- package/foundation/integrations/validate-contributions.mjs +81 -6
- package/foundation/response/error-codes.d.mts +4 -1
- package/foundation/response/error-codes.doc.mjs +16 -1
- package/foundation/response/error-codes.mjs +10 -1
- package/foundation/response/response-types.doc.mjs +23 -2
- package/package.json +8 -1
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/** @param {string} value */
|
|
5
|
+
export function projectPath(value: string): string;
|
|
6
|
+
/** @param {string} startDir */
|
|
7
|
+
export function findPackageDir(startDir: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} WritePlan
|
|
10
|
+
* @property {string} path
|
|
11
|
+
* @property {string} contents
|
|
12
|
+
* @property {boolean} createOnly
|
|
13
|
+
* @property {Buffer} [expectedOriginal] bytes captured before validation;
|
|
14
|
+
* a different current file is a concurrent edit and must never be overwritten
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {object} PackageJsonUpdate
|
|
18
|
+
* @property {string} contents
|
|
19
|
+
* @property {Buffer} expectedOriginal
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {object} PackageExportEntry
|
|
23
|
+
* @property {string} subpath package export key, e.g. `./components/Card.tsx`
|
|
24
|
+
* @property {string} target package-relative target, e.g. `./components/Card.tsx`
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Compute one package.json update for an existing `files` allowlist and existing
|
|
28
|
+
* `exports` map. Neither field is created: adding an exports map to a package
|
|
29
|
+
* that has none would make every previously-open deep import private.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} packageFile
|
|
32
|
+
* @param {string[]} entries project-relative files or directories
|
|
33
|
+
* @param {PackageExportEntry[]} [publicExports]
|
|
34
|
+
* @returns {PackageJsonUpdate|null}
|
|
35
|
+
*/
|
|
36
|
+
export function packageJsonFilesUpdate(packageFile: string, entries: string[], publicExports?: PackageExportEntry[]): PackageJsonUpdate | null;
|
|
37
|
+
/**
|
|
38
|
+
* Include one contribution root, the manifest, and any public source entries in
|
|
39
|
+
* existing package allowlists.
|
|
40
|
+
* @param {string} packageFile
|
|
41
|
+
* @param {string} rootPath
|
|
42
|
+
* @param {string} manifestName
|
|
43
|
+
* @param {PackageExportEntry[]} [publicExports]
|
|
44
|
+
* @returns {PackageJsonUpdate|null}
|
|
45
|
+
*/
|
|
46
|
+
export function packageJsonUpdate(packageFile: string, rootPath: string, manifestName: string, publicExports?: PackageExportEntry[]): PackageJsonUpdate | null;
|
|
47
|
+
/**
|
|
48
|
+
* Apply several writes as one operation. New files publish with an atomic
|
|
49
|
+
* hard link so a racing creator is never overwritten; replacements are
|
|
50
|
+
* rejected if their bytes changed after preflight or if the path is a symlink.
|
|
51
|
+
* Returns a rollback function for a later step in the same transaction.
|
|
52
|
+
*
|
|
53
|
+
* @param {WritePlan[]} plans
|
|
54
|
+
* @returns {() => void}
|
|
55
|
+
*/
|
|
56
|
+
export function applyWrites(plans: WritePlan[]): () => void;
|
|
57
|
+
export type WritePlan = {
|
|
58
|
+
path: string;
|
|
59
|
+
contents: string;
|
|
60
|
+
createOnly: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* bytes captured before validation;
|
|
63
|
+
* a different current file is a concurrent edit and must never be overwritten
|
|
64
|
+
*/
|
|
65
|
+
expectedOriginal?: Buffer<ArrayBufferLike> | undefined;
|
|
66
|
+
};
|
|
67
|
+
export type PackageJsonUpdate = {
|
|
68
|
+
contents: string;
|
|
69
|
+
expectedOriginal: Buffer;
|
|
70
|
+
};
|
|
71
|
+
export type PackageExportEntry = {
|
|
72
|
+
/**
|
|
73
|
+
* package export key, e.g. `./components/Card.tsx`
|
|
74
|
+
*/
|
|
75
|
+
subpath: string;
|
|
76
|
+
/**
|
|
77
|
+
* package-relative target, e.g. `./components/Card.tsx`
|
|
78
|
+
*/
|
|
79
|
+
target: string;
|
|
80
|
+
};
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Shared helpers for integration contribution writers.
|
|
5
|
+
*
|
|
6
|
+
* Hosts the atomic staged-write transaction, package.json files-array
|
|
7
|
+
* maintenance, package-dir resolution, and project-path normalization that
|
|
8
|
+
* every `integration add <kind>` command needs. Stateless and side-effect-free
|
|
9
|
+
* outside of {@link applyWrites}.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as fs from 'node:fs';
|
|
13
|
+
import * as path from 'node:path';
|
|
14
|
+
import {randomUUID} from 'node:crypto';
|
|
15
|
+
import {AstryxError} from '../error.mjs';
|
|
16
|
+
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
17
|
+
|
|
18
|
+
/** @param {string} value */
|
|
19
|
+
export function projectPath(value) {
|
|
20
|
+
return value.split(path.sep).join('/');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @param {string} startDir */
|
|
24
|
+
export function findPackageDir(startDir) {
|
|
25
|
+
let dir = path.resolve(startDir);
|
|
26
|
+
for (let depth = 0; depth < 50; depth++) {
|
|
27
|
+
if (fs.existsSync(path.join(dir, 'package.json'))) return dir;
|
|
28
|
+
const parent = path.dirname(dir);
|
|
29
|
+
if (parent === dir) break;
|
|
30
|
+
dir = parent;
|
|
31
|
+
}
|
|
32
|
+
throw new AstryxError(
|
|
33
|
+
'No package.json found. Run this command inside an integration package.',
|
|
34
|
+
undefined,
|
|
35
|
+
ERROR_CODES.ERR_FILE_NOT_FOUND,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @typedef {object} WritePlan
|
|
41
|
+
* @property {string} path
|
|
42
|
+
* @property {string} contents
|
|
43
|
+
* @property {boolean} createOnly
|
|
44
|
+
* @property {Buffer} [expectedOriginal] bytes captured before validation;
|
|
45
|
+
* a different current file is a concurrent edit and must never be overwritten
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @typedef {object} PackageJsonUpdate
|
|
50
|
+
* @property {string} contents
|
|
51
|
+
* @property {Buffer} expectedOriginal
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {object} PackageExportEntry
|
|
56
|
+
* @property {string} subpath package export key, e.g. `./components/Card.tsx`
|
|
57
|
+
* @property {string} target package-relative target, e.g. `./components/Card.tsx`
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Compute one package.json update for an existing `files` allowlist and existing
|
|
62
|
+
* `exports` map. Neither field is created: adding an exports map to a package
|
|
63
|
+
* that has none would make every previously-open deep import private.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} packageFile
|
|
66
|
+
* @param {string[]} entries project-relative files or directories
|
|
67
|
+
* @param {PackageExportEntry[]} [publicExports]
|
|
68
|
+
* @returns {PackageJsonUpdate|null}
|
|
69
|
+
*/
|
|
70
|
+
export function packageJsonFilesUpdate(
|
|
71
|
+
packageFile,
|
|
72
|
+
entries,
|
|
73
|
+
publicExports = [],
|
|
74
|
+
) {
|
|
75
|
+
const expectedOriginal = fs.readFileSync(packageFile);
|
|
76
|
+
const original = expectedOriginal.toString('utf-8');
|
|
77
|
+
/** @type {any} */
|
|
78
|
+
let pkg;
|
|
79
|
+
try {
|
|
80
|
+
pkg = JSON.parse(original);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
83
|
+
throw new AstryxError(
|
|
84
|
+
`Cannot update package.json: ${message}`,
|
|
85
|
+
undefined,
|
|
86
|
+
ERROR_CODES.ERR_INVALID_ARGUMENT,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const normalize = (/** @type {string} */ value) =>
|
|
91
|
+
projectPath(value).replace(/^\.\//u, '').replace(/\/$/u, '');
|
|
92
|
+
let changed = false;
|
|
93
|
+
|
|
94
|
+
if (pkg.files !== undefined) {
|
|
95
|
+
if (
|
|
96
|
+
!Array.isArray(pkg.files) ||
|
|
97
|
+
pkg.files.some((/** @type {unknown} */ item) => typeof item !== 'string')
|
|
98
|
+
) {
|
|
99
|
+
throw new AstryxError(
|
|
100
|
+
'package.json files must be an array of strings before Astryx can add the contribution root.',
|
|
101
|
+
undefined,
|
|
102
|
+
ERROR_CODES.ERR_INVALID_ARGUMENT,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
for (const item of entries.map(normalize)) {
|
|
106
|
+
if (
|
|
107
|
+
pkg.files.some(
|
|
108
|
+
(/** @type {string} */ value) => normalize(value) === item,
|
|
109
|
+
)
|
|
110
|
+
) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
pkg.files.push(item);
|
|
114
|
+
changed = true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (pkg.exports !== undefined && publicExports.length > 0) {
|
|
119
|
+
const authored = pkg.exports;
|
|
120
|
+
/** @type {Record<string, unknown>} */
|
|
121
|
+
let exportsMap;
|
|
122
|
+
if (
|
|
123
|
+
authored == null ||
|
|
124
|
+
typeof authored !== 'object' ||
|
|
125
|
+
Array.isArray(authored)
|
|
126
|
+
) {
|
|
127
|
+
exportsMap = {'.': authored};
|
|
128
|
+
} else {
|
|
129
|
+
const keys = Object.keys(authored);
|
|
130
|
+
const hasSubpaths = keys.some(key => key.startsWith('.'));
|
|
131
|
+
const hasConditions = keys.some(key => !key.startsWith('.'));
|
|
132
|
+
if (hasSubpaths && hasConditions) {
|
|
133
|
+
throw new AstryxError(
|
|
134
|
+
'package.json exports mixes subpaths and conditions, so Astryx cannot add a contribution export safely.',
|
|
135
|
+
undefined,
|
|
136
|
+
ERROR_CODES.ERR_INTEGRATION_EXPORT_CONFLICT,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
exportsMap = hasConditions ? {'.': authored} : {...authored};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
for (const {subpath, target} of publicExports) {
|
|
143
|
+
const key = `./${normalize(subpath)}`;
|
|
144
|
+
const value = `./${normalize(target)}`;
|
|
145
|
+
if (Object.prototype.hasOwnProperty.call(exportsMap, key)) {
|
|
146
|
+
if (exportsMap[key] !== value) {
|
|
147
|
+
throw new AstryxError(
|
|
148
|
+
`package.json exports already maps "${key}" to a different target; refusing to replace it with "${value}".`,
|
|
149
|
+
undefined,
|
|
150
|
+
ERROR_CODES.ERR_INTEGRATION_EXPORT_CONFLICT,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
exportsMap[key] = value;
|
|
156
|
+
changed = true;
|
|
157
|
+
}
|
|
158
|
+
if (changed || exportsMap !== authored) pkg.exports = exportsMap;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!changed) return null;
|
|
162
|
+
const indent = original.match(/\n([ \t]+)"/u)?.[1] ?? ' ';
|
|
163
|
+
return {
|
|
164
|
+
contents:
|
|
165
|
+
JSON.stringify(pkg, null, indent) + (original.endsWith('\n') ? '\n' : ''),
|
|
166
|
+
expectedOriginal,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Include one contribution root, the manifest, and any public source entries in
|
|
172
|
+
* existing package allowlists.
|
|
173
|
+
* @param {string} packageFile
|
|
174
|
+
* @param {string} rootPath
|
|
175
|
+
* @param {string} manifestName
|
|
176
|
+
* @param {PackageExportEntry[]} [publicExports]
|
|
177
|
+
* @returns {PackageJsonUpdate|null}
|
|
178
|
+
*/
|
|
179
|
+
export function packageJsonUpdate(
|
|
180
|
+
packageFile,
|
|
181
|
+
rootPath,
|
|
182
|
+
manifestName,
|
|
183
|
+
publicExports = [],
|
|
184
|
+
) {
|
|
185
|
+
return packageJsonFilesUpdate(
|
|
186
|
+
packageFile,
|
|
187
|
+
[rootPath, manifestName],
|
|
188
|
+
publicExports,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ── Atomic staged-write transaction ─────────────────────────────────
|
|
193
|
+
|
|
194
|
+
/** @param {string} file */
|
|
195
|
+
function removeTemporary(file) {
|
|
196
|
+
try {
|
|
197
|
+
fs.rmSync(file, {force: true});
|
|
198
|
+
} catch {
|
|
199
|
+
// Best effort. The transaction error remains the actionable failure.
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Restore writes that already published. Best-effort so callers preserve
|
|
205
|
+
* the original actionable error.
|
|
206
|
+
* @param {Array<WritePlan & {temporary: string, original: Buffer|null, mode: number}>} published
|
|
207
|
+
*/
|
|
208
|
+
function rollbackWrites(published) {
|
|
209
|
+
for (const plan of [...published].reverse()) {
|
|
210
|
+
let restore = null;
|
|
211
|
+
try {
|
|
212
|
+
const stat = fs.lstatSync(plan.path);
|
|
213
|
+
if (stat.isSymbolicLink()) continue;
|
|
214
|
+
const current = fs.readFileSync(plan.path);
|
|
215
|
+
if (!current.equals(Buffer.from(plan.contents))) continue;
|
|
216
|
+
if (plan.original == null) {
|
|
217
|
+
fs.rmSync(plan.path, {force: true});
|
|
218
|
+
} else {
|
|
219
|
+
restore = path.join(
|
|
220
|
+
path.dirname(plan.path),
|
|
221
|
+
`.${path.basename(plan.path)}.restore-${randomUUID()}`,
|
|
222
|
+
);
|
|
223
|
+
fs.writeFileSync(restore, plan.original, {mode: plan.mode});
|
|
224
|
+
fs.renameSync(restore, plan.path);
|
|
225
|
+
restore = null;
|
|
226
|
+
}
|
|
227
|
+
} catch {
|
|
228
|
+
// Best effort. A concurrent edit belongs to its writer, not this rollback.
|
|
229
|
+
} finally {
|
|
230
|
+
if (restore != null) removeTemporary(restore);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** @param {string} file */
|
|
236
|
+
function assertNotSymlink(file) {
|
|
237
|
+
let stat;
|
|
238
|
+
try {
|
|
239
|
+
stat = fs.lstatSync(file);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
if (
|
|
242
|
+
error instanceof Error &&
|
|
243
|
+
/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT'
|
|
244
|
+
) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
if (stat.isSymbolicLink()) {
|
|
250
|
+
throw new AstryxError(
|
|
251
|
+
`Refusing to replace symlinked file ${file}.`,
|
|
252
|
+
undefined,
|
|
253
|
+
ERROR_CODES.ERR_PATH_TRAVERSAL,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Apply several writes as one operation. New files publish with an atomic
|
|
260
|
+
* hard link so a racing creator is never overwritten; replacements are
|
|
261
|
+
* rejected if their bytes changed after preflight or if the path is a symlink.
|
|
262
|
+
* Returns a rollback function for a later step in the same transaction.
|
|
263
|
+
*
|
|
264
|
+
* @param {WritePlan[]} plans
|
|
265
|
+
* @returns {() => void}
|
|
266
|
+
*/
|
|
267
|
+
export function applyWrites(plans) {
|
|
268
|
+
/** @type {Array<WritePlan & {temporary: string, original: Buffer|null, mode: number}>} */
|
|
269
|
+
const staged = [];
|
|
270
|
+
/** @type {typeof staged} */
|
|
271
|
+
const published = [];
|
|
272
|
+
try {
|
|
273
|
+
for (const plan of plans) {
|
|
274
|
+
fs.mkdirSync(path.dirname(plan.path), {recursive: true});
|
|
275
|
+
assertNotSymlink(plan.path);
|
|
276
|
+
const exists = fs.existsSync(plan.path);
|
|
277
|
+
if (plan.createOnly && exists) {
|
|
278
|
+
throw new AstryxError(
|
|
279
|
+
`Refusing to overwrite existing file ${plan.path}.`,
|
|
280
|
+
undefined,
|
|
281
|
+
ERROR_CODES.ERR_FILE_EXISTS,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
const original = exists ? fs.readFileSync(plan.path) : null;
|
|
285
|
+
if (
|
|
286
|
+
plan.expectedOriginal != null &&
|
|
287
|
+
(original == null || !original.equals(plan.expectedOriginal))
|
|
288
|
+
) {
|
|
289
|
+
throw new Error(`File changed while preparing: ${plan.path}`);
|
|
290
|
+
}
|
|
291
|
+
const mode = exists ? fs.statSync(plan.path).mode : 0o666;
|
|
292
|
+
const temporary = path.join(
|
|
293
|
+
path.dirname(plan.path),
|
|
294
|
+
`.${path.basename(plan.path)}.tmp-${randomUUID()}`,
|
|
295
|
+
);
|
|
296
|
+
let descriptor;
|
|
297
|
+
try {
|
|
298
|
+
descriptor = fs.openSync(temporary, 'wx', mode);
|
|
299
|
+
try {
|
|
300
|
+
fs.writeFileSync(descriptor, plan.contents, 'utf-8');
|
|
301
|
+
} finally {
|
|
302
|
+
fs.closeSync(descriptor);
|
|
303
|
+
}
|
|
304
|
+
} catch (error) {
|
|
305
|
+
removeTemporary(temporary);
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
staged.push({...plan, temporary, original, mode});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
for (const plan of staged) {
|
|
312
|
+
if (plan.original == null) {
|
|
313
|
+
fs.linkSync(plan.temporary, plan.path);
|
|
314
|
+
published.push(plan);
|
|
315
|
+
removeTemporary(plan.temporary);
|
|
316
|
+
continue;
|
|
317
|
+
} else {
|
|
318
|
+
assertNotSymlink(plan.path);
|
|
319
|
+
const current = fs.readFileSync(plan.path);
|
|
320
|
+
if (!current.equals(plan.original)) {
|
|
321
|
+
throw new Error(`File changed while writing: ${plan.path}`);
|
|
322
|
+
}
|
|
323
|
+
fs.renameSync(plan.temporary, plan.path);
|
|
324
|
+
}
|
|
325
|
+
published.push(plan);
|
|
326
|
+
}
|
|
327
|
+
return () => rollbackWrites(published);
|
|
328
|
+
} catch (error) {
|
|
329
|
+
for (const plan of staged) removeTemporary(plan.temporary);
|
|
330
|
+
rollbackWrites(published);
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
|
4
|
+
import * as fs from 'node:fs';
|
|
5
|
+
import * as path from 'node:path';
|
|
6
|
+
import {applyWrites, packageJsonFilesUpdate} from './add-helpers.mjs';
|
|
7
|
+
|
|
8
|
+
let tmpDir;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-add-helpers-'));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
fs.rmSync(tmpDir, {recursive: true, force: true});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('applyWrites', () => {
|
|
19
|
+
it('refuses to replace a symlink and preserves its target', () => {
|
|
20
|
+
const target = path.join(tmpDir, 'target.json');
|
|
21
|
+
const link = path.join(tmpDir, 'manifest.json');
|
|
22
|
+
fs.writeFileSync(target, '{"owner":"author"}\n');
|
|
23
|
+
fs.symlinkSync(target, link);
|
|
24
|
+
|
|
25
|
+
expect(() =>
|
|
26
|
+
applyWrites([
|
|
27
|
+
{
|
|
28
|
+
path: link,
|
|
29
|
+
contents: '{"owner":"agent"}\n',
|
|
30
|
+
createOnly: false,
|
|
31
|
+
},
|
|
32
|
+
]),
|
|
33
|
+
).toThrow(/Refusing to replace symlinked file/);
|
|
34
|
+
expect(fs.lstatSync(link).isSymbolicLink()).toBe(true);
|
|
35
|
+
expect(fs.readFileSync(target, 'utf-8')).toBe('{"owner":"author"}\n');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('rolls back only bytes still owned by the transaction', () => {
|
|
39
|
+
const created = path.join(tmpDir, 'created.txt');
|
|
40
|
+
const replaced = path.join(tmpDir, 'replaced.txt');
|
|
41
|
+
fs.writeFileSync(replaced, 'before\n');
|
|
42
|
+
|
|
43
|
+
const rollback = applyWrites([
|
|
44
|
+
{path: created, contents: 'created-by-call\n', createOnly: true},
|
|
45
|
+
{path: replaced, contents: 'replaced-by-call\n', createOnly: false},
|
|
46
|
+
]);
|
|
47
|
+
fs.writeFileSync(created, 'concurrent-created\n');
|
|
48
|
+
fs.writeFileSync(replaced, 'concurrent-replaced\n');
|
|
49
|
+
|
|
50
|
+
rollback();
|
|
51
|
+
|
|
52
|
+
expect(fs.readFileSync(created, 'utf-8')).toBe('concurrent-created\n');
|
|
53
|
+
expect(fs.readFileSync(replaced, 'utf-8')).toBe('concurrent-replaced\n');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('removes created files and restores replacements when bytes are unchanged', () => {
|
|
57
|
+
const created = path.join(tmpDir, 'created.txt');
|
|
58
|
+
const replaced = path.join(tmpDir, 'replaced.txt');
|
|
59
|
+
fs.writeFileSync(replaced, 'before\n');
|
|
60
|
+
|
|
61
|
+
const rollback = applyWrites([
|
|
62
|
+
{path: created, contents: 'created-by-call\n', createOnly: true},
|
|
63
|
+
{path: replaced, contents: 'replaced-by-call\n', createOnly: false},
|
|
64
|
+
]);
|
|
65
|
+
rollback();
|
|
66
|
+
|
|
67
|
+
expect(fs.existsSync(created)).toBe(false);
|
|
68
|
+
expect(fs.readFileSync(replaced, 'utf-8')).toBe('before\n');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('refuses a file changed after validation and preserves the new bytes', () => {
|
|
72
|
+
const file = path.join(tmpDir, 'manifest.json');
|
|
73
|
+
const validated = Buffer.from('{"themes":[]}\n');
|
|
74
|
+
fs.writeFileSync(file, validated);
|
|
75
|
+
fs.writeFileSync(file, '{"themes":[{"slug":"concurrent"}]}\n');
|
|
76
|
+
|
|
77
|
+
expect(() =>
|
|
78
|
+
applyWrites([
|
|
79
|
+
{
|
|
80
|
+
path: file,
|
|
81
|
+
contents: '{"themes":[{"slug":"mine"}]}\n',
|
|
82
|
+
createOnly: false,
|
|
83
|
+
expectedOriginal: validated,
|
|
84
|
+
},
|
|
85
|
+
]),
|
|
86
|
+
).toThrow(/changed while preparing/);
|
|
87
|
+
expect(fs.readFileSync(file, 'utf-8')).toContain('concurrent');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('packageJsonFilesUpdate', () => {
|
|
92
|
+
it('never creates files when the package has no allowlist', () => {
|
|
93
|
+
const file = path.join(tmpDir, 'package.json');
|
|
94
|
+
fs.writeFileSync(file, '{"name":"fixture"}\n');
|
|
95
|
+
expect(packageJsonFilesUpdate(file, ['astryx.integration.mjs'])).toBeNull();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('never creates exports when the package has no exports field', () => {
|
|
99
|
+
const file = path.join(tmpDir, 'package.json');
|
|
100
|
+
fs.writeFileSync(file, '{"name":"fixture"}\n');
|
|
101
|
+
expect(
|
|
102
|
+
packageJsonFilesUpdate(
|
|
103
|
+
file,
|
|
104
|
+
[],
|
|
105
|
+
[{subpath: 'components/Card.tsx', target: 'components/Card.tsx'}],
|
|
106
|
+
),
|
|
107
|
+
).toBeNull();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('expands exports sugar and appends an exact source subpath', () => {
|
|
111
|
+
const file = path.join(tmpDir, 'package.json');
|
|
112
|
+
fs.writeFileSync(
|
|
113
|
+
file,
|
|
114
|
+
`${JSON.stringify({name: 'fixture', exports: './index.mjs'}, null, 2)}\n`,
|
|
115
|
+
);
|
|
116
|
+
const next = packageJsonFilesUpdate(
|
|
117
|
+
file,
|
|
118
|
+
[],
|
|
119
|
+
[{subpath: 'components/Card.tsx', target: 'components/Card.tsx'}],
|
|
120
|
+
);
|
|
121
|
+
expect(JSON.parse(next?.contents ?? '').exports).toEqual({
|
|
122
|
+
'.': './index.mjs',
|
|
123
|
+
'./components/Card.tsx': './components/Card.tsx',
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('refuses to replace an authored export target', () => {
|
|
128
|
+
const file = path.join(tmpDir, 'package.json');
|
|
129
|
+
fs.writeFileSync(
|
|
130
|
+
file,
|
|
131
|
+
`${JSON.stringify({
|
|
132
|
+
name: 'fixture',
|
|
133
|
+
exports: {'./components/Card.tsx': './different.tsx'},
|
|
134
|
+
})}\n`,
|
|
135
|
+
);
|
|
136
|
+
expect(() =>
|
|
137
|
+
packageJsonFilesUpdate(
|
|
138
|
+
file,
|
|
139
|
+
[],
|
|
140
|
+
[{subpath: 'components/Card.tsx', target: 'components/Card.tsx'}],
|
|
141
|
+
),
|
|
142
|
+
).toThrow(/refusing to replace/);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('adds only missing normalized paths to an existing allowlist', () => {
|
|
146
|
+
const file = path.join(tmpDir, 'package.json');
|
|
147
|
+
fs.writeFileSync(
|
|
148
|
+
file,
|
|
149
|
+
`${JSON.stringify({name: 'fixture', files: ['./docs/']}, null, 2)}\n`,
|
|
150
|
+
);
|
|
151
|
+
const next = packageJsonFilesUpdate(file, [
|
|
152
|
+
'./docs',
|
|
153
|
+
'astryx.integration.mjs',
|
|
154
|
+
]);
|
|
155
|
+
expect(JSON.parse(next?.contents ?? '').files).toEqual([
|
|
156
|
+
'./docs/',
|
|
157
|
+
'astryx.integration.mjs',
|
|
158
|
+
]);
|
|
159
|
+
expect(next?.expectedOriginal.toString('utf-8')).toContain('"./docs/"');
|
|
160
|
+
});
|
|
161
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Add one source theme to the local integration package.
|
|
6
|
+
* @param {string} name lowercase kebab-case theme slug
|
|
7
|
+
* @param {import('./integration-authoring.type.mjs').IntegrationAddThemeOptions} [options]
|
|
8
|
+
* @returns {Promise<import('./integration-authoring.type.mjs').IntegrationAddResponse>}
|
|
9
|
+
*/
|
|
10
|
+
export function integrationAddTheme(name: string, options?: import("./integration-authoring.type.mjs").IntegrationAddThemeOptions): Promise<import("./integration-authoring.type.mjs").IntegrationAddResponse>;
|