@pradip1995/framework-compiler 0.2.6 → 0.3.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.
@@ -0,0 +1,60 @@
1
+ function buildBackendPackageJson({ medusa, plugins, projectName = "backend" }) {
2
+ return {
3
+ name: `${projectName}-backend`,
4
+ version: "0.0.1",
5
+ private: true,
6
+ description: "Generated Medusa backend (do not edit — run backend-build)",
7
+ license: "MIT",
8
+ scripts: {
9
+ dev: "medusa develop",
10
+ build:
11
+ "TS_NODE_COMPILER=typescript TS_NODE_TRANSPILE_ONLY=true MEDUSA_ADMIN_DISABLE_SW=true medusa build",
12
+ start: "medusa start",
13
+ seed: "medusa exec ./src/scripts/seed.ts",
14
+ "dynamic-config:defaults": "node scripts/build-dynamic-config-defaults.mjs",
15
+ },
16
+ dependencies: {
17
+ ...medusa,
18
+ ...plugins,
19
+ },
20
+ devDependencies: {
21
+ "@types/node": "^20.12.11",
22
+ typescript: "^5.6.2",
23
+ "ts-node": "^10.9.2",
24
+ },
25
+ engines: {
26
+ node: ">=20",
27
+ },
28
+ }
29
+ }
30
+
31
+ function resolveEnabledPlugins(pluginsConfig, backendConfig, defaults) {
32
+ if (Array.isArray(pluginsConfig.enabled) && pluginsConfig.enabled.length > 0) {
33
+ return pluginsConfig.enabled
34
+ }
35
+
36
+ const preset = backendConfig.preset || "full"
37
+ const presetList = preset === "minimal" ? defaults.minimalPlugins : defaults.fullPlugins
38
+ return presetList
39
+ }
40
+
41
+ function resolveDependencies(pluginsConfig, enabledPlugins, defaults) {
42
+ const medusa = { ...(pluginsConfig.medusa || defaults.medusa) }
43
+ const plugins = {}
44
+
45
+ for (const key of enabledPlugins) {
46
+ const version = pluginsConfig.plugins?.[key] ?? defaults.plugins?.[key]
47
+ if (!version) {
48
+ throw new Error(`No version for plugin "${key}" in plugins.config.json or framework defaults`)
49
+ }
50
+ plugins[key] = version
51
+ }
52
+
53
+ return { medusa, plugins }
54
+ }
55
+
56
+ module.exports = {
57
+ buildBackendPackageJson,
58
+ resolveEnabledPlugins,
59
+ resolveDependencies,
60
+ }
@@ -0,0 +1,269 @@
1
+ const pluginDefinitions = require("./plugin-definitions.cjs")
2
+
3
+ function formatObjectKey(key) {
4
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : JSON.stringify(key)
5
+ }
6
+
7
+ function formatValue(value, indent = 4) {
8
+ const pad = " ".repeat(indent)
9
+ if (value === "homepageConfig") return "homepageConfig"
10
+ if (value === "storefrontUrl") return "storefrontUrl"
11
+ if (value === "PRODUCT_METADATA_DESCRIPTORS") return "PRODUCT_METADATA_DESCRIPTORS"
12
+ if (typeof value === "string" && value.startsWith("process.env.")) return value
13
+ if (typeof value === "string" && value.includes("process.env.")) return value
14
+ if (typeof value === "string") return JSON.stringify(value)
15
+ if (typeof value === "number" || typeof value === "boolean") return String(value)
16
+ if (Array.isArray(value)) {
17
+ if (value.length === 0) return "[]"
18
+ return `[\n${value.map((item) => `${pad} ${formatValue(item, indent + 2)}`).join(",\n")}\n${pad}]`
19
+ }
20
+ if (value && typeof value === "object") {
21
+ const entries = Object.entries(value)
22
+ return `{\n${entries
23
+ .map(([key, val]) => `${pad} ${formatObjectKey(key)}: ${formatValue(val, indent + 2)}`)
24
+ .join(",\n")}\n${pad}}`
25
+ }
26
+ return "undefined"
27
+ }
28
+
29
+ function formatPluginEntry(key, definition) {
30
+ const lines = [" {"]
31
+ if (definition.resolveSpecial === "medusa-analytics") {
32
+ lines.push(
33
+ ' resolve: path.dirname(require.resolve("medusa-analytics/package.json")),',
34
+ ` options: ${formatValue(definition.options, 6)},`
35
+ )
36
+ } else {
37
+ lines.push(` resolve: ${JSON.stringify(definition.resolve)},`)
38
+ lines.push(` options: ${formatValue(definition.options, 6)},`)
39
+ }
40
+ lines.push(" }")
41
+ return lines.join("\n")
42
+ }
43
+
44
+ function buildPluginsBlock(enabledPlugins) {
45
+ const entries = []
46
+ for (const key of enabledPlugins) {
47
+ const definition = pluginDefinitions[key]
48
+ if (!definition) {
49
+ throw new Error(`Unknown plugin "${key}" — add it to plugin-definitions.cjs or remove from plugins.config.json`)
50
+ }
51
+ entries.push(formatPluginEntry(key, definition))
52
+ }
53
+ return entries.join(",\n")
54
+ }
55
+
56
+ function hasPlugin(enabledPlugins, name) {
57
+ return enabledPlugins.includes(name)
58
+ }
59
+
60
+ function generateMedusaConfig({ enabledPlugins, preset = "full" }) {
61
+ const useShiprocket =
62
+ preset === "full" && hasPlugin(enabledPlugins, "medusa-shiprocket-fulfillment-sbl")
63
+ const usePaymentProvider =
64
+ preset === "full" && hasPlugin(enabledPlugins, "medusa-payment-provider")
65
+ const useCustomerRegistration = hasPlugin(enabledPlugins, "customer-registration")
66
+
67
+ const fulfillmentProviders = useShiprocket
68
+ ? ` providers: [
69
+ { resolve: "@medusajs/medusa/fulfillment-manual", id: "manual" },
70
+ ...(process.env.SHIPROCKET_EMAIL
71
+ ? [
72
+ {
73
+ resolve: "medusa-shiprocket-fulfillment-sbl",
74
+ id: "shiprocket",
75
+ },
76
+ ]
77
+ : []),
78
+ ],`
79
+ : ` providers: [{ resolve: "@medusajs/medusa/fulfillment-manual", id: "manual" }],`
80
+
81
+ const authProviders = useCustomerRegistration
82
+ ? ` providers: [
83
+ { resolve: "@medusajs/medusa/auth-emailpass", id: "emailpass" },
84
+ { resolve: "customer-registration/providers/phonepass", id: "phonepass" },
85
+ ...(process.env.GOOGLE_CLIENT_ID
86
+ ? [
87
+ {
88
+ resolve: "@medusajs/auth-google",
89
+ id: "google",
90
+ options: {
91
+ clientId: process.env.GOOGLE_CLIENT_ID,
92
+ clientSecret: process.env.GOOGLE_CLIENT_SECRET,
93
+ },
94
+ },
95
+ ]
96
+ : []),
97
+ ],`
98
+ : ` providers: [
99
+ { resolve: "@medusajs/medusa/auth-emailpass", id: "emailpass" },
100
+ ...(process.env.GOOGLE_CLIENT_ID
101
+ ? [
102
+ {
103
+ resolve: "@medusajs/auth-google",
104
+ id: "google",
105
+ options: {
106
+ clientId: process.env.GOOGLE_CLIENT_ID,
107
+ clientSecret: process.env.GOOGLE_CLIENT_SECRET,
108
+ },
109
+ },
110
+ ]
111
+ : []),
112
+ ],`
113
+
114
+ const paymentModule = usePaymentProvider
115
+ ? `
116
+ ...(process.env.CASHFREE_CLIENT_ID
117
+ ? {
118
+ [Modules.PAYMENT]: {
119
+ resolve: "@medusajs/medusa/payment",
120
+ options: {
121
+ providers: [
122
+ {
123
+ resolve: "medusa-payment-provider/providers/cashfree",
124
+ id: "cashfree",
125
+ options: {
126
+ client_id: process.env.CASHFREE_CLIENT_ID,
127
+ client_secret: process.env.CASHFREE_CLIENT_SECRET,
128
+ environment:
129
+ process.env.CASHFREE_ENVIRONMENT === "production" ? "production" : "sandbox",
130
+ return_url: \`\${storefrontUrl}/\${defaultRegion}/checkout/payment/callback\`,
131
+ },
132
+ },
133
+ ],
134
+ },
135
+ },
136
+ }
137
+ : {}),`
138
+ : ""
139
+
140
+ const notificationModule =
141
+ preset === "full"
142
+ ? `
143
+ ...(process.env.SMTP_HOST
144
+ ? {
145
+ [Modules.NOTIFICATION]: {
146
+ resolve: "@medusajs/medusa/notification",
147
+ options: {
148
+ providers: [
149
+ {
150
+ resolve: "@tsc_tech/medusa-plugin-smtp/providers/smtp",
151
+ id: "smtp",
152
+ options: {
153
+ host: process.env.SMTP_HOST,
154
+ port: Number(process.env.SMTP_PORT) || 465,
155
+ secure: process.env.SMTP_SECURE === "true",
156
+ auth: {
157
+ user: process.env.SMTP_USER,
158
+ pass: process.env.SMTP_PASS,
159
+ },
160
+ from: process.env.SMTP_FROM || process.env.SMTP_USER,
161
+ },
162
+ },
163
+ ],
164
+ },
165
+ },
166
+ }
167
+ : {}),`
168
+ : ""
169
+
170
+ const fileModule =
171
+ preset === "full"
172
+ ? ` file: {
173
+ resolve: "@medusajs/medusa/file",
174
+ options: {
175
+ providers: [
176
+ ...(process.env.AWS_BUCKET
177
+ ? [
178
+ {
179
+ resolve: "@medusajs/medusa/file-s3",
180
+ id: "s3",
181
+ options: {
182
+ file_url: process.env.AWS_FILE_URL,
183
+ access_key_id: process.env.AWS_ACCESS_KEY,
184
+ secret_access_key: process.env.AWS_SECRET_ACCESS_KEY,
185
+ region: process.env.AWS_REGION,
186
+ bucket: process.env.AWS_BUCKET,
187
+ prefix: "uploads",
188
+ },
189
+ },
190
+ ]
191
+ : [
192
+ {
193
+ resolve: "@medusajs/medusa/file-local",
194
+ id: "local",
195
+ options: {
196
+ upload_dir: "static",
197
+ backend_url: \`\${process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"}/static\`,
198
+ },
199
+ },
200
+ ]),
201
+ ],
202
+ },
203
+ },`
204
+ : ` file: {
205
+ resolve: "@medusajs/medusa/file",
206
+ options: {
207
+ providers: [
208
+ {
209
+ resolve: "@medusajs/medusa/file-local",
210
+ id: "local",
211
+ options: {
212
+ upload_dir: "static",
213
+ backend_url: \`\${process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"}/static\`,
214
+ },
215
+ },
216
+ ],
217
+ },
218
+ },`
219
+
220
+ const needsPathImport = hasPlugin(enabledPlugins, "medusa-analytics")
221
+
222
+ return `${needsPathImport ? 'import path from "path"\n' : ""}import { loadEnv, defineConfig, Modules, ContainerRegistrationKeys } from "@medusajs/framework/utils"
223
+ import homepageConfig from "./config/homepage-config.json"
224
+ import { PRODUCT_METADATA_DESCRIPTORS } from "./src/config/product-metadata-descriptors"
225
+
226
+ loadEnv(process.env.NODE_ENV || "development", process.cwd())
227
+
228
+ const storefrontUrl = process.env.STOREFRONT_URL || "http://localhost:8000"
229
+ const defaultRegion = process.env.SEED_DEFAULT_REGION || "in"
230
+
231
+ module.exports = defineConfig({
232
+ projectConfig: {
233
+ databaseUrl: process.env.DATABASE_URL,
234
+ http: {
235
+ storeCors: process.env.STORE_CORS!,
236
+ adminCors: process.env.ADMIN_CORS!,
237
+ authCors: process.env.AUTH_CORS!,
238
+ jwtSecret: process.env.JWT_SECRET || "supersecret",
239
+ cookieSecret: process.env.COOKIE_SECRET || "supersecret",
240
+ },
241
+ cookieOptions: {
242
+ sameSite: "lax",
243
+ secure: process.env.NODE_ENV === "production",
244
+ },
245
+ },
246
+ plugins: [
247
+ ${buildPluginsBlock(enabledPlugins)}
248
+ ],
249
+ modules: {
250
+ [Modules.AUTH]: {
251
+ resolve: "@medusajs/medusa/auth",
252
+ dependencies: [Modules.CACHE, ContainerRegistrationKeys.LOGGER],
253
+ options: {
254
+ ${authProviders}
255
+ },
256
+ },
257
+ [Modules.FULFILLMENT]: {
258
+ resolve: "@medusajs/medusa/fulfillment",
259
+ options: {
260
+ ${fulfillmentProviders}
261
+ },
262
+ },${paymentModule}${notificationModule}
263
+ ${fileModule}
264
+ },
265
+ })
266
+ `
267
+ }
268
+
269
+ module.exports = { generateMedusaConfig, buildPluginsBlock }