@pradip1995/framework-compiler 0.2.3 → 0.2.6

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.
@@ -132,7 +132,7 @@ function main() {
132
132
  // Copy static assets: plugin public/ dirs, then client overrides
133
133
  copyPublicAssets(packages, clientDir, outDir)
134
134
 
135
- generateDynamicConfigSchema(clientDir, outDir)
135
+ syncBackendDynamicConfigFromBuild(pagesConfig)
136
136
 
137
137
  console.log(" generated:", packages.segments.length, "segments,", packages.layouts.length, "layouts,", packages.workflows.length, "workflows")
138
138
  console.log("Done.")
@@ -272,83 +272,32 @@ function copyPublicAssets(packages, clientDir, outDir) {
272
272
  }
273
273
  }
274
274
 
275
- function resolveDynamicConfigSchemaDir() {
276
- const bundled = join(__dirname, "..", "dynamic-config-schema")
277
- if (existsSync(join(bundled, "schemas", "homepage-config.json"))) return bundled
278
-
279
- const candidates = [
280
- join(__dirname, "..", "dynamic-config-schema"),
281
- join(__dirname, "..", "..", "dynamic-config-schema"),
282
- ]
283
- for (const candidate of candidates) {
284
- if (existsSync(join(candidate, "schemas", "homepage-config.json"))) return candidate
285
- }
286
- let dir = dirname(__dirname)
287
- for (let depth = 0; depth < 6; depth++) {
288
- const candidate = join(dir, "packages", "dynamic-config-schema")
289
- if (existsSync(join(candidate, "schemas", "homepage-config.json"))) return candidate
290
- const parent = dirname(dir)
291
- if (parent === dir) break
292
- dir = parent
293
- }
294
- return null
295
- }
275
+ function syncBackendDynamicConfigFromBuild(pagesConfig) {
276
+ try {
277
+ const { syncBackendDynamicConfig } = require("../dynamic-config-schema/sync-backend-schema.cjs")
278
+ const result = syncBackendDynamicConfig({
279
+ clientDir,
280
+ pagesConfig,
281
+ compilerDir: join(__dirname, ".."),
282
+ })
296
283
 
297
- /**
298
- * Emit Medusa dynamic-config schema for backend registration.
299
- * Output: dynamic-config.generated.json with { configs: { "homepage-config": ... } }
300
- *
301
- * Client override: place dynamic-config.schema.json in client root (full { configs } or bare config map).
302
- */
303
- function generateDynamicConfigSchema(clientDir, outDir) {
304
- const clientOverride = join(clientDir, "dynamic-config.schema.json")
305
- let payload
306
-
307
- if (existsSync(clientOverride)) {
308
- payload = JSON.parse(readFileSync(clientOverride, "utf8"))
309
- if (!payload.configs) {
310
- payload = { configs: payload }
311
- }
312
- console.log(" dynamic-config: using client dynamic-config.schema.json")
313
- } else {
314
- const schemaDir = resolveDynamicConfigSchemaDir()
315
- if (!schemaDir) {
316
- console.warn(" dynamic-config: schema package not found — skip generation")
317
- return
284
+ if (result.synced) {
285
+ console.log(
286
+ ` dynamic-config: synced ${result.fieldCount} field(s) to backend/config/homepage-config.json`
287
+ )
288
+ for (const warning of result.warnings || []) {
289
+ console.warn(` dynamic-config: ${warning}`)
290
+ }
291
+ } else if (result.warnings?.length) {
292
+ console.warn(` dynamic-config: ${result.warnings.join("; ")}`)
293
+ } else {
294
+ console.log(
295
+ " dynamic-config: no ../backend/ — storefront reads GET /store/dynamic-config at runtime"
296
+ )
318
297
  }
319
- const homepage = JSON.parse(
320
- readFileSync(join(schemaDir, "schemas", "homepage-config.json"), "utf8")
321
- )
322
- payload = { configs: { "homepage-config": homepage } }
323
- console.log(" dynamic-config: from packages/dynamic-config-schema")
298
+ } catch (error) {
299
+ console.warn(` dynamic-config: backend sync skipped (${error.message})`)
324
300
  }
325
-
326
- const schemaDir = resolveDynamicConfigSchemaDir()
327
- const homepageSchema = payload.configs?.["homepage-config"]
328
- let homepageDefaults = {}
329
-
330
- if (homepageSchema && schemaDir) {
331
- const defaultsPath = existsSync(join(schemaDir, "build-defaults.cjs"))
332
- ? join(schemaDir, "build-defaults.cjs")
333
- : join(schemaDir, "build-defaults.js")
334
- const { buildHomepageConfigDefaults } = require(defaultsPath)
335
- homepageDefaults = buildHomepageConfigDefaults(homepageSchema)
336
- }
337
-
338
- payload.defaults = {
339
- ...(payload.defaults || {}),
340
- "homepage-config": homepageDefaults,
341
- }
342
-
343
- const json = `${JSON.stringify(payload, null, 2)}\n`
344
- writeFileSync(join(clientDir, "dynamic-config.generated.json"), json)
345
- writeFileSync(join(outDir, "dynamic-config.generated.json"), json)
346
-
347
- const defaultsJson = `${JSON.stringify({ "homepage-config": homepageDefaults }, null, 2)}\n`
348
- writeFileSync(join(clientDir, "dynamic-config.defaults.json"), defaultsJson)
349
- writeFileSync(join(outDir, "dynamic-config.defaults.json"), defaultsJson)
350
- console.log(" dynamic-config: wrote dynamic-config.generated.json")
351
- console.log(" dynamic-config: wrote dynamic-config.defaults.json")
352
301
  }
353
302
 
354
303
  function loadPagesConfig(clientDir) {
@@ -0,0 +1,53 @@
1
+ {
2
+ "always": [
3
+ "logo",
4
+ "website-description",
5
+ "promo-bar",
6
+ "social-links",
7
+ "footer-shop-label",
8
+ "footer-company-label",
9
+ "footer-newsletter-label",
10
+ "footer-newsletter-description"
11
+ ],
12
+ "segments": {
13
+ "@pradip1995/segment-hero": ["homepage-banner-array", "app-banner-array"],
14
+ "@pradip1995/segment-promotional-banners": ["promotional-banner-array"],
15
+ "@pradip1995/segment-why-choose-us": ["why-choose-us-title", "why-choose-us-features"],
16
+ "@pradip1995/segment-features": ["trust-strip-features"],
17
+ "@pradip1995/segment-reviews-marquee": [
18
+ "rating-title",
19
+ "rating-eyebrow",
20
+ "rating-description",
21
+ "ratings"
22
+ ],
23
+ "@pradip1995/segment-new-arrivals": [
24
+ "new-arrivals-title",
25
+ "new-arrivals-view-all",
26
+ "new-arrivals-empty-message"
27
+ ],
28
+ "@pradip1995/segment-bestsellers-carousel": ["bestsellers-title", "bestsellers-view-all"],
29
+ "@pradip1995/segment-loved-by-moms": ["loved-by-moms-title", "loved-by-moms-view-all"],
30
+ "@pradip1995/segment-shop-by-category": ["shop-by-category-title", "shop-by-category-cta"],
31
+ "@pradip1995/segment-shop-by-age": ["shop-by-age-title"],
32
+ "@pradip1995/segment-collections-showcase": [
33
+ "collections-showcase-eyebrow",
34
+ "collections-showcase-title"
35
+ ],
36
+ "@pradip1995/segment-related-products": ["related-products-title"],
37
+ "@pradip1995/segment-help": ["faq-array", "contact-us"]
38
+ },
39
+ "workflows": {
40
+ "@pradip1995/workflow-store": ["store-page-title", "store-page-empty-message"],
41
+ "@pradip1995/workflow-cart": ["cart-summary-title", "cart-empty-message", "cart-empty-button"],
42
+ "@pradip1995/workflow-checkout": ["checkout-form-title", "checkout-summary-title"],
43
+ "@pradip1995/workflow-wishlist": [
44
+ "wishlist-sign-in-title",
45
+ "wishlist-sign-in-description",
46
+ "wishlist-sign-in-button",
47
+ "wishlist-empty-title",
48
+ "wishlist-empty-description",
49
+ "wishlist-empty-button"
50
+ ],
51
+ "@pradip1995/workflow-help": ["faq-array", "contact-us"]
52
+ }
53
+ }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Filter homepage-config schema from pages.config segments/workflows
3
+ * and sync to ../backend/config/ (monorepo scaffold layout).
4
+ */
5
+ const { existsSync, readFileSync, writeFileSync, mkdirSync } = require("fs")
6
+ const { join, dirname } = require("path")
7
+
8
+ function resolveSchemaBundleDir(compilerDir) {
9
+ const compilerBundle = join(compilerDir, "dynamic-config-schema")
10
+ if (existsSync(join(compilerBundle, "schemas", "homepage-config.json"))) {
11
+ return compilerBundle
12
+ }
13
+
14
+ const bundled = join(__dirname, "..")
15
+ if (existsSync(join(bundled, "schemas", "homepage-config.json"))) return bundled
16
+
17
+ let dir = compilerDir
18
+ for (let depth = 0; depth < 8; depth++) {
19
+ const candidate = join(dir, "packages", "framework-compiler", "dynamic-config-schema")
20
+ if (existsSync(join(candidate, "schemas", "homepage-config.json"))) return candidate
21
+ const candidate2 = join(dir, "packages", "dynamic-config-schema")
22
+ if (existsSync(join(candidate2, "schemas", "homepage-config.json"))) return candidate2
23
+ const parent = dirname(dir)
24
+ if (parent === dir) break
25
+ dir = parent
26
+ }
27
+ return null
28
+ }
29
+
30
+ function loadJson(path) {
31
+ return JSON.parse(readFileSync(path, "utf8"))
32
+ }
33
+
34
+ function collectRequiredStructureIds(pagesConfig, fieldMap) {
35
+ const required = new Set(fieldMap.always || [])
36
+
37
+ for (const page of pagesConfig) {
38
+ const workflowFields = fieldMap.workflows?.[page.workflow]
39
+ if (workflowFields) workflowFields.forEach((id) => required.add(id))
40
+
41
+ for (const segment of page.segments || []) {
42
+ const segmentFields = fieldMap.segments?.[segment]
43
+ if (segmentFields) {
44
+ segmentFields.forEach((id) => required.add(id))
45
+ }
46
+ }
47
+ }
48
+
49
+ return required
50
+ }
51
+
52
+ function filterHomepageSchema(fullSchema, requiredIds) {
53
+ const structure = (fullSchema.structure || []).filter((field) => requiredIds.has(field.id))
54
+ return { ...fullSchema, structure }
55
+ }
56
+
57
+ function buildHomepageDefaults(schema) {
58
+ const defaultsPath = join(__dirname, "build-defaults.cjs")
59
+ const { buildHomepageConfigDefaults } = require(defaultsPath)
60
+ return buildHomepageConfigDefaults(schema)
61
+ }
62
+
63
+ /**
64
+ * @param {{ clientDir: string, pagesConfig: object[], compilerDir?: string }} options
65
+ * @returns {{ synced: boolean, fieldCount?: number, warnings?: string[] }}
66
+ */
67
+ function syncBackendDynamicConfig({ clientDir, pagesConfig, compilerDir }) {
68
+ const fromDir = compilerDir || join(__dirname, "..")
69
+ const schemaDir = resolveSchemaBundleDir(fromDir)
70
+ if (!schemaDir) {
71
+ return { synced: false, warnings: ["dynamic-config schema bundle not found"] }
72
+ }
73
+
74
+ const backendConfigDir = join(clientDir, "..", "backend", "config")
75
+ const backendConfigPath = join(backendConfigDir, "homepage-config.json")
76
+ if (!existsSync(dirname(backendConfigPath))) {
77
+ return { synced: false }
78
+ }
79
+
80
+ const clientOverride = join(clientDir, "dynamic-config.schema.json")
81
+ let fullSchema
82
+ if (existsSync(clientOverride)) {
83
+ const payload = loadJson(clientOverride)
84
+ fullSchema = payload.configs?.["homepage-config"] || payload["homepage-config"] || payload
85
+ } else {
86
+ fullSchema = loadJson(join(schemaDir, "schemas", "homepage-config.json"))
87
+ }
88
+
89
+ const fieldMap = loadJson(join(schemaDir, "segment-field-map.json"))
90
+ const requiredIds = collectRequiredStructureIds(pagesConfig, fieldMap)
91
+ const filtered = filterHomepageSchema(fullSchema, requiredIds)
92
+
93
+ const warnings = []
94
+ const knownSegments = new Set(Object.keys(fieldMap.segments || {}))
95
+ for (const page of pagesConfig) {
96
+ for (const segment of page.segments || []) {
97
+ if (!knownSegments.has(segment)) {
98
+ warnings.push(`no dynamic-config mapping for segment ${segment}`)
99
+ }
100
+ }
101
+ }
102
+
103
+ mkdirSync(backendConfigDir, { recursive: true })
104
+ writeFileSync(backendConfigPath, `${JSON.stringify(filtered, null, 2)}\n`)
105
+
106
+ const defaults = buildHomepageDefaults(filtered)
107
+ writeFileSync(
108
+ join(backendConfigDir, "homepage-config.defaults.json"),
109
+ `${JSON.stringify(defaults, null, 2)}\n`
110
+ )
111
+
112
+ return {
113
+ synced: true,
114
+ fieldCount: filtered.structure.length,
115
+ warnings,
116
+ backendConfigPath,
117
+ }
118
+ }
119
+
120
+ module.exports = { syncBackendDynamicConfig, collectRequiredStructureIds, filterHomepageSchema }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pradip1995/framework-compiler",
3
- "version": "0.2.3",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "publishConfig": {