@pradip1995/framework-compiler 0.2.4 → 0.3.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.
@@ -0,0 +1,190 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * backend-build — reads plugins.config.json + backend.config.json,
4
+ * generates Medusa backend into .generated-backend/ (gitignored).
5
+ */
6
+ import {
7
+ cpSync,
8
+ existsSync,
9
+ mkdirSync,
10
+ readFileSync,
11
+ readdirSync,
12
+ rmSync,
13
+ writeFileSync,
14
+ } from "fs"
15
+ import { join, dirname, resolve } from "path"
16
+ import { fileURLToPath } from "url"
17
+ import { createRequire } from "module"
18
+
19
+ const __dirname = dirname(fileURLToPath(import.meta.url))
20
+ const require = createRequire(import.meta.url)
21
+ const COMPILER_ROOT = join(__dirname, "..")
22
+ const TEMPLATE_ROOT = join(COMPILER_ROOT, "backend-templates")
23
+
24
+ const { generateMedusaConfig } = require(join(TEMPLATE_ROOT, "generate-medusa-config.cjs"))
25
+ const {
26
+ buildBackendPackageJson,
27
+ resolveEnabledPlugins,
28
+ resolveDependencies,
29
+ } = require(join(TEMPLATE_ROOT, "generate-backend-package.cjs"))
30
+ const { buildHomepageConfigDefaults } = require(
31
+ join(TEMPLATE_ROOT, "scripts", "build-homepage-defaults.cjs")
32
+ )
33
+
34
+ const clientDir = resolve(process.cwd())
35
+ const outDir = join(clientDir, ".generated-backend")
36
+
37
+ function loadJson(path) {
38
+ return JSON.parse(readFileSync(path, "utf8"))
39
+ }
40
+
41
+ function applyTemplate(content, vars) {
42
+ return content.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] ?? "")
43
+ }
44
+
45
+ function copyDir(src, dest, { skip = [] } = {}) {
46
+ if (!existsSync(src)) return
47
+ mkdirSync(dest, { recursive: true })
48
+ for (const entry of readdirSync(src, { withFileTypes: true })) {
49
+ if (skip.includes(entry.name)) continue
50
+ const from = join(src, entry.name)
51
+ const to = join(dest, entry.name)
52
+ if (entry.isDirectory()) {
53
+ copyDir(from, to, { skip })
54
+ } else {
55
+ cpSync(from, to)
56
+ }
57
+ }
58
+ }
59
+
60
+ function resolveTemplateVars(backendConfig) {
61
+ const defaultRegion = backendConfig.defaultRegion || "in"
62
+ const regionCurrency = {
63
+ in: { currency: "inr", name: "India" },
64
+ us: { currency: "usd", name: "United States" },
65
+ gb: { currency: "gbp", name: "United Kingdom" },
66
+ eu: { currency: "eur", name: "Europe" },
67
+ }
68
+ const meta = regionCurrency[defaultRegion] || {
69
+ currency: defaultRegion.length === 2 ? defaultRegion : "usd",
70
+ name: defaultRegion.toUpperCase(),
71
+ }
72
+
73
+ return {
74
+ DEFAULT_REGION: defaultRegion,
75
+ DEFAULT_CURRENCY: meta.currency,
76
+ DEFAULT_REGION_NAME: meta.name,
77
+ }
78
+ }
79
+
80
+ function preserveHomepageConfig() {
81
+ const configDir = join(outDir, "config")
82
+ const homepagePath = join(configDir, "homepage-config.json")
83
+ const defaultsPath = join(configDir, "homepage-config.defaults.json")
84
+ if (!existsSync(homepagePath)) return null
85
+ return {
86
+ homepage: readFileSync(homepagePath, "utf8"),
87
+ defaults: existsSync(defaultsPath) ? readFileSync(defaultsPath, "utf8") : null,
88
+ }
89
+ }
90
+
91
+ function restoreHomepageConfig(configDir, preserved) {
92
+ if (!preserved) return
93
+ writeFileSync(join(configDir, "homepage-config.json"), preserved.homepage)
94
+ if (preserved.defaults) {
95
+ writeFileSync(join(configDir, "homepage-config.defaults.json"), preserved.defaults)
96
+ } else {
97
+ const schema = JSON.parse(preserved.homepage)
98
+ writeFileSync(
99
+ join(configDir, "homepage-config.defaults.json"),
100
+ `${JSON.stringify(buildHomepageConfigDefaults(schema), null, 2)}\n`
101
+ )
102
+ }
103
+ }
104
+
105
+ function main() {
106
+ console.log("backend build")
107
+ console.log(" client:", clientDir)
108
+ console.log(" output:", outDir)
109
+
110
+ const pluginsConfigPath = join(clientDir, "plugins.config.json")
111
+ const backendConfigPath = join(clientDir, "backend.config.json")
112
+
113
+ if (!existsSync(pluginsConfigPath)) {
114
+ console.error("Missing plugins.config.json in", clientDir)
115
+ process.exit(1)
116
+ }
117
+ if (!existsSync(backendConfigPath)) {
118
+ console.error("Missing backend.config.json in", clientDir)
119
+ process.exit(1)
120
+ }
121
+
122
+ const pluginsConfig = loadJson(pluginsConfigPath)
123
+ const backendConfig = loadJson(backendConfigPath)
124
+ const defaults = loadJson(join(TEMPLATE_ROOT, "medusa-plugin-versions.json"))
125
+
126
+ const enabledPlugins = resolveEnabledPlugins(pluginsConfig, backendConfig, defaults)
127
+ const { medusa, plugins } = resolveDependencies(pluginsConfig, enabledPlugins, defaults)
128
+ const preset = backendConfig.preset || "full"
129
+ const templateVars = resolveTemplateVars(backendConfig)
130
+
131
+ const preservedHomepage = preserveHomepageConfig()
132
+
133
+ if (existsSync(outDir)) {
134
+ rmSync(outDir, { recursive: true, force: true })
135
+ }
136
+ mkdirSync(outDir, { recursive: true })
137
+
138
+ copyDir(join(TEMPLATE_ROOT, "src"), join(outDir, "src"))
139
+ copyDir(join(TEMPLATE_ROOT, "scripts"), join(outDir, "scripts"))
140
+ cpSync(join(TEMPLATE_ROOT, "tsconfig.json"), join(outDir, "tsconfig.json"))
141
+
142
+ const seedContent = applyTemplate(
143
+ readFileSync(join(TEMPLATE_ROOT, "src", "scripts", "seed.ts"), "utf8"),
144
+ templateVars
145
+ )
146
+ writeFileSync(join(outDir, "src", "scripts", "seed.ts"), seedContent)
147
+
148
+ const configDir = join(outDir, "config")
149
+ mkdirSync(configDir, { recursive: true })
150
+ if (preservedHomepage) {
151
+ restoreHomepageConfig(configDir, preservedHomepage)
152
+ } else {
153
+ cpSync(join(TEMPLATE_ROOT, "config", "homepage-config.json"), join(configDir, "homepage-config.json"))
154
+ const schema = loadJson(join(configDir, "homepage-config.json"))
155
+ writeFileSync(
156
+ join(configDir, "homepage-config.defaults.json"),
157
+ `${JSON.stringify(buildHomepageConfigDefaults(schema), null, 2)}\n`
158
+ )
159
+ }
160
+
161
+ writeFileSync(
162
+ join(outDir, "medusa-config.ts"),
163
+ generateMedusaConfig({ enabledPlugins, preset })
164
+ )
165
+
166
+ writeFileSync(
167
+ join(outDir, "package.json"),
168
+ `${JSON.stringify(
169
+ buildBackendPackageJson({
170
+ medusa,
171
+ plugins,
172
+ projectName: backendConfig.projectName || "store",
173
+ }),
174
+ null,
175
+ 2
176
+ )}\n`
177
+ )
178
+
179
+ writeFileSync(join(outDir, ".npmrc"), "legacy-peer-deps=true\n")
180
+
181
+ const envSource = join(clientDir, ".env")
182
+ if (existsSync(envSource)) {
183
+ cpSync(envSource, join(outDir, ".env"))
184
+ }
185
+
186
+ console.log(" plugins:", enabledPlugins.length)
187
+ console.log("Done.")
188
+ }
189
+
190
+ main()
@@ -132,9 +132,7 @@ function main() {
132
132
  // Copy static assets: plugin public/ dirs, then client overrides
133
133
  copyPublicAssets(packages, clientDir, outDir)
134
134
 
135
- console.log(
136
- " dynamic-config: schema in backend/config/homepage-config.json — storefront fetches GET /store/dynamic-config"
137
- )
135
+ syncBackendDynamicConfigFromBuild(pagesConfig)
138
136
 
139
137
  console.log(" generated:", packages.segments.length, "segments,", packages.layouts.length, "layouts,", packages.workflows.length, "workflows")
140
138
  console.log("Done.")
@@ -274,6 +272,34 @@ function copyPublicAssets(packages, clientDir, outDir) {
274
272
  }
275
273
  }
276
274
 
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
+ })
283
+
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
+ )
297
+ }
298
+ } catch (error) {
299
+ console.warn(` dynamic-config: backend sync skipped (${error.message})`)
300
+ }
301
+ }
302
+
277
303
  function loadPagesConfig(clientDir) {
278
304
  const jsonPath = join(clientDir, "pages.config.json")
279
305
  if (existsSync(jsonPath)) {
@@ -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,124 @@
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 backendRoot = join(clientDir, "..", "backend")
75
+ const configOnlyLayout = existsSync(join(backendRoot, "plugins.config.json"))
76
+ const backendConfigDir = configOnlyLayout
77
+ ? join(backendRoot, ".generated-backend", "config")
78
+ : join(backendRoot, "config")
79
+ const backendConfigPath = join(backendConfigDir, "homepage-config.json")
80
+ if (!configOnlyLayout && !existsSync(dirname(backendConfigPath))) {
81
+ return { synced: false }
82
+ }
83
+
84
+ const clientOverride = join(clientDir, "dynamic-config.schema.json")
85
+ let fullSchema
86
+ if (existsSync(clientOverride)) {
87
+ const payload = loadJson(clientOverride)
88
+ fullSchema = payload.configs?.["homepage-config"] || payload["homepage-config"] || payload
89
+ } else {
90
+ fullSchema = loadJson(join(schemaDir, "schemas", "homepage-config.json"))
91
+ }
92
+
93
+ const fieldMap = loadJson(join(schemaDir, "segment-field-map.json"))
94
+ const requiredIds = collectRequiredStructureIds(pagesConfig, fieldMap)
95
+ const filtered = filterHomepageSchema(fullSchema, requiredIds)
96
+
97
+ const warnings = []
98
+ const knownSegments = new Set(Object.keys(fieldMap.segments || {}))
99
+ for (const page of pagesConfig) {
100
+ for (const segment of page.segments || []) {
101
+ if (!knownSegments.has(segment)) {
102
+ warnings.push(`no dynamic-config mapping for segment ${segment}`)
103
+ }
104
+ }
105
+ }
106
+
107
+ mkdirSync(backendConfigDir, { recursive: true })
108
+ writeFileSync(backendConfigPath, `${JSON.stringify(filtered, null, 2)}\n`)
109
+
110
+ const defaults = buildHomepageDefaults(filtered)
111
+ writeFileSync(
112
+ join(backendConfigDir, "homepage-config.defaults.json"),
113
+ `${JSON.stringify(defaults, null, 2)}\n`
114
+ )
115
+
116
+ return {
117
+ synced: true,
118
+ fieldCount: filtered.structure.length,
119
+ warnings,
120
+ backendConfigPath,
121
+ }
122
+ }
123
+
124
+ module.exports = { syncBackendDynamicConfig, collectRequiredStructureIds, filterHomepageSchema }
package/package.json CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "name": "@pradip1995/framework-compiler",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
9
  "bin": {
10
- "storefront-build": "./bin/storefront-build.js"
10
+ "storefront-build": "./bin/storefront-build.js",
11
+ "backend-build": "./bin/backend-build.js"
11
12
  },
12
13
  "files": [
13
14
  "bin",
14
15
  "templates",
16
+ "backend-templates",
15
17
  "dynamic-config-schema"
16
18
  ],
17
19
  "dependencies": {