Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ mercur-cli dev
- Node.js v20+
- PostgreSQL
- Git CLI
- Nodemon

# Resources

Expand Down
62 changes: 62 additions & 0 deletions apps/backend/dev-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { spawn } = require('child_process')
const path = require('path')

const ROOT = path.resolve(__dirname, '../..')
const MODULES_DIR = path.join(ROOT, 'packages/modules')
const modules = process.argv.slice(2)

const children = []
const run = (cmd, args, opts = {}) => {
const p = spawn(cmd, args, { stdio: 'pipe', shell: true, ...opts })
children.push(p)
return p
}
const runInherit = (cmd, args, opts = {}) => {
const p = spawn(cmd, args, { stdio: 'inherit', shell: true, ...opts })
children.push(p)
return p
}

const backendCmd =
'CHOKIDAR_USEPOLLING=1 CHOKIDAR_INTERVAL=300 nodemon --verbose --legacy-watch --signal SIGTERM -e js,json,ts -w src -w ../../packages/modules --exec "medusa develop --types=false"'

const backend = run(backendCmd, [], {
cwd: path.resolve(__dirname),
env: { ...process.env }
})

const READY_RE = /(server|medusa).*(ready|listening|running|started)/i
let started = false
const startModules = () => {
if (started) return
started = true
modules.forEach((name) => {
const cwd = path.join(MODULES_DIR, name)
console.log(`\n[watch] ${name} → medusa plugin:develop`)
runInherit('npx', ['medusa', 'plugin:develop'], { cwd, env: process.env })
})
}

backend.stdout.on('data', (b) => {
const line = b.toString()
process.stdout.write(line)
if (READY_RE.test(line)) startModules()
})
backend.stderr.on('data', (b) => {
const line = b.toString()
process.stderr.write(line)
if (READY_RE.test(line)) startModules()
})

setTimeout(startModules, 8000).unref()

backend.on('exit', (code) => {
children.forEach((c) => c !== backend && !c.killed && c.kill('SIGTERM'))
process.exit(code ?? 0)
})
;['SIGINT', 'SIGTERM'].forEach((sig) =>
process.on(sig, () => {
children.forEach((c) => !c.killed && c.kill('SIGTERM'))
process.exit(0)
})
)
4 changes: 2 additions & 2 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"build": "medusa build && ln -s .medusa/server/public/ public",
"seed": "medusa exec ./src/scripts/seed.ts",
"start": "medusa start --types=false",
"dev": "medusa develop --types=false",
"dev": "node ./dev-runner.js",
"db:migrate": "medusa db:migrate",
"test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
"test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
Expand Down Expand Up @@ -79,4 +79,4 @@
"engines": {
"node": ">=20"
}
}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"format": "turbo run format",
"lint": "turbo run lint",
"codegen": "cd .",
"generate:oas": "turbo run generate:oas"
"generate:oas": "turbo run generate:oas",
"clean:dirs": "rimraf --glob \"node_modules\" \"**/node_modules\" \"**/.turbo\" \"**/.medusa\" \"**/dist\"",
"reinstall": "yarn",
"clean-build": "yarn clean:dirs && yarn cache clean --force && yarn reinstall && yarn build"
}
}
}