@nextcommerce/campaigns-os 1.33.0

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.
Files changed (310) hide show
  1. package/AGENTS.md +204 -0
  2. package/CHANGELOG.md +5002 -0
  3. package/CONTEXT.md +685 -0
  4. package/LICENSE +202 -0
  5. package/NOTICE +4 -0
  6. package/README.md +368 -0
  7. package/agents/claude/CLAUDE.md +32 -0
  8. package/agents/codex/AGENTS.md +27 -0
  9. package/agents/copilot/copilot-instructions.md +14 -0
  10. package/agents/cursor/campaigns-os.mdc +13 -0
  11. package/bin/campaigns-os.mjs +38 -0
  12. package/campaign-spec/README.md +138 -0
  13. package/campaign-spec/dist/analytics-vocabulary.d.ts +47 -0
  14. package/campaign-spec/dist/analytics-vocabulary.js +74 -0
  15. package/campaign-spec/dist/index.d.ts +40 -0
  16. package/campaign-spec/dist/index.js +77 -0
  17. package/campaign-spec/dist/normalize.d.ts +22 -0
  18. package/campaign-spec/dist/normalize.js +41 -0
  19. package/campaign-spec/dist/routing.d.ts +190 -0
  20. package/campaign-spec/dist/routing.js +263 -0
  21. package/campaign-spec/dist/rules/analytics-contract-shape.d.ts +42 -0
  22. package/campaign-spec/dist/rules/analytics-contract-shape.js +303 -0
  23. package/campaign-spec/dist/rules/assembly-hints-shape.d.ts +42 -0
  24. package/campaign-spec/dist/rules/assembly-hints-shape.js +191 -0
  25. package/campaign-spec/dist/rules/campaign-metadata.d.ts +14 -0
  26. package/campaign-spec/dist/rules/campaign-metadata.js +40 -0
  27. package/campaign-spec/dist/rules/checkout-has-success-url.d.ts +31 -0
  28. package/campaign-spec/dist/rules/checkout-has-success-url.js +66 -0
  29. package/campaign-spec/dist/rules/cycle-detection.d.ts +12 -0
  30. package/campaign-spec/dist/rules/cycle-detection.js +141 -0
  31. package/campaign-spec/dist/rules/design-source-shape.d.ts +29 -0
  32. package/campaign-spec/dist/rules/design-source-shape.js +142 -0
  33. package/campaign-spec/dist/rules/downsell-without-upsell.d.ts +11 -0
  34. package/campaign-spec/dist/rules/downsell-without-upsell.js +40 -0
  35. package/campaign-spec/dist/rules/exit-intent-validation.d.ts +23 -0
  36. package/campaign-spec/dist/rules/exit-intent-validation.js +147 -0
  37. package/campaign-spec/dist/rules/funnel-count.d.ts +9 -0
  38. package/campaign-spec/dist/rules/funnel-count.js +37 -0
  39. package/campaign-spec/dist/rules/funnel-hypothesis-length.d.ts +22 -0
  40. package/campaign-spec/dist/rules/funnel-hypothesis-length.js +60 -0
  41. package/campaign-spec/dist/rules/funnel-identity.d.ts +15 -0
  42. package/campaign-spec/dist/rules/funnel-identity.js +57 -0
  43. package/campaign-spec/dist/rules/funnel-weight-sum.d.ts +20 -0
  44. package/campaign-spec/dist/rules/funnel-weight-sum.js +66 -0
  45. package/campaign-spec/dist/rules/index.d.ts +69 -0
  46. package/campaign-spec/dist/rules/index.js +131 -0
  47. package/campaign-spec/dist/rules/offer-ref-integrity.d.ts +13 -0
  48. package/campaign-spec/dist/rules/offer-ref-integrity.js +59 -0
  49. package/campaign-spec/dist/rules/package-pricing-sanity.d.ts +12 -0
  50. package/campaign-spec/dist/rules/package-pricing-sanity.js +42 -0
  51. package/campaign-spec/dist/rules/page-count.d.ts +11 -0
  52. package/campaign-spec/dist/rules/page-count.js +31 -0
  53. package/campaign-spec/dist/rules/page-id-uniqueness.d.ts +14 -0
  54. package/campaign-spec/dist/rules/page-id-uniqueness.js +47 -0
  55. package/campaign-spec/dist/rules/promo-code-input-validation.d.ts +8 -0
  56. package/campaign-spec/dist/rules/promo-code-input-validation.js +126 -0
  57. package/campaign-spec/dist/rules/promo-codes-shape.d.ts +30 -0
  58. package/campaign-spec/dist/rules/promo-codes-shape.js +187 -0
  59. package/campaign-spec/dist/rules/route-field-ignored-for-page-type.d.ts +33 -0
  60. package/campaign-spec/dist/rules/route-field-ignored-for-page-type.js +81 -0
  61. package/campaign-spec/dist/rules/route-target-resolves.d.ts +32 -0
  62. package/campaign-spec/dist/rules/route-target-resolves.js +112 -0
  63. package/campaign-spec/dist/rules/schema-version.d.ts +21 -0
  64. package/campaign-spec/dist/rules/schema-version.js +53 -0
  65. package/campaign-spec/dist/rules/sdk-version.d.ts +26 -0
  66. package/campaign-spec/dist/rules/sdk-version.js +96 -0
  67. package/campaign-spec/dist/rules/shipping-countries-shape.d.ts +10 -0
  68. package/campaign-spec/dist/rules/shipping-countries-shape.js +30 -0
  69. package/campaign-spec/dist/rules/shipping-methods-present.d.ts +10 -0
  70. package/campaign-spec/dist/rules/shipping-methods-present.js +26 -0
  71. package/campaign-spec/dist/rules/store-profile-shape.d.ts +30 -0
  72. package/campaign-spec/dist/rules/store-profile-shape.js +127 -0
  73. package/campaign-spec/dist/rules/thank-you-requirement.d.ts +16 -0
  74. package/campaign-spec/dist/rules/thank-you-requirement.js +50 -0
  75. package/campaign-spec/dist/rules/unknown-top-level-fields.d.ts +28 -0
  76. package/campaign-spec/dist/rules/unknown-top-level-fields.js +114 -0
  77. package/campaign-spec/dist/rules/upsell-has-packages.d.ts +9 -0
  78. package/campaign-spec/dist/rules/upsell-has-packages.js +35 -0
  79. package/campaign-spec/dist/rules/upsell-routing-complete.d.ts +10 -0
  80. package/campaign-spec/dist/rules/upsell-routing-complete.js +45 -0
  81. package/campaign-spec/dist/rules/upsell-without-checkout.d.ts +11 -0
  82. package/campaign-spec/dist/rules/upsell-without-checkout.js +44 -0
  83. package/campaign-spec/dist/rules/variant-labels-shape.d.ts +28 -0
  84. package/campaign-spec/dist/rules/variant-labels-shape.js +86 -0
  85. package/campaign-spec/dist/sdk-version-parse.d.ts +43 -0
  86. package/campaign-spec/dist/sdk-version-parse.js +62 -0
  87. package/campaign-spec/dist/types.d.ts +674 -0
  88. package/campaign-spec/dist/types.js +40 -0
  89. package/campaign-spec/package.json +12 -0
  90. package/compatibility.json +25 -0
  91. package/contracts/agent-relevant-change-policy.v1.json +111 -0
  92. package/contracts/brand-theme-source-defaults.figma-sections-export.v0.json +32 -0
  93. package/contracts/brand-theme-target-tokens.next-core.v0.json +65 -0
  94. package/contracts/campaign-cart-checkout-field-contract.v0.json +45 -0
  95. package/contracts/campaign-cart-sdk-support-policy.v0.json +11 -0
  96. package/contracts/commerce-surface-catalog.json +2452 -0
  97. package/contracts/fixtures/orientation/canonicalization/v1.json +34 -0
  98. package/contracts/fixtures/orientation/envelope/current.json +95 -0
  99. package/contracts/fixtures/orientation/envelope/freshness_unknown.json +97 -0
  100. package/contracts/fixtures/orientation/envelope/legacy_baseline.json +95 -0
  101. package/contracts/fixtures/orientation/envelope/orientation_available.json +136 -0
  102. package/contracts/fixtures/orientation/envelope/recovered_interrupted_update.json +136 -0
  103. package/contracts/fixtures/orientation/envelope/refused.json +100 -0
  104. package/contracts/fixtures/orientation/envelope/restart_required.json +137 -0
  105. package/contracts/fixtures/orientation/envelope/updated.json +135 -0
  106. package/contracts/fixtures/orientation/hostile-target/README.md +61 -0
  107. package/contracts/fixtures/orientation/hostile-target/manifest.json +82 -0
  108. package/contracts/fixtures/orientation/hostile-target/repo/CHANGELOG.md +18 -0
  109. package/contracts/fixtures/orientation/hostile-target/repo/bin/intended.mjs +12 -0
  110. package/contracts/fixtures/orientation/hostile-target/repo/bin/tripwire.mjs +15 -0
  111. package/contracts/fixtures/orientation/hostile-target/repo/contracts/release-ledger.json +48 -0
  112. package/contracts/fixtures/orientation/hostile-target/repo/contracts/supported-surface.json +17 -0
  113. package/contracts/fixtures/orientation/hostile-target/repo/docs/example-contract.md +13 -0
  114. package/contracts/fixtures/orientation/hostile-target/repo/hooks/post-checkout +5 -0
  115. package/contracts/fixtures/orientation/hostile-target/repo/hooks/post-merge +5 -0
  116. package/contracts/fixtures/orientation/hostile-target/repo/hooks/pre-commit +5 -0
  117. package/contracts/fixtures/orientation/hostile-target/repo/hostile-dependency-tripwire/package.json +15 -0
  118. package/contracts/fixtures/orientation/hostile-target/repo/hostile-dependency-tripwire/tripwire.mjs +9 -0
  119. package/contracts/fixtures/orientation/hostile-target/repo/package.json +20 -0
  120. package/contracts/fixtures/orientation/hostile-target/repo/schemas/example.v0.schema.json +15 -0
  121. package/contracts/fixtures/orientation/release-gate/cases.json +1073 -0
  122. package/contracts/fixtures/runtime-recipe/accept/current.json +299 -0
  123. package/contracts/fixtures/runtime-recipe/accept/minimal.json +294 -0
  124. package/contracts/fixtures/runtime-recipe/dist-states.json +51 -0
  125. package/contracts/fixtures/runtime-recipe/manifest.json +85 -0
  126. package/contracts/fixtures/runtime-recipe/reject/advisory-enforcement.json +299 -0
  127. package/contracts/fixtures/runtime-recipe/reject/allowlist-without-hosts.json +297 -0
  128. package/contracts/fixtures/runtime-recipe/reject/committed-output-claim.json +299 -0
  129. package/contracts/fixtures/runtime-recipe/reject/engines-disagreement-warns.json +299 -0
  130. package/contracts/fixtures/runtime-recipe/reject/lifecycle-scripts-enabled.json +299 -0
  131. package/contracts/fixtures/runtime-recipe/reject/missing-required-field.json +251 -0
  132. package/contracts/fixtures/runtime-recipe/reject/unknown-kind.json +299 -0
  133. package/contracts/fixtures/runtime-recipe/reject/unknown-network-policy.json +299 -0
  134. package/contracts/fixtures/runtime-recipe/reject/unknown-output-check.json +310 -0
  135. package/contracts/fixtures/runtime-recipe/reject/unknown-revision.json +299 -0
  136. package/contracts/fixtures/runtime-recipe/reject/unknown-step-id.json +299 -0
  137. package/contracts/fixtures/runtime-recipe/reject/unperformable-check-skipped.json +299 -0
  138. package/contracts/fixtures/runtime-recipe/reject/unpinned-lockfile.json +299 -0
  139. package/contracts/fixtures/sidecar-bundle/production-shaped/.campaign-runtime/assembly-report.json +180 -0
  140. package/contracts/fixtures/sidecar-bundle/production-shaped/.campaign-runtime/build-context.json +115 -0
  141. package/contracts/fixtures/sidecar-bundle/production-shaped/.campaign-runtime/doctor-output.json +29 -0
  142. package/contracts/fixtures/sidecar-bundle/production-shaped/.campaign-runtime/qa-verdict.json +27 -0
  143. package/contracts/fixtures/sidecar-bundle/production-shaped/campaign-runtime.build.json +141 -0
  144. package/contracts/migration-sidecar-bundle.v0.json +149 -0
  145. package/contracts/orientation-limits.v1.json +41 -0
  146. package/contracts/orientation-reason-codes.v1.json +196 -0
  147. package/contracts/private-template-sources.json +8 -0
  148. package/contracts/release-ledger.json +4598 -0
  149. package/contracts/reserved-skill-names.json +13 -0
  150. package/contracts/runtime-recipe.campaigns-os-node-v1.json +299 -0
  151. package/contracts/supported-surface.json +180 -0
  152. package/contracts/template-brand-contract.apollo-mv-single-step.v0.json +27 -0
  153. package/contracts/template-brand-contract.apollo.v0.json +27 -0
  154. package/contracts/template-brand-contract.demeter.v0.json +27 -0
  155. package/contracts/template-brand-contract.olympus-mv-single-step.v0.json +27 -0
  156. package/contracts/template-brand-contract.olympus-mv-two-step.v0.json +28 -0
  157. package/contracts/template-brand-contract.olympus.v0.json +27 -0
  158. package/contracts/template-brand-contract.shared-commerce.v0.json +190 -0
  159. package/contracts/template-brand-contract.shop-single-step.v0.json +27 -0
  160. package/contracts/template-brand-contract.shop-three-step.v0.json +29 -0
  161. package/contracts/template-slot-manifest.apollo-mv-single-step.v0.json +14 -0
  162. package/contracts/template-slot-manifest.apollo.v0.json +12 -0
  163. package/contracts/template-slot-manifest.demeter.v0.json +30 -0
  164. package/contracts/template-slot-manifest.olympus-mv-single-step.v0.json +14 -0
  165. package/contracts/template-slot-manifest.olympus-mv-two-step.v0.json +15 -0
  166. package/contracts/template-slot-manifest.olympus.v0.json +12 -0
  167. package/contracts/template-slot-manifest.shared-content-core.v0.json +4254 -0
  168. package/contracts/template-slot-manifest.shop-single-step.v0.json +47 -0
  169. package/contracts/template-slot-manifest.shop-three-step.v0.json +33 -0
  170. package/docs/brand-theme-bridge.md +159 -0
  171. package/docs/build-packet.md +1300 -0
  172. package/docs/campaign-build-brief.md +145 -0
  173. package/docs/campaign-standardization-report.md +329 -0
  174. package/docs/campaigns-os-build-flow.md +117 -0
  175. package/docs/design-source-package.md +784 -0
  176. package/docs/legacy-migration.md +58 -0
  177. package/docs/migration-sidecar-bundle.md +139 -0
  178. package/docs/orientation-contract-reference.md +1220 -0
  179. package/docs/polish-evidence.md +502 -0
  180. package/docs/qa-and-test-orders.md +1691 -0
  181. package/docs/release-ledger-authoring-guide.md +274 -0
  182. package/docs/runtime-readiness.md +211 -0
  183. package/docs/supported-surface.md +83 -0
  184. package/docs/versioning.md +55 -0
  185. package/docs/workflow-findings-sidecar.md +588 -0
  186. package/package.json +135 -0
  187. package/prompts/first-build.md +27 -0
  188. package/prompts/friction-log.md +30 -0
  189. package/schemas/campaign-build-brief.v1.schema.json +149 -0
  190. package/schemas/campaign-design-source-package.v0.schema.json +697 -0
  191. package/schemas/campaign-runtime-assembly-report.v0.schema.json +390 -0
  192. package/schemas/campaign-runtime-build-context.v0.schema.json +339 -0
  193. package/schemas/campaign-runtime-build-packet.v0.schema.json +452 -0
  194. package/schemas/campaign-spec.v4.schema.json +582 -0
  195. package/schemas/campaigns-os-doctor-output.v0.schema.json +43 -0
  196. package/schemas/campaigns-os-legacy-migration-inventory.v0.schema.json +112 -0
  197. package/schemas/campaigns-os-legacy-provisioning-plan.v0.schema.json +38 -0
  198. package/schemas/campaigns-os-legacy-provisioning-receipt.v0.schema.json +57 -0
  199. package/schemas/campaigns-os-qa-verdict-sidecar.v0.schema.json +150 -0
  200. package/schemas/campaigns-os-qa-verdict.v0.schema.json +438 -0
  201. package/schemas/campaigns-os-release-ledger.v1.schema.json +162 -0
  202. package/schemas/campaigns-os-run-record.v0.schema.json +343 -0
  203. package/schemas/campaigns-os-runtime-recipe.v1.schema.json +313 -0
  204. package/schemas/campaigns-os-sidecar-bundle-conformance.v0.schema.json +78 -0
  205. package/schemas/campaigns-os-tooling-orientation.v1.schema.json +381 -0
  206. package/schemas/campaigns-os-workflow-finding.v0.schema.json +134 -0
  207. package/schemas/source-html-manifest.v0.schema.json +252 -0
  208. package/skills/next-campaigns-build/SKILL.md +73 -0
  209. package/skills/next-campaigns-os/SKILL.md +101 -0
  210. package/skills/next-campaigns-os/references/session-intake.md +160 -0
  211. package/skills/next-campaigns-os-setup/SKILL.md +22 -0
  212. package/skills/next-campaigns-polish/SKILL.md +145 -0
  213. package/skills/next-campaigns-qa/SKILL.md +92 -0
  214. package/skills.json +56 -0
  215. package/skills.sh +64 -0
  216. package/src/adapter-decision-contract.mjs +333 -0
  217. package/src/brand-theme.mjs +1151 -0
  218. package/src/browser-launch.mjs +79 -0
  219. package/src/build-brief.mjs +781 -0
  220. package/src/built-site-scope.mjs +312 -0
  221. package/src/campaign-ecosystem.mjs +734 -0
  222. package/src/campaign-identity.mjs +405 -0
  223. package/src/campaign-workspace.mjs +127 -0
  224. package/src/checkpoint-waiver.mjs +302 -0
  225. package/src/cli.mjs +13442 -0
  226. package/src/commercial-journey.mjs +1119 -0
  227. package/src/commercial-parity.mjs +965 -0
  228. package/src/consent.mjs +347 -0
  229. package/src/content-residue.mjs +322 -0
  230. package/src/deadline.mjs +81 -0
  231. package/src/design-source-package.mjs +2604 -0
  232. package/src/deviation.mjs +107 -0
  233. package/src/doctor-check-registry.mjs +49 -0
  234. package/src/doctor-sidecar.mjs +106 -0
  235. package/src/finding-cause.mjs +557 -0
  236. package/src/findings.mjs +326 -0
  237. package/src/fs-identity.mjs +68 -0
  238. package/src/gate-actions.mjs +105 -0
  239. package/src/html-scan.mjs +53 -0
  240. package/src/install-mode.mjs +272 -0
  241. package/src/legacy-migration.d.ts +128 -0
  242. package/src/legacy-migration.mjs +510 -0
  243. package/src/lifecycle.mjs +338 -0
  244. package/src/local-proof.mjs +401 -0
  245. package/src/map-pin-writeback.mjs +210 -0
  246. package/src/orchestration-stage-contract.mjs +81 -0
  247. package/src/package-install-fixture.mjs +33 -0
  248. package/src/page-kit-build-summary.mjs +175 -0
  249. package/src/page-kit-campaign-config.mjs +57 -0
  250. package/src/page-kit-sdk-version.mjs +392 -0
  251. package/src/page-kit-store-profile.mjs +369 -0
  252. package/src/page-kit-sync.mjs +162 -0
  253. package/src/polish-browser.mjs +867 -0
  254. package/src/polish-capture.mjs +1094 -0
  255. package/src/polish-deadline.mjs +51 -0
  256. package/src/polish-gate.mjs +739 -0
  257. package/src/polish-node.mjs +639 -0
  258. package/src/polish-page-load.mjs +1100 -0
  259. package/src/private-template-source.mjs +237 -0
  260. package/src/proof-policy.mjs +82 -0
  261. package/src/qa-analytics-correctness.mjs +307 -0
  262. package/src/qa-analytics-errors.mjs +38 -0
  263. package/src/qa-analytics-parity.mjs +699 -0
  264. package/src/qa-binding-evidence.mjs +140 -0
  265. package/src/qa-browser.mjs +6608 -0
  266. package/src/qa-cart-entry.mjs +406 -0
  267. package/src/qa-commercial-parity.mjs +641 -0
  268. package/src/qa-node.mjs +3620 -0
  269. package/src/qa-order-bump.mjs +381 -0
  270. package/src/qa-parity-capture.mjs +428 -0
  271. package/src/qa-parity-fixture.mjs +359 -0
  272. package/src/qa-publish.mjs +362 -0
  273. package/src/qa-purchase-data-layer.mjs +263 -0
  274. package/src/qa-route-probe.mjs +272 -0
  275. package/src/qa-sidecar.mjs +188 -0
  276. package/src/qa-test-order-topology.mjs +207 -0
  277. package/src/qa-url-privacy.mjs +13 -0
  278. package/src/qa-verdict-discovery.mjs +192 -0
  279. package/src/qa-verdict-publish.mjs +105 -0
  280. package/src/qa-verdict.mjs +287 -0
  281. package/src/remit.mjs +388 -0
  282. package/src/repo-scan.mjs +83 -0
  283. package/src/route-identity.mjs +133 -0
  284. package/src/run-record-closeout.mjs +229 -0
  285. package/src/run-record.mjs +839 -0
  286. package/src/run-session.mjs +226 -0
  287. package/src/runtime-state-ignore.mjs +113 -0
  288. package/src/sdk-attribute-index.mjs +212 -0
  289. package/src/sdk-markup.mjs +358 -0
  290. package/src/sdk-meta-tags.mjs +52 -0
  291. package/src/shell-token.mjs +7 -0
  292. package/src/sidecar-bundle.mjs +399 -0
  293. package/src/source-asset-crawl.mjs +469 -0
  294. package/src/source-html-intake.mjs +627 -0
  295. package/src/source-html-manifest.mjs +276 -0
  296. package/src/source-prep.mjs +284 -0
  297. package/src/spec-derive-store.mjs +431 -0
  298. package/src/spec-derive.mjs +514 -0
  299. package/src/spec-fetch.mjs +66 -0
  300. package/src/spec-hash.mjs +48 -0
  301. package/src/spec-identity.mjs +27 -0
  302. package/src/stage-ledger.mjs +509 -0
  303. package/src/standardization-report.mjs +1297 -0
  304. package/src/template-brand-contract.mjs +473 -0
  305. package/src/template-freshness.mjs +196 -0
  306. package/src/template-reference.mjs +81 -0
  307. package/src/template-slot-manifest.mjs +150 -0
  308. package/src/text-safety.mjs +62 -0
  309. package/src/theme-gate.mjs +185 -0
  310. package/src/upsell-selector-scope.mjs +299 -0
@@ -0,0 +1,469 @@
1
+ import { createHash } from "node:crypto";
2
+ import { existsSync, readFileSync, statSync } from "node:fs";
3
+ import { dirname, extname, isAbsolute, relative, resolve } from "node:path";
4
+
5
+ export const SOURCE_ASSET_CRAWL_SCHEMA = "source-asset-crawl/v0";
6
+
7
+ const ASSET_EXTENSIONS = new Set([
8
+ ".avif",
9
+ ".css",
10
+ ".eot",
11
+ ".gif",
12
+ ".ico",
13
+ ".jpeg",
14
+ ".jpg",
15
+ ".js",
16
+ ".json",
17
+ ".mjs",
18
+ ".mov",
19
+ ".mp3",
20
+ ".mp4",
21
+ ".otf",
22
+ ".pdf",
23
+ ".png",
24
+ ".svg",
25
+ ".ttf",
26
+ ".wav",
27
+ ".webm",
28
+ ".webp",
29
+ ".woff",
30
+ ".woff2",
31
+ ]);
32
+
33
+ const SKIP_PROTOCOL_PATTERN = /^(?:about|blob|data|javascript|mailto|tel):/i;
34
+ const DEFAULT_MAX_CSS_FILES = 256;
35
+ const HTML_ASSET_ATTRIBUTES = new Set([
36
+ "content",
37
+ "data-background",
38
+ "data-bg",
39
+ "data-src",
40
+ "href",
41
+ "imagesrcset",
42
+ "poster",
43
+ "src",
44
+ "srcset",
45
+ ]);
46
+
47
+ function isNonEmptyString(value) {
48
+ return typeof value === "string" && value.trim().length > 0;
49
+ }
50
+
51
+ function sha256Content(content) {
52
+ return createHash("sha256").update(content).digest("hex");
53
+ }
54
+
55
+ function toPosixPath(value) {
56
+ return String(value || "").replaceAll("\\", "/");
57
+ }
58
+
59
+ function relFromRoot(root, path) {
60
+ return toPosixPath(relative(resolve(root), resolve(path)));
61
+ }
62
+
63
+ function isInsideRoot(root, path) {
64
+ const rel = relative(resolve(root), resolve(path));
65
+ return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
66
+ }
67
+
68
+ function cleanRef(value) {
69
+ const raw = decodeHtmlEntities(String(value || "").trim().replace(/^["']|["']$/g, ""));
70
+ if (!raw || raw.startsWith("#")) return null;
71
+ if (raw.startsWith("{{") || raw.startsWith("{%")) return null;
72
+ if (raw.startsWith("//")) return null;
73
+ if (SKIP_PROTOCOL_PATTERN.test(raw)) return null;
74
+ try {
75
+ const url = new URL(raw);
76
+ if (url.protocol === "http:" || url.protocol === "https:") return null;
77
+ } catch {
78
+ // Relative or root-relative refs are the expected input.
79
+ }
80
+ return raw.split("#")[0].split("?")[0].trim();
81
+ }
82
+
83
+ function decodeHtmlEntities(value) {
84
+ return String(value || "")
85
+ .replace(/&/gi, "&")
86
+ .replace(/"/gi, "\"")
87
+ .replace(/'|'/gi, "'")
88
+ .replace(/&lt;/gi, "<")
89
+ .replace(/&gt;/gi, ">");
90
+ }
91
+
92
+ function looksLikeAssetRef(value) {
93
+ const cleaned = cleanRef(value);
94
+ if (!cleaned) return false;
95
+ const pathname = cleaned.split(/[?#]/)[0];
96
+ if (/^\/?assets\//i.test(pathname)) return true;
97
+ if (/(^|\/)(images?|img|fonts?|css|js|media|products)\//i.test(pathname)) return true;
98
+ return ASSET_EXTENSIONS.has(extname(pathname).toLowerCase());
99
+ }
100
+
101
+ function splitSrcset(value) {
102
+ return String(value || "")
103
+ .split(",")
104
+ .map((part) => part.trim().split(/\s+/)[0])
105
+ .filter(Boolean);
106
+ }
107
+
108
+ function extractCssRefs(content) {
109
+ const refs = [];
110
+ const urlPattern = /url\(\s*(['"]?)([^'")]+)\1\s*\)/gi;
111
+ for (const match of content.matchAll(urlPattern)) {
112
+ refs.push({ raw: match[2], attribute: "url()" });
113
+ }
114
+ const importPattern = /@import\s+(?:url\()?["']?([^"')\s]+)["']?\)?/gi;
115
+ for (const match of content.matchAll(importPattern)) {
116
+ refs.push({ raw: match[1], attribute: "@import" });
117
+ }
118
+ return refs;
119
+ }
120
+
121
+ function extractHtmlRefs(content) {
122
+ const refs = [];
123
+ const tags = extractHtmlTags(content);
124
+
125
+ function addRef(raw, attribute) {
126
+ const attr = attribute.toLowerCase();
127
+ if (attr === "srcset" || attr === "imagesrcset") {
128
+ for (const item of splitSrcset(raw)) refs.push({ raw: item, attribute: attr });
129
+ return;
130
+ }
131
+ refs.push({ raw, attribute: attr });
132
+ }
133
+
134
+ for (const tag of tags) {
135
+ for (const attr of extractHtmlTagAttributes(tag)) {
136
+ if (HTML_ASSET_ATTRIBUTES.has(attr.name)) addRef(attr.value, attr.name);
137
+ if (attr.name === "style") {
138
+ for (const ref of extractCssRefs(attr.value)) refs.push({ ...ref, attribute: `style:${ref.attribute}` });
139
+ }
140
+ }
141
+ }
142
+
143
+ const styleBlockPattern = /<style\b[^>]*>([\s\S]*?)<\/style>/gi;
144
+ for (const match of content.matchAll(styleBlockPattern)) {
145
+ for (const ref of extractCssRefs(match[1])) refs.push({ ...ref, attribute: `style-block:${ref.attribute}` });
146
+ }
147
+ return refs;
148
+ }
149
+
150
+ function extractHtmlTagAttributes(tag) {
151
+ const attrs = [];
152
+ const text = String(tag || "");
153
+ let index = 0;
154
+
155
+ if (text[index] !== "<") return attrs;
156
+ index += 1;
157
+ while (index < text.length && /\s/.test(text[index])) index += 1;
158
+ while (index < text.length && !/[\s/>]/.test(text[index])) index += 1;
159
+
160
+ while (index < text.length) {
161
+ while (index < text.length && /\s/.test(text[index])) index += 1;
162
+ if (index >= text.length || text[index] === ">") break;
163
+ if (text[index] === "/" || text[index] === "?") {
164
+ index += 1;
165
+ continue;
166
+ }
167
+
168
+ const nameStart = index;
169
+ while (index < text.length && !/[\s=/>]/.test(text[index])) index += 1;
170
+ const name = text.slice(nameStart, index).toLowerCase();
171
+ while (index < text.length && /\s/.test(text[index])) index += 1;
172
+
173
+ let value = "";
174
+ if (text[index] === "=") {
175
+ index += 1;
176
+ while (index < text.length && /\s/.test(text[index])) index += 1;
177
+ const quote = text[index];
178
+ if (quote === "\"" || quote === "'") {
179
+ index += 1;
180
+ const valueStart = index;
181
+ while (index < text.length && text[index] !== quote) index += 1;
182
+ value = text.slice(valueStart, index);
183
+ if (text[index] === quote) index += 1;
184
+ } else {
185
+ const valueStart = index;
186
+ while (index < text.length && !/[\s>]/.test(text[index])) index += 1;
187
+ value = text.slice(valueStart, index);
188
+ }
189
+ }
190
+
191
+ if (name) attrs.push({ name, value });
192
+ }
193
+ return attrs;
194
+ }
195
+
196
+ function extractHtmlTags(content) {
197
+ const tags = [];
198
+ const text = String(content || "");
199
+ let tagStart = -1;
200
+ let quote = null;
201
+
202
+ for (let index = 0; index < text.length; index += 1) {
203
+ const char = text[index];
204
+ if (tagStart === -1) {
205
+ if (char === "<" && /[!/a-z?]/i.test(text[index + 1] || "")) tagStart = index;
206
+ continue;
207
+ }
208
+
209
+ if (quote) {
210
+ if (char === quote) quote = null;
211
+ continue;
212
+ }
213
+ if (char === "\"" || char === "'") {
214
+ quote = char;
215
+ continue;
216
+ }
217
+ if (char !== ">") continue;
218
+
219
+ const tag = text.slice(tagStart, index + 1);
220
+ tags.push(tag);
221
+ const tagName = tag.match(/^<\s*([a-z0-9:-]+)/i)?.[1]?.toLowerCase();
222
+ tagStart = -1;
223
+ if (tagName === "script" || tagName === "style") {
224
+ const closePattern = new RegExp(`</\\s*${tagName}\\s*>`, "ig");
225
+ closePattern.lastIndex = index + 1;
226
+ const closeMatch = closePattern.exec(text);
227
+ if (closeMatch) index = closeMatch.index + closeMatch[0].length - 1;
228
+ }
229
+ }
230
+ return tags;
231
+ }
232
+
233
+ function assetKindForRef(value) {
234
+ const ext = extname(String(value || "").split(/[?#]/)[0]).toLowerCase();
235
+ if ([".png", ".jpg", ".jpeg", ".webp", ".avif", ".gif", ".svg", ".ico"].includes(ext)) return "image";
236
+ if ([".css"].includes(ext)) return "style";
237
+ if ([".js", ".mjs", ".json"].includes(ext)) return "script";
238
+ if ([".woff", ".woff2", ".ttf", ".otf", ".eot"].includes(ext)) return "font";
239
+ if ([".mp4", ".webm", ".mov", ".mp3", ".wav"].includes(ext)) return "media";
240
+ return "asset";
241
+ }
242
+
243
+ function resolveAssetRef(sourceRoot, originPath, rawRef) {
244
+ const cleaned = cleanRef(rawRef);
245
+ if (!cleaned || !looksLikeAssetRef(cleaned)) return null;
246
+
247
+ const rootRelative = cleaned.startsWith("/");
248
+ const normalized = cleaned.replace(/^\/+/, "");
249
+ const originDir = dirname(resolve(sourceRoot, originPath));
250
+ const primaryPath = rootRelative ? resolve(sourceRoot, normalized) : resolve(originDir, cleaned);
251
+ const fallbackPath = !rootRelative ? resolve(sourceRoot, normalized) : null;
252
+ const primaryExists = safeIsFile(primaryPath);
253
+ const fallbackExists = fallbackPath ? safeIsFile(fallbackPath) : false;
254
+ const sourceRootFallback = !primaryExists && fallbackExists;
255
+ const resolvedPath = primaryExists ? primaryPath : sourceRootFallback ? fallbackPath : primaryPath;
256
+ const insideSourceRoot = isInsideRoot(sourceRoot, resolvedPath);
257
+ const sourcePath = insideSourceRoot ? relFromRoot(sourceRoot, resolvedPath) : null;
258
+ const sourceExists = insideSourceRoot && (primaryExists || sourceRootFallback);
259
+ const pagekitAssetPath = sourcePath ? sourcePath.replace(/^assets\//i, "") : null;
260
+ const needsRewrite = rootRelative || /^\.?\.?\/?assets\//i.test(cleaned);
261
+
262
+ return {
263
+ raw: rawRef,
264
+ normalized,
265
+ asset_kind: assetKindForRef(normalized),
266
+ root_relative: rootRelative,
267
+ source_path: sourcePath,
268
+ source_exists: sourceExists,
269
+ outside_source_root: !insideSourceRoot,
270
+ source_root_fallback: sourceRootFallback,
271
+ pagekit_asset_path: pagekitAssetPath,
272
+ rewrite_required: needsRewrite,
273
+ rewrite_hint: needsRewrite && pagekitAssetPath
274
+ ? `Rewrite source asset ref "${cleaned}" to the Page Kit campaign asset root, e.g. campaign_asset "${pagekitAssetPath}".`
275
+ : null,
276
+ };
277
+ }
278
+
279
+ function safeIsFile(path) {
280
+ try {
281
+ return existsSync(path) && statSync(path).isFile();
282
+ } catch {
283
+ return false;
284
+ }
285
+ }
286
+
287
+ function referenceKey(ref) {
288
+ return [
289
+ ref.normalized,
290
+ ref.source_path || "",
291
+ ref.asset_kind,
292
+ ref.root_relative ? "root" : "relative",
293
+ ].join("\u0000");
294
+ }
295
+
296
+ function addReference(referencesByKey, ref, referencedBy) {
297
+ const key = referenceKey(ref);
298
+ const existing = referencesByKey.get(key);
299
+ if (existing) {
300
+ const duplicate = existing.referenced_by.some((item) =>
301
+ item.path === referencedBy.path && item.attribute === referencedBy.attribute
302
+ );
303
+ if (!duplicate) existing.referenced_by.push(referencedBy);
304
+ return existing;
305
+ }
306
+ const next = { ...ref, referenced_by: [referencedBy] };
307
+ referencesByKey.set(key, next);
308
+ return next;
309
+ }
310
+
311
+ function scannedFile(path, kind, stats, content) {
312
+ return {
313
+ path,
314
+ kind,
315
+ bytes: stats.size,
316
+ sha256: sha256Content(content),
317
+ };
318
+ }
319
+
320
+ function readScannableFile(path, kind, sourceRoot) {
321
+ const fullPath = resolve(sourceRoot, path);
322
+ let stats;
323
+ try {
324
+ stats = statSync(fullPath);
325
+ } catch (error) {
326
+ return { ok: false, warning: scanWarning(path, error) };
327
+ }
328
+ if (!stats.isFile()) return { ok: false, warning: null };
329
+ try {
330
+ const content = readFileSync(fullPath);
331
+ return {
332
+ ok: true,
333
+ content: content.toString("utf8"),
334
+ file: scannedFile(path, kind, stats, content),
335
+ };
336
+ } catch (error) {
337
+ return { ok: false, warning: scanWarning(path, error) };
338
+ }
339
+ }
340
+
341
+ function scanWarning(path, error) {
342
+ return {
343
+ code: "source_asset.scan_file_error",
344
+ message: `Could not scan source asset file "${path}": ${error.message}`,
345
+ sample: [path],
346
+ };
347
+ }
348
+
349
+ function pageIdsBySourcePath(pageMappings) {
350
+ const map = new Map();
351
+ for (const mapping of Array.isArray(pageMappings) ? pageMappings : []) {
352
+ if (!isNonEmptyString(mapping?.path) || !isNonEmptyString(mapping?.page_id)) continue;
353
+ const path = toPosixPath(mapping.path);
354
+ if (!map.has(path)) map.set(path, []);
355
+ map.get(path).push(mapping.page_id);
356
+ }
357
+ return map;
358
+ }
359
+
360
+ function summarizeWarnings(references) {
361
+ const warnings = [];
362
+ const rootAssetRefs = references.filter((ref) => ref.root_relative && /^assets\//i.test(ref.normalized));
363
+ const fallbackRefs = references.filter((ref) => ref.source_root_fallback);
364
+ const outsideRefs = references.filter((ref) => ref.outside_source_root);
365
+ const missingRefs = references.filter((ref) => !ref.source_exists && !ref.outside_source_root);
366
+ if (rootAssetRefs.length > 0) {
367
+ warnings.push({
368
+ code: "source_asset.root_assets_path",
369
+ message: `${rootAssetRefs.length} source asset reference(s) use raw /assets/... paths. Rewrite these for Page Kit's campaign asset root during assembly.`,
370
+ sample: rootAssetRefs.slice(0, 6).map((ref) => ref.raw),
371
+ });
372
+ }
373
+ if (fallbackRefs.length > 0) {
374
+ warnings.push({
375
+ code: "source_asset.source_root_fallback",
376
+ message: `${fallbackRefs.length} source asset reference(s) resolved through the source-root fallback instead of the HTML-file-relative path. Confirm these refs are intended before assembly.`,
377
+ sample: fallbackRefs.slice(0, 6).map((ref) => ref.raw),
378
+ });
379
+ }
380
+ if (outsideRefs.length > 0) {
381
+ warnings.push({
382
+ code: "source_asset.outside_source_root",
383
+ message: `${outsideRefs.length} source asset reference(s) resolve outside the source root. Move these files into the source handoff before assembly.`,
384
+ sample: outsideRefs.slice(0, 6).map((ref) => ref.raw),
385
+ });
386
+ }
387
+ if (missingRefs.length > 0) {
388
+ warnings.push({
389
+ code: "source_asset.missing_file",
390
+ message: `${missingRefs.length} local source asset reference(s) did not resolve to files under the source root.`,
391
+ sample: missingRefs.slice(0, 6).map((ref) => ref.raw),
392
+ });
393
+ }
394
+ return warnings;
395
+ }
396
+
397
+ export function crawlSourceAssetPaths({ sourceRoot, htmlFiles = [], pageMappings = [], maxCssFiles = DEFAULT_MAX_CSS_FILES }) {
398
+ const root = resolve(sourceRoot);
399
+ const referencesByKey = new Map();
400
+ const scannedByPath = new Map();
401
+ const cssQueue = [];
402
+ const queuedCss = new Set();
403
+ const scanWarnings = [];
404
+ const pageIds = pageIdsBySourcePath(pageMappings);
405
+
406
+ function enqueueCss(sourcePath) {
407
+ if (!sourcePath || queuedCss.has(sourcePath)) return;
408
+ if (!existsSync(resolve(root, sourcePath))) return;
409
+ if (queuedCss.size >= maxCssFiles) {
410
+ if (!scanWarnings.some((warning) => warning.code === "source_asset.css_queue_truncated")) {
411
+ scanWarnings.push({
412
+ code: "source_asset.css_queue_truncated",
413
+ message: `Source asset CSS crawl reached the ${maxCssFiles} file cap. Remaining CSS imports were skipped.`,
414
+ sample: [sourcePath],
415
+ });
416
+ }
417
+ return;
418
+ }
419
+ queuedCss.add(sourcePath);
420
+ cssQueue.push(sourcePath);
421
+ }
422
+
423
+ function scanFile(sourcePath, kind) {
424
+ const scanned = readScannableFile(sourcePath, kind, root);
425
+ if (!scanned.ok) {
426
+ if (scanned.warning) scanWarnings.push(scanned.warning);
427
+ return;
428
+ }
429
+ if (!scannedByPath.has(sourcePath)) scannedByPath.set(sourcePath, scanned.file);
430
+
431
+ const extracted = kind === "css" ? extractCssRefs(scanned.content) : extractHtmlRefs(scanned.content);
432
+ for (const item of extracted) {
433
+ const ref = resolveAssetRef(root, sourcePath, item.raw);
434
+ if (!ref) continue;
435
+ const stored = addReference(referencesByKey, ref, {
436
+ path: sourcePath,
437
+ kind,
438
+ attribute: item.attribute,
439
+ page_ids: pageIds.get(sourcePath) || [],
440
+ });
441
+ if (stored.asset_kind === "style" && stored.source_exists && stored.source_path) enqueueCss(stored.source_path);
442
+ }
443
+ }
444
+
445
+ const sourceHtmlPaths = [...new Set((htmlFiles || []).map((file) => toPosixPath(file.path)).filter(Boolean))];
446
+ for (const sourcePath of sourceHtmlPaths) scanFile(sourcePath, "html");
447
+ while (cssQueue.length > 0) scanFile(cssQueue.shift(), "css");
448
+
449
+ const references = [...referencesByKey.values()].sort((a, b) =>
450
+ String(a.source_path || a.normalized).localeCompare(String(b.source_path || b.normalized))
451
+ );
452
+ const scanned_files = [...scannedByPath.values()].sort((a, b) => a.path.localeCompare(b.path));
453
+
454
+ return {
455
+ schema_version: SOURCE_ASSET_CRAWL_SCHEMA,
456
+ scanned_files,
457
+ references,
458
+ summary: {
459
+ scanned_file_count: scanned_files.length,
460
+ reference_count: references.length,
461
+ missing_count: references.filter((ref) => !ref.source_exists).length,
462
+ outside_source_root_count: references.filter((ref) => ref.outside_source_root).length,
463
+ source_root_fallback_count: references.filter((ref) => ref.source_root_fallback).length,
464
+ rewrite_required_count: references.filter((ref) => ref.rewrite_required).length,
465
+ root_assets_path_count: references.filter((ref) => ref.root_relative && /^assets\//i.test(ref.normalized)).length,
466
+ },
467
+ warnings: [...scanWarnings, ...summarizeWarnings(references)],
468
+ };
469
+ }