@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,328 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Shared writer for adding one contribution root to an existing Astryx
|
|
5
|
+
* integration manifest without replacing any author-owned field.
|
|
6
|
+
*
|
|
7
|
+
* The function is intentionally independent of every contribution kind. Theme,
|
|
8
|
+
* component, template, doc, and codemod writers all need the same operation and
|
|
9
|
+
* must not grow separate source-rewrite implementations.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as fs from 'node:fs';
|
|
13
|
+
import * as path from 'node:path';
|
|
14
|
+
import {randomUUID} from 'node:crypto';
|
|
15
|
+
import jscodeshift from 'jscodeshift';
|
|
16
|
+
import {assertWithin} from '../fs/path-safety.mjs';
|
|
17
|
+
import {findManifestPaths, loadManifestObject} from './integrations.mjs';
|
|
18
|
+
|
|
19
|
+
export class IntegrationRootConflictError extends Error {
|
|
20
|
+
/** @param {string} kind @param {string} current @param {string} requested */
|
|
21
|
+
constructor(kind, current, requested) {
|
|
22
|
+
super(
|
|
23
|
+
`Integration manifest already declares ${kind}: "${current}"; refusing to replace it with "${requested}".`,
|
|
24
|
+
);
|
|
25
|
+
this.name = 'IntegrationRootConflictError';
|
|
26
|
+
this.code = 'INTEGRATION_ROOT_CONFLICT';
|
|
27
|
+
this.kind = kind;
|
|
28
|
+
this.current = current;
|
|
29
|
+
this.requested = requested;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Find the one local integration manifest, or null when none exists.
|
|
35
|
+
* @param {string} packageDir
|
|
36
|
+
* @returns {string|null}
|
|
37
|
+
*/
|
|
38
|
+
export function findLocalIntegrationManifestOrNull(packageDir) {
|
|
39
|
+
const manifests = findManifestPaths(packageDir);
|
|
40
|
+
if (manifests.length === 0) return null;
|
|
41
|
+
if (manifests.length > 1) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Multiple integration manifests found (${manifests
|
|
44
|
+
.map(file => path.basename(file))
|
|
45
|
+
.join(', ')}). Keep exactly one.`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return manifests[0];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Find the one local integration manifest.
|
|
53
|
+
* @param {string} packageDir
|
|
54
|
+
* @returns {string}
|
|
55
|
+
*/
|
|
56
|
+
export function findLocalIntegrationManifest(packageDir) {
|
|
57
|
+
const manifest = findLocalIntegrationManifestOrNull(packageDir);
|
|
58
|
+
if (manifest == null) {
|
|
59
|
+
throw new Error('No astryx.integration.{ts,mjs,js} file found.');
|
|
60
|
+
}
|
|
61
|
+
return manifest;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** @param {any} node */
|
|
65
|
+
export function unwrapManifestExpression(node) {
|
|
66
|
+
let current = node;
|
|
67
|
+
while (
|
|
68
|
+
current &&
|
|
69
|
+
[
|
|
70
|
+
'TSAsExpression',
|
|
71
|
+
'TSSatisfiesExpression',
|
|
72
|
+
'TypeCastExpression',
|
|
73
|
+
'ParenthesizedExpression',
|
|
74
|
+
].includes(current.type)
|
|
75
|
+
) {
|
|
76
|
+
current = current.expression;
|
|
77
|
+
}
|
|
78
|
+
return current;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** @param {any} property @param {string} name */
|
|
82
|
+
function propertyNamed(property, name) {
|
|
83
|
+
if (
|
|
84
|
+
!property ||
|
|
85
|
+
!['ObjectProperty', 'Property'].includes(property.type) ||
|
|
86
|
+
property.computed
|
|
87
|
+
) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return (
|
|
91
|
+
(property.key?.type === 'Identifier' && property.key.name === name) ||
|
|
92
|
+
(['Literal', 'StringLiteral'].includes(property.key?.type) &&
|
|
93
|
+
property.key.value === name)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Find the object literal an integration manifest exports.
|
|
99
|
+
* @param {ReturnType<typeof jscodeshift.withParser>} j
|
|
100
|
+
* @param {ReturnType<ReturnType<typeof jscodeshift.withParser>>} ast
|
|
101
|
+
* @returns {any|null}
|
|
102
|
+
*/
|
|
103
|
+
export function findManifestObject(j, ast) {
|
|
104
|
+
const exports = ast.find(j.ExportDefaultDeclaration);
|
|
105
|
+
if (exports.size() !== 1) return null;
|
|
106
|
+
let declaration = unwrapManifestExpression(exports.nodes()[0].declaration);
|
|
107
|
+
|
|
108
|
+
if (j.Identifier.check(declaration)) {
|
|
109
|
+
const bindings = ast.find(j.VariableDeclarator, {
|
|
110
|
+
id: {type: 'Identifier', name: declaration.name},
|
|
111
|
+
});
|
|
112
|
+
if (bindings.size() !== 1) return null;
|
|
113
|
+
declaration = unwrapManifestExpression(bindings.nodes()[0].init);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (j.CallExpression.check(declaration) && declaration.arguments.length > 0) {
|
|
117
|
+
declaration = unwrapManifestExpression(declaration.arguments[0]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return j.ObjectExpression.check(declaration) ? declaration : null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @param {string} file */
|
|
124
|
+
function removeTemporary(file) {
|
|
125
|
+
try {
|
|
126
|
+
fs.rmSync(file, {force: true});
|
|
127
|
+
} catch {
|
|
128
|
+
// Best effort. The transaction error remains the actionable failure.
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Atomically replace one text file while preserving its mode.
|
|
134
|
+
* @param {string} file
|
|
135
|
+
* @param {string} contents
|
|
136
|
+
* @param {string} [expectedContents] bytes read during preflight
|
|
137
|
+
*/
|
|
138
|
+
function replaceFile(file, contents, expectedContents) {
|
|
139
|
+
const expected =
|
|
140
|
+
expectedContents === undefined ? null : Buffer.from(expectedContents);
|
|
141
|
+
const assertUnchanged = () => {
|
|
142
|
+
if (expected != null && !fs.readFileSync(file).equals(expected)) {
|
|
143
|
+
throw new Error(`File changed while writing: ${file}`);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
const temporary = path.join(
|
|
147
|
+
path.dirname(file),
|
|
148
|
+
`.${path.basename(file)}.tmp-${randomUUID()}`,
|
|
149
|
+
);
|
|
150
|
+
let descriptor;
|
|
151
|
+
try {
|
|
152
|
+
assertUnchanged();
|
|
153
|
+
const mode = fs.statSync(file).mode;
|
|
154
|
+
descriptor = fs.openSync(temporary, 'wx', mode);
|
|
155
|
+
fs.writeFileSync(descriptor, contents, 'utf-8');
|
|
156
|
+
fs.closeSync(descriptor);
|
|
157
|
+
descriptor = undefined;
|
|
158
|
+
assertUnchanged();
|
|
159
|
+
fs.renameSync(temporary, file);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (descriptor != null) fs.closeSync(descriptor);
|
|
162
|
+
removeTemporary(temporary);
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Atomically create one text file without replacing a racing writer.
|
|
169
|
+
* @param {string} file
|
|
170
|
+
* @param {string} contents
|
|
171
|
+
*/
|
|
172
|
+
function createFile(file, contents) {
|
|
173
|
+
const temporary = path.join(
|
|
174
|
+
path.dirname(file),
|
|
175
|
+
`.${path.basename(file)}.tmp-${randomUUID()}`,
|
|
176
|
+
);
|
|
177
|
+
let descriptor;
|
|
178
|
+
let linked = false;
|
|
179
|
+
try {
|
|
180
|
+
descriptor = fs.openSync(temporary, 'wx', 0o666);
|
|
181
|
+
fs.writeFileSync(descriptor, contents, 'utf-8');
|
|
182
|
+
fs.closeSync(descriptor);
|
|
183
|
+
descriptor = undefined;
|
|
184
|
+
fs.linkSync(temporary, file);
|
|
185
|
+
linked = true;
|
|
186
|
+
removeTemporary(temporary);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (descriptor != null) fs.closeSync(descriptor);
|
|
189
|
+
if (linked) {
|
|
190
|
+
try {
|
|
191
|
+
removeFileIfUnchanged(file, contents);
|
|
192
|
+
} catch {
|
|
193
|
+
// Preserve a concurrent edit rather than forcing cleanup.
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
removeTemporary(temporary);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @param {string} file @param {string} expectedContents */
|
|
202
|
+
function removeFileIfUnchanged(file, expectedContents) {
|
|
203
|
+
if (fs.lstatSync(file).isSymbolicLink()) {
|
|
204
|
+
throw new Error(`File changed before rollback: ${file}`);
|
|
205
|
+
}
|
|
206
|
+
if (!fs.readFileSync(file).equals(Buffer.from(expectedContents))) {
|
|
207
|
+
throw new Error(`File changed before rollback: ${file}`);
|
|
208
|
+
}
|
|
209
|
+
fs.rmSync(file);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Add a contribution root to an integration manifest, creating the manifest
|
|
214
|
+
* when requested and none exists.
|
|
215
|
+
*
|
|
216
|
+
* If the key already has `rootPath`, this is an idempotent no-op. A different
|
|
217
|
+
* author-owned value is a typed refusal. `dryRun` performs every read and
|
|
218
|
+
* source-shape check but writes nothing. `createIfMissing` creates a minimal
|
|
219
|
+
* `.mjs` manifest as part of the same verified operation.
|
|
220
|
+
*
|
|
221
|
+
* @param {string} packageDir
|
|
222
|
+
* @param {string} kind
|
|
223
|
+
* @param {string} rootPath
|
|
224
|
+
* @param {{dryRun?: boolean, verify?: () => void | Promise<void>, createIfMissing?: boolean}} [options]
|
|
225
|
+
* @returns {Promise<{path: string, created: boolean}>}
|
|
226
|
+
*/
|
|
227
|
+
export async function patchIntegrationRoot(
|
|
228
|
+
packageDir,
|
|
229
|
+
kind,
|
|
230
|
+
rootPath,
|
|
231
|
+
{dryRun = false, verify, createIfMissing = false} = {},
|
|
232
|
+
) {
|
|
233
|
+
if (!/^[A-Za-z_$][\w$]*$/u.test(kind)) {
|
|
234
|
+
throw new Error(`Invalid integration root key "${kind}".`);
|
|
235
|
+
}
|
|
236
|
+
assertWithin(rootPath, packageDir, {label: `${kind} root`});
|
|
237
|
+
|
|
238
|
+
const existingManifest = findLocalIntegrationManifestOrNull(packageDir);
|
|
239
|
+
const manifestExists = existingManifest != null;
|
|
240
|
+
if (!manifestExists && !createIfMissing) {
|
|
241
|
+
throw new Error('No astryx.integration.{ts,mjs,js} file found.');
|
|
242
|
+
}
|
|
243
|
+
const manifestFile =
|
|
244
|
+
existingManifest ?? path.join(packageDir, 'astryx.integration.mjs');
|
|
245
|
+
let writableManifestFile = manifestFile;
|
|
246
|
+
if (manifestExists && fs.lstatSync(manifestFile).isSymbolicLink()) {
|
|
247
|
+
writableManifestFile = assertWithin(
|
|
248
|
+
fs.realpathSync(manifestFile),
|
|
249
|
+
packageDir,
|
|
250
|
+
{
|
|
251
|
+
allowAbsolute: true,
|
|
252
|
+
label: 'integration manifest symlink target',
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** @type {Record<string, unknown>} */
|
|
258
|
+
let manifest = {};
|
|
259
|
+
if (manifestExists) {
|
|
260
|
+
manifest = /** @type {Record<string, unknown>} */ (
|
|
261
|
+
await loadManifestObject(
|
|
262
|
+
manifestFile,
|
|
263
|
+
`Integration manifest ${path.basename(manifestFile)}`,
|
|
264
|
+
{fresh: true},
|
|
265
|
+
)
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
const current = manifest[kind];
|
|
269
|
+
if (current !== undefined) {
|
|
270
|
+
if (current !== rootPath) {
|
|
271
|
+
throw new IntegrationRootConflictError(kind, String(current), rootPath);
|
|
272
|
+
}
|
|
273
|
+
if (!dryRun && verify) await verify();
|
|
274
|
+
return {path: rootPath, created: false};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const source = manifestExists
|
|
278
|
+
? fs.readFileSync(manifestFile, 'utf-8')
|
|
279
|
+
: 'export default {};\n';
|
|
280
|
+
const j = jscodeshift.withParser('tsx');
|
|
281
|
+
let ast;
|
|
282
|
+
try {
|
|
283
|
+
ast = j(source);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
286
|
+
throw new Error(
|
|
287
|
+
`Could not parse ${path.basename(manifestFile)}: ${message}`,
|
|
288
|
+
{cause: error},
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
const object = findManifestObject(j, ast);
|
|
292
|
+
if (!object) {
|
|
293
|
+
throw new Error(
|
|
294
|
+
`Could not update ${path.basename(manifestFile)} safely: its default export is not a static object literal.`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (
|
|
299
|
+
object.properties.some((/** @type {any} */ property) =>
|
|
300
|
+
propertyNamed(property, kind),
|
|
301
|
+
)
|
|
302
|
+
) {
|
|
303
|
+
throw new Error(
|
|
304
|
+
`${path.basename(manifestFile)} changed while preparing ${kind}. Run the command again.`,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
object.properties.push(
|
|
309
|
+
j.objectProperty(j.identifier(kind), j.stringLiteral(rootPath)),
|
|
310
|
+
);
|
|
311
|
+
let next = ast.toSource({quote: 'single', reuseWhitespace: true});
|
|
312
|
+
if (source.endsWith('\n') && !next.endsWith('\n')) next += '\n';
|
|
313
|
+
if (!dryRun) {
|
|
314
|
+
if (manifestExists) replaceFile(writableManifestFile, next, source);
|
|
315
|
+
else createFile(manifestFile, next);
|
|
316
|
+
try {
|
|
317
|
+
if (verify) await verify();
|
|
318
|
+
} catch (error) {
|
|
319
|
+
if (manifestExists) replaceFile(writableManifestFile, source, next);
|
|
320
|
+
else removeFileIfUnchanged(manifestFile, next);
|
|
321
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
322
|
+
throw new Error(`Post-write verification failed: ${message}`, {
|
|
323
|
+
cause: error,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return {path: rootPath, created: true};
|
|
328
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
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 {
|
|
7
|
+
findLocalIntegrationManifest,
|
|
8
|
+
findLocalIntegrationManifestOrNull,
|
|
9
|
+
IntegrationRootConflictError,
|
|
10
|
+
patchIntegrationRoot,
|
|
11
|
+
} from './manifest-writer.mjs';
|
|
12
|
+
|
|
13
|
+
let tmpDir;
|
|
14
|
+
|
|
15
|
+
function manifest(source = 'export default {};\n', ext = 'mjs') {
|
|
16
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"fixture"}\n');
|
|
17
|
+
const file = path.join(tmpDir, `astryx.integration.${ext}`);
|
|
18
|
+
fs.writeFileSync(file, source);
|
|
19
|
+
return file;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-manifest-writer-'));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
fs.rmSync(tmpDir, {recursive: true, force: true});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('patchIntegrationRoot', () => {
|
|
31
|
+
it('creates a minimal manifest when requested', async () => {
|
|
32
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"fixture"}\n');
|
|
33
|
+
await expect(
|
|
34
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {
|
|
35
|
+
createIfMissing: true,
|
|
36
|
+
}),
|
|
37
|
+
).resolves.toEqual({path: './themes', created: true});
|
|
38
|
+
expect(
|
|
39
|
+
fs.readFileSync(path.join(tmpDir, 'astryx.integration.mjs'), 'utf-8'),
|
|
40
|
+
).toContain("themes: './themes'");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('plans manifest creation without writing it', async () => {
|
|
44
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"fixture"}\n');
|
|
45
|
+
await expect(
|
|
46
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {
|
|
47
|
+
createIfMissing: true,
|
|
48
|
+
dryRun: true,
|
|
49
|
+
}),
|
|
50
|
+
).resolves.toEqual({path: './themes', created: true});
|
|
51
|
+
expect(findLocalIntegrationManifestOrNull(tmpDir)).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('adds a root to a direct default export and preserves other fields', async () => {
|
|
55
|
+
const file = manifest(
|
|
56
|
+
`// Keep this note.\nexport default {components: './components'};\n`,
|
|
57
|
+
);
|
|
58
|
+
const result = await patchIntegrationRoot(tmpDir, 'themes', './themes');
|
|
59
|
+
const source = fs.readFileSync(file, 'utf-8');
|
|
60
|
+
|
|
61
|
+
expect(result).toEqual({path: './themes', created: true});
|
|
62
|
+
expect(source).toContain('// Keep this note.');
|
|
63
|
+
expect(source).toContain("components: './components'");
|
|
64
|
+
expect(source).toContain("themes: './themes'");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('handles a typed satisfies expression', async () => {
|
|
68
|
+
const file = manifest(
|
|
69
|
+
`import type {AstryxIntegration} from '@astryxdesign/cli/authoring';\nexport default {} satisfies AstryxIntegration;\n`,
|
|
70
|
+
'ts',
|
|
71
|
+
);
|
|
72
|
+
await patchIntegrationRoot(tmpDir, 'themes', './themes');
|
|
73
|
+
expect(fs.readFileSync(file, 'utf-8')).toContain("themes: './themes'");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('is an idempotent no-op when the root already matches', async () => {
|
|
77
|
+
const file = manifest(`export default {themes: './themes'};\n`);
|
|
78
|
+
const before = fs.readFileSync(file, 'utf-8');
|
|
79
|
+
await expect(
|
|
80
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes'),
|
|
81
|
+
).resolves.toEqual({path: './themes', created: false});
|
|
82
|
+
expect(fs.readFileSync(file, 'utf-8')).toBe(before);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('returns a typed refusal instead of replacing an author-owned root', async () => {
|
|
86
|
+
manifest(`export default {themes: './src/themes'};\n`);
|
|
87
|
+
await expect(
|
|
88
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes'),
|
|
89
|
+
).rejects.toBeInstanceOf(IntegrationRootConflictError);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('runs read-back for an already-correct root', async () => {
|
|
93
|
+
manifest(`export default {themes: './themes'};\n`);
|
|
94
|
+
let verified = 0;
|
|
95
|
+
await expect(
|
|
96
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {
|
|
97
|
+
verify: () => {
|
|
98
|
+
verified += 1;
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
).resolves.toEqual({path: './themes', created: false});
|
|
102
|
+
expect(verified).toBe(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('restores the manifest when post-write verification fails', async () => {
|
|
106
|
+
const file = manifest(`export default {components: './components'};\n`);
|
|
107
|
+
const before = fs.readFileSync(file, 'utf-8');
|
|
108
|
+
await expect(
|
|
109
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {
|
|
110
|
+
verify: () => {
|
|
111
|
+
throw new Error('not visible');
|
|
112
|
+
},
|
|
113
|
+
}),
|
|
114
|
+
).rejects.toThrow(/Post-write verification failed: not visible/u);
|
|
115
|
+
expect(fs.readFileSync(file, 'utf-8')).toBe(before);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('does not clobber a concurrent manifest edit during rollback', async () => {
|
|
119
|
+
const file = manifest(`export default {components: './components'};\n`);
|
|
120
|
+
const concurrent = `export default {components: './components', docs: './docs'};\n`;
|
|
121
|
+
await expect(
|
|
122
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {
|
|
123
|
+
verify: () => {
|
|
124
|
+
fs.writeFileSync(file, concurrent);
|
|
125
|
+
throw new Error('not visible');
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
).rejects.toThrow(/File changed while writing/u);
|
|
129
|
+
expect(fs.readFileSync(file, 'utf-8')).toBe(concurrent);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('removes a newly created manifest when verification fails', async () => {
|
|
133
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"fixture"}\n');
|
|
134
|
+
await expect(
|
|
135
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {
|
|
136
|
+
createIfMissing: true,
|
|
137
|
+
verify: () => {
|
|
138
|
+
throw new Error('not visible');
|
|
139
|
+
},
|
|
140
|
+
}),
|
|
141
|
+
).rejects.toThrow(/Post-write verification failed: not visible/u);
|
|
142
|
+
expect(findLocalIntegrationManifestOrNull(tmpDir)).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('preserves a concurrent edit to a newly created manifest on rollback', async () => {
|
|
146
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"fixture"}\n');
|
|
147
|
+
const file = path.join(tmpDir, 'astryx.integration.mjs');
|
|
148
|
+
const concurrent = `export default {docs: './docs'};\n`;
|
|
149
|
+
await expect(
|
|
150
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {
|
|
151
|
+
createIfMissing: true,
|
|
152
|
+
verify: () => {
|
|
153
|
+
fs.writeFileSync(file, concurrent);
|
|
154
|
+
throw new Error('not visible');
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
).rejects.toThrow(/File changed before rollback/u);
|
|
158
|
+
expect(fs.readFileSync(file, 'utf-8')).toBe(concurrent);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('dry-runs the same patch without changing bytes', async () => {
|
|
162
|
+
const file = manifest();
|
|
163
|
+
const before = fs.readFileSync(file, 'utf-8');
|
|
164
|
+
await expect(
|
|
165
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes', {dryRun: true}),
|
|
166
|
+
).resolves.toEqual({path: './themes', created: true});
|
|
167
|
+
expect(fs.readFileSync(file, 'utf-8')).toBe(before);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('refuses a root key that is not a JavaScript identifier', async () => {
|
|
171
|
+
manifest();
|
|
172
|
+
await expect(
|
|
173
|
+
patchIntegrationRoot(tmpDir, 'not-a-key', './root'),
|
|
174
|
+
).rejects.toThrow(/Invalid integration root key/);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('refuses a root that escapes the package', async () => {
|
|
178
|
+
manifest();
|
|
179
|
+
await expect(
|
|
180
|
+
patchIntegrationRoot(tmpDir, 'themes', '../themes'),
|
|
181
|
+
).rejects.toThrow(/outside the project root/u);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('preserves a manifest symlink and updates its in-package target', async () => {
|
|
185
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"fixture"}\n');
|
|
186
|
+
fs.mkdirSync(path.join(tmpDir, 'config'));
|
|
187
|
+
const target = path.join(tmpDir, 'config', 'integration.mjs');
|
|
188
|
+
const manifestFile = path.join(tmpDir, 'astryx.integration.mjs');
|
|
189
|
+
fs.writeFileSync(target, 'export default {};\n');
|
|
190
|
+
fs.symlinkSync(target, manifestFile);
|
|
191
|
+
|
|
192
|
+
await patchIntegrationRoot(tmpDir, 'themes', './themes');
|
|
193
|
+
|
|
194
|
+
expect(fs.lstatSync(manifestFile).isSymbolicLink()).toBe(true);
|
|
195
|
+
expect(fs.readFileSync(target, 'utf-8')).toContain("themes: './themes'");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('refuses a manifest symlink whose target escapes the package', async () => {
|
|
199
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"fixture"}\n');
|
|
200
|
+
const outside = fs.mkdtempSync(
|
|
201
|
+
path.join(process.cwd(), '.astryx-manifest-outside-'),
|
|
202
|
+
);
|
|
203
|
+
try {
|
|
204
|
+
const target = path.join(outside, 'integration.mjs');
|
|
205
|
+
fs.writeFileSync(target, 'export default {};\n');
|
|
206
|
+
fs.symlinkSync(target, path.join(tmpDir, 'astryx.integration.mjs'));
|
|
207
|
+
|
|
208
|
+
await expect(
|
|
209
|
+
patchIntegrationRoot(tmpDir, 'themes', './themes'),
|
|
210
|
+
).rejects.toThrow(/outside the project root|symlink/u);
|
|
211
|
+
expect(fs.readFileSync(target, 'utf-8')).toBe('export default {};\n');
|
|
212
|
+
} finally {
|
|
213
|
+
fs.rmSync(outside, {recursive: true, force: true});
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('finds zero or exactly one conventional manifest', () => {
|
|
218
|
+
expect(findLocalIntegrationManifestOrNull(tmpDir)).toBeNull();
|
|
219
|
+
const file = manifest();
|
|
220
|
+
expect(findLocalIntegrationManifest(tmpDir)).toBe(file);
|
|
221
|
+
fs.writeFileSync(
|
|
222
|
+
path.join(tmpDir, 'astryx.integration.js'),
|
|
223
|
+
'export default {};\n',
|
|
224
|
+
);
|
|
225
|
+
expect(() => findLocalIntegrationManifest(tmpDir)).toThrow(
|
|
226
|
+
/Multiple integration manifests/u,
|
|
227
|
+
);
|
|
228
|
+
});
|
|
229
|
+
});
|