@bestcheckout/dynamic-component-cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +536 -2
- package/package.json +51 -4
- package/src/cli.mjs +87 -0
- package/src/core/browser.mjs +23 -0
- package/src/core/build-runner.mjs +18 -0
- package/src/core/build-session.mjs +69 -0
- package/src/core/build-worker.mjs +8 -0
- package/src/core/build.mjs +85 -0
- package/src/core/config.mjs +115 -0
- package/src/core/doctor.mjs +102 -0
- package/src/core/project.mjs +143 -0
- package/src/core/react-bridge.mjs +65 -0
- package/src/core/server.mjs +191 -0
- package/src/core/source-boundary.mjs +69 -0
- package/src/core/validate.mjs +247 -0
- package/src/protocol/artifact.mjs +88 -0
- package/src/protocol/validation.mjs +96 -0
- package/templates/starter/README.md +58 -0
- package/templates/starter/tsconfig.json +14 -0
- package/types/index.d.ts +1230 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
var offerRecommendationKeys = [
|
|
2
|
+
"recommendationRule",
|
|
3
|
+
"maximumProducts",
|
|
4
|
+
"specificProducts",
|
|
5
|
+
"defaultProducts",
|
|
6
|
+
"productMappings",
|
|
7
|
+
"fillDefaultProducts",
|
|
8
|
+
"discountType",
|
|
9
|
+
"discountValue"
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
function isRecord(value) {
|
|
13
|
+
return typeof value === "object" && value !== null;
|
|
14
|
+
}
|
|
15
|
+
function isPageType(value) {
|
|
16
|
+
return value === "checkout" || value === "upsell" || value === "downsell" || value === "thankYou";
|
|
17
|
+
}
|
|
18
|
+
function validateDynamicManifest(value) {
|
|
19
|
+
if (!isRecord(value))
|
|
20
|
+
return { ok: false, error: "manifest must be an object" };
|
|
21
|
+
if (typeof value.componentId !== "string" || !/^[a-z0-9_-]{1,64}$/.test(value.componentId)) {
|
|
22
|
+
return {
|
|
23
|
+
ok: false,
|
|
24
|
+
error: "manifest.componentId must use 1\u201364 lowercase letters, numbers, underscores or hyphens"
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (typeof value.name !== "string" || !value.name.trim() || [...value.name.trim()].length > 128) {
|
|
28
|
+
return { ok: false, error: "manifest.name is required" };
|
|
29
|
+
}
|
|
30
|
+
if ("dataRequirements" in value) {
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
error: "manifest.dataRequirements is not supported; use runtime snapshots and subscriptions"
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if ("errorContent" in value) {
|
|
37
|
+
return {
|
|
38
|
+
ok: false,
|
|
39
|
+
error: "manifest.errorContent is not supported"
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (value.supportedPageTypes !== void 0 && (!Array.isArray(value.supportedPageTypes) || value.supportedPageTypes.some((item) => !isPageType(item)))) {
|
|
43
|
+
return { ok: false, error: "manifest.supportedPageTypes is invalid" };
|
|
44
|
+
}
|
|
45
|
+
if (value.capabilities !== void 0 && (!isRecord(value.capabilities) || Array.isArray(value.capabilities) || value.capabilities.offerRecommendation !== void 0 && typeof value.capabilities.offerRecommendation !== "boolean")) {
|
|
46
|
+
return {
|
|
47
|
+
ok: false,
|
|
48
|
+
error: "manifest.capabilities.offerRecommendation must be a boolean"
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return { ok: true, manifest: value };
|
|
52
|
+
}
|
|
53
|
+
function validateDynamicComponentDefinition(value) {
|
|
54
|
+
if (!isRecord(value))
|
|
55
|
+
return { ok: false, error: "definition must be an object" };
|
|
56
|
+
const manifestResult = validateDynamicManifest(value.manifest);
|
|
57
|
+
if (!manifestResult.ok) return manifestResult;
|
|
58
|
+
if (!isRecord(value.defaultSettings)) {
|
|
59
|
+
return { ok: false, error: "defaultSettings must be an object" };
|
|
60
|
+
}
|
|
61
|
+
if (!isRecord(value.schema) && !Array.isArray(value.schema)) {
|
|
62
|
+
return { ok: false, error: "schema must be an object or array" };
|
|
63
|
+
}
|
|
64
|
+
if (manifestResult.manifest.capabilities?.offerRecommendation) {
|
|
65
|
+
const fields = Array.isArray(value.schema) ? value.schema : value.schema.fields;
|
|
66
|
+
if (Array.isArray(fields) && fields.some(
|
|
67
|
+
(field) => isRecord(field) && offerRecommendationKeys.includes(
|
|
68
|
+
field.key
|
|
69
|
+
)
|
|
70
|
+
))
|
|
71
|
+
return {
|
|
72
|
+
ok: false,
|
|
73
|
+
error: "Offer recommendation fields are provided by the platform; remove duplicate schema keys"
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const hasComponent = typeof value.Component === "function";
|
|
77
|
+
if (!hasComponent) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
error: "Component must be a function"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if ("renderError" in value) {
|
|
84
|
+
return { ok: false, error: "renderError is not supported" };
|
|
85
|
+
}
|
|
86
|
+
if (value.styles !== void 0 && typeof value.styles !== "string") {
|
|
87
|
+
return { ok: false, error: "styles must be a string" };
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
ok: true,
|
|
91
|
+
definition: value
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
validateDynamicComponentDefinition
|
|
96
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Dynamic component project
|
|
2
|
+
|
|
3
|
+
Full component protocol, Runtime API, examples and publishing guidance: [Development guide](https://www.npmjs.com/package/@bestcheckout/dynamic-component-cli).
|
|
4
|
+
|
|
5
|
+
## Start development
|
|
6
|
+
|
|
7
|
+
Use Node.js >=20.10 and npm. From this project directory:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install
|
|
11
|
+
npx playwright install chromium
|
|
12
|
+
npm run dev
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Chromium is required for build validation, including Editor Save. If a suitable browser is already installed, the browser download can be skipped; macOS Chrome can be detected automatically.
|
|
16
|
+
|
|
17
|
+
1. Open the matching page type in the BestCheckout theme editor and add/select a custom component.
|
|
18
|
+
2. Paste the terminal's component URL into **Component source → Local Dev Server source URL** and finish editing the field. The URL is a source module, not a standalone page.
|
|
19
|
+
3. Edit the registered `src/<componentId>/component.tsx`. JSX and exported `styles` changes update the preview through HMR.
|
|
20
|
+
4. Keep the server running and click **Save** in the Editor to build, validate and upload the artifact and save template configuration. Fix errors before retrying.
|
|
21
|
+
5. Click **Publish** to apply the saved changes to the storefront. Stop the local server and refresh the storefront to check the published artifact.
|
|
22
|
+
|
|
23
|
+
For a custom port, run `npm run dev -- --port 5180` and reconnect using the newly printed URL. Ctrl+C stops the server.
|
|
24
|
+
|
|
25
|
+
## Add components
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx dynamic-component create offer-card --page-type upsell
|
|
29
|
+
npx dynamic-component create downsell-card --page-type downsell
|
|
30
|
+
npx dynamic-component create order-card --page-type thankYou
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Supported page types are exactly `checkout`, `upsell`, `downsell`, `thankYou`. IDs use 1–64 lowercase letters, digits, underscores or hyphens. Restart the server after adding components or changing `dynamic.config.json`.
|
|
34
|
+
|
|
35
|
+
The config registers local entry files; each entry's exported manifest must have the matching component ID and support its page type. Upsell/Downsell starters declare `offerRecommendation`; configure recommendation rules in the Editor.
|
|
36
|
+
|
|
37
|
+
## Build and diagnose
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm run build
|
|
41
|
+
npm run validate
|
|
42
|
+
npm run doctor
|
|
43
|
+
npx dynamic-component --help
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`build` type-checks and validates the components in a browser, then writes `dist/<componentId>.js` for manual upload. `validate` checks without writing artifacts. `doctor` diagnoses setup; it is optional. Editor Local Save calls the build automatically. Save/Publish remain Editor operations.
|
|
47
|
+
|
|
48
|
+
If Chromium is unavailable, install it using the command above. A manual build can use `npm run build -- --browser-executable "/absolute/path/to/browser"`; Editor-triggered builds can use `DYNAMIC_COMPONENT_BROWSER` set before starting the server.
|
|
49
|
+
|
|
50
|
+
## Authoring and dependencies
|
|
51
|
+
|
|
52
|
+
Use `settings` for configured values and `runtime` for page capabilities. Public types come from `@bestcheckout/dynamic-component-cli/types`. Host React/ReactDOM and relative source-code imports inside `src` are supported. Arbitrary third-party runtime dependencies, source symlinks and imports outside `src` are not supported. Use exported `styles` and hosted URLs instead of CSS/image imports.
|
|
53
|
+
|
|
54
|
+
If initialized from a local archive, keep `vendor/dynamic-component-cli.tgz`; this project's dependency references it. Public dependencies still require registry access.
|
|
55
|
+
|
|
56
|
+
After a new CLI release, stop the server and explicitly upgrade the project dependency with `npm install --save-dev --save-exact @bestcheckout/dynamic-component-cli@latest`, install its Chromium if needed, then build and restart. Existing component source is not regenerated.
|
|
57
|
+
|
|
58
|
+
The CLI package uses the MIT license; your generated project does not automatically receive a license declaration. No platform credentials are stored by the CLI. Preview and validation execute component code in a browser and are not a sandbox. Windows/Linux, Safari/Firefox and production HTTPS Editor compatibility have not yet been fully verified.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"jsx": "react-jsx",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noEmit": true,
|
|
9
|
+
"skipLibCheck": false,
|
|
10
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
11
|
+
"types": ["react", "react-dom"]
|
|
12
|
+
},
|
|
13
|
+
"include": ["src/**/*.tsx", "src/**/*.ts", "src/**/*.jsx"]
|
|
14
|
+
}
|