@hszrdm/create-commerce 1.9.0 → 1.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hszrdm/create-commerce",
3
- "version": "1.9.0",
3
+ "version": "1.9.1",
4
4
  "description": "Create a complete HSZRDM Commerce webshop project.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
package/src/cli.mjs CHANGED
@@ -234,17 +234,60 @@ function configureProject(root, settings) {
234
234
  "core:init": "npm --workspace backend run core:init --",
235
235
  "storefront:key": "npm --workspace backend run storefront:key --",
236
236
  "bootstrap:assert": "npm --workspace backend run bootstrap:assert",
237
+ "install:dev": "node ./scripts/commerce-ops.mjs install dev",
238
+ "install:prod": "node ./scripts/commerce-ops.mjs install prod",
239
+ "deploy:dev": "node ./scripts/commerce-ops.mjs deploy dev",
240
+ "deploy:prod": "node ./scripts/commerce-ops.mjs deploy prod",
241
+ "backup:dev": "node ./scripts/commerce-ops.mjs backup dev",
242
+ "backup:prod": "node ./scripts/commerce-ops.mjs backup prod",
243
+ "restore:dev": "node ./scripts/commerce-ops.mjs restore dev",
244
+ "restore:prod": "node ./scripts/commerce-ops.mjs restore prod",
237
245
  "docker:up": "docker compose --env-file .env.docker up -d --wait",
238
246
  "docker:down": "docker compose --env-file .env.docker down"
239
247
  },
240
248
  }
241
249
  writeJson(join(root, "package.json"), rootPackage)
250
+ mkdirSync(join(root, "scripts"), { recursive: true })
251
+ cpSync(join(root, "backend", "scripts", "commerce-ops.mjs"),
252
+ join(root, "scripts", "commerce-ops.mjs"))
253
+ writeFileSync(join(root, "ecosystem.config.cjs"), `module.exports = {
254
+ apps: [
255
+ {
256
+ name: ${JSON.stringify(settings.slug + "-backend")},
257
+ cwd: __dirname,
258
+ script: "npm",
259
+ args: "--workspace backend run start:prod",
260
+ env_production: {
261
+ NODE_ENV: "production",
262
+ APP_ENV: "production",
263
+ PORT: ${JSON.stringify(new URL(settings.backendUrl).port || "9000")},
264
+ HOST: "127.0.0.1",
265
+ },
266
+ },
267
+ {
268
+ name: ${JSON.stringify(settings.slug + "-storefront")},
269
+ cwd: __dirname,
270
+ script: "npm",
271
+ args: "--workspace storefront run start",
272
+ env_production: {
273
+ NODE_ENV: "production",
274
+ APP_ENV: "production",
275
+ PORT: ${JSON.stringify(new URL(settings.storefrontUrl).port || "3000")},
276
+ HOSTNAME: "127.0.0.1",
277
+ },
278
+ },
279
+ ],
280
+ }
281
+ `)
282
+
242
283
  writeFileSync(join(root, ".nvmrc"), "22\n")
243
284
  writeFileSync(
244
285
  join(root, ".gitignore"),
245
286
  [
246
287
  "node_modules/",
247
288
  "backend/.env",
289
+ "backend/.env.production",
290
+ "storefront/.env.production",
248
291
  "backend/.medusa/",
249
292
  "backend/src/api",
250
293
  "backend/src/jobs",
@@ -278,7 +321,18 @@ function configureProject(root, settings) {
278
321
 
279
322
  writeFileSync(
280
323
  join(root, "README.md"),
281
- `# ${settings.storeName}\n\nGenerated by @hszrdm/create-commerce.\n\n## Development\n\n\`npm run docker:up\`\n\`npm run dev:backend\`\n\`npm run dev:storefront\`\n\nProject-specific backend code belongs in \`backend/src/project/\`. Shared commerce behavior comes from \`@hszrdm/core\`.\n`,
324
+ `# ${settings.storeName}\n\nGenerated by @hszrdm/create-commerce.\n\n## Development\n\n\`npm run docker:up\`\n\`npm run dev:backend\`\n\`npm run dev:storefront\`\n\n## Environment operations
325
+
326
+ Development uses \`backend/.env\` and a separate development PostgreSQL database:
327
+ \`npm run install:dev\`, \`npm run deploy:dev\`, \`npm run backup:dev\`.
328
+ Production requires an independently configured \`backend/.env.production\` and PM2:
329
+ \`npm run install:prod\`, \`npm run deploy:prod\`, \`npm run backup:prod\`.
330
+ Restore is dry-run by default: \`npm run restore:dev -- --from backups/dev/<bundle>\`
331
+ or \`npm run restore:prod -- --from backups/prod/<bundle>\`.
332
+ Only add \`--yes\` after checking the target database; a restore can delete existing data.
333
+ Do not point development and production at the same PostgreSQL database.
334
+
335
+ Project-specific backend code belongs in \`backend/src/project/\`. Shared commerce behavior comes from \`@hszrdm/core\`.\n`,
282
336
  )
283
337
  }
284
338
 
@@ -22,7 +22,7 @@
22
22
  "start:prod": "cd .medusa/server && medusa start"
23
23
  },
24
24
  "dependencies": {
25
- "@hszrdm/core": "1.9.0",
25
+ "@hszrdm/core": "1.9.1",
26
26
  "@medusajs/admin-sdk": "2.22.2",
27
27
  "@medusajs/cli": "2.22.2",
28
28
  "@medusajs/draft-order": "2.22.2",
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env node
2
+ // Shared development/production operations for generated HSZRDM shops.
3
+ // Environment selection is explicit: never reuse a production database in dev.
4
+ import { existsSync, readFileSync } from "node:fs"
5
+ import { resolve, join } from "node:path"
6
+ import { spawnSync, spawn } from "node:child_process"
7
+
8
+ const [action, target, ...rest] = process.argv.slice(2)
9
+ if (!["install", "deploy", "backup", "restore"].includes(action) || !["dev", "prod"].includes(target)) {
10
+ console.error("Usage: node scripts/commerce-ops.mjs <install|deploy|backup|restore> <dev|prod> [--from ...] [--yes]")
11
+ process.exit(2)
12
+ }
13
+ const root = process.cwd()
14
+ const backendDir = existsSync(join(root, "backend")) ? join(root, "backend") : join(root, "apps/backend")
15
+ const envPath = join(backendDir, target === "prod" ? ".env.production" : ".env")
16
+ if (!existsSync(envPath)) throw new Error(`Missing ${envPath}. Configure it before ${target} operations.`)
17
+ const envFile = parseEnv(readFileSync(envPath, "utf8"))
18
+ const expectedApp = target === "prod" ? "production" : "development"
19
+ const expectedNode = target === "prod" ? "production" : "development"
20
+ if (envFile.APP_ENV && envFile.APP_ENV !== expectedApp) {
21
+ throw new Error(`Environment mismatch: expected APP_ENV=${expectedApp} in ${envPath}`)
22
+ }
23
+ if (envFile.NODE_ENV && envFile.NODE_ENV !== expectedNode) {
24
+ throw new Error(`Environment mismatch: expected NODE_ENV=${expectedNode} in ${envPath}`)
25
+ }
26
+ if (!envFile.DATABASE_URL) throw new Error(`DATABASE_URL is missing from ${envPath}`)
27
+ const dbName = databaseIdentity(envFile.DATABASE_URL)
28
+ const otherPath = join(backendDir, target === "prod" ? ".env" : ".env.production")
29
+ if (existsSync(otherPath)) {
30
+ const other = parseEnv(readFileSync(otherPath, "utf8"))
31
+ if (other.DATABASE_URL && databaseIdentity(other.DATABASE_URL) === dbName) {
32
+ throw new Error("Dev and prod may not share the same database connection.")
33
+ }
34
+ }
35
+ const env = { ...process.env, ...envFile, APP_ENV: expectedApp, NODE_ENV: expectedNode }
36
+ console.log(`HSZRDM ${action}:${target} | DB: ${new URL(envFile.DATABASE_URL).pathname.slice(1)}`)
37
+
38
+ function run(command, args) {
39
+ const result = spawnSync(command, args, { cwd: root, env, stdio: "inherit" })
40
+ if (result.error) throw result.error
41
+ if (result.status !== 0) process.exit(result.status || 1)
42
+ }
43
+ function npm(args) { run(process.platform === "win32" ? "npm.cmd" : "npm", args) }
44
+ function backupScript() {
45
+ const candidates = [join(root, "scripts/core-backup.mjs"), join(backendDir, "scripts/core-backup.mjs")]
46
+ const path = candidates.find(existsSync)
47
+ if (!path) throw new Error("Core backup script not installed.")
48
+ return path
49
+ }
50
+ function restoreScript() {
51
+ const candidates = [join(root, "scripts/core-restore.mjs"), join(backendDir, "scripts/core-restore.mjs")]
52
+ const path = candidates.find(existsSync)
53
+ if (!path) throw new Error("Core restore script not installed.")
54
+ return path
55
+ }
56
+ function backup() {
57
+ run(process.execPath, [backupScript(), "--project-dir", root,
58
+ "--database-url", envFile.DATABASE_URL, "--output", join(root, "backups", target)])
59
+ }
60
+ function parseEnv(src) {
61
+ const result = {}
62
+ for (const line of src.split(/\r?\n/)) {
63
+ const m = line.match(/^\s*([A-Z][A-Z0-9_]*)\s*=\s*(.*?)\s*$/)
64
+ if (!m) continue
65
+ const v = m[2]
66
+ result[m[1]] = (v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'")) ? v.slice(1, -1) : v
67
+ }
68
+ return result
69
+ }
70
+ function databaseIdentity(value) {
71
+ const u = new URL(value)
72
+ if (!["postgres:", "postgresql:"].includes(u.protocol)) throw new Error("DATABASE_URL must be PostgreSQL.")
73
+ return `${u.hostname.toLowerCase()}:${u.port || "5432"}/${decodeURIComponent(u.pathname)}`
74
+ }
75
+ function devServer() {
76
+ const children = []
77
+ const cmd = process.platform === "win32" ? "npm.cmd" : "npm"
78
+ for (const name of ["dev:backend", "dev:storefront"]) {
79
+ children.push(spawn(cmd, ["run", name], { cwd: root, env, stdio: "inherit" }))
80
+ }
81
+ function stop() { for (const child of children) child.kill("SIGTERM") }
82
+ process.on("SIGINT", stop)
83
+ process.on("SIGTERM", stop)
84
+ for (const child of children) child.on("exit", (code) => {
85
+ if (code && !process.exitCode) process.exitCode = code
86
+ stop()
87
+ })
88
+ }
89
+ if (action === "backup") backup()
90
+ if (action === "restore") {
91
+ if (!rest.includes("--from")) throw new Error("Restore requires --from <backup-directory>.")
92
+ const fromIndex = rest.indexOf("--from")
93
+ const from = rest[fromIndex + 1]
94
+ if (!from || from.startsWith("--")) throw new Error("Restore --from path missing.")
95
+ // Explicit --yes is still required by core-restore for destructive restore.
96
+ run(process.execPath, [restoreScript(), "--project-dir", root,
97
+ "--database-url", envFile.DATABASE_URL, "--from", resolve(from),
98
+ ...(rest.includes("--yes") ? ["--yes"] : []),
99
+ ...(rest.includes("--overwrite-files") ? ["--overwrite-files"] : [])])
100
+ }
101
+ if (action === "install") npm(["ci"])
102
+ if (action === "deploy") {
103
+ npm(["ci"])
104
+ backup()
105
+ npm(["run", "db:migrate"])
106
+ if (target === "dev") {
107
+ devServer()
108
+ } else {
109
+ npm(["run", "build"])
110
+ if (!existsSync(join(root, "ecosystem.config.cjs"))) {
111
+ throw new Error("Missing ecosystem.config.cjs: cannot safely reload production PM2.")
112
+ }
113
+ run("pm2", ["startOrReload", "ecosystem.config.cjs", "--env", "production", "--update-env"])
114
+ run("pm2", ["save"])
115
+ }
116
+ }