-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdev.mjs
More file actions
55 lines (50 loc) · 1.91 KB
/
Copy pathdev.mjs
File metadata and controls
55 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Runs the devframe RPC/WebSocket backend and the Next.js dev server (with
// HMR) together. Open the UI URL printed below; the client connects to the
// backend over WebSocket via NEXT_PUBLIC_DEVFRAME_WS.
import { spawn } from 'node:child_process'
import { createRequire } from 'node:module'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
const require = createRequire(import.meta.url)
const root = fileURLToPath(new URL('..', import.meta.url))
const host = process.env.HOST ?? '0.0.0.0'
const serverPort = process.env.DEVFRAME_GIT_PORT ?? '9710'
const clientPort = process.env.PORT ?? '3000'
// Resolve the Next.js bin explicitly so this works regardless of PATH.
const nextBin = require.resolve('next/dist/bin/next')
const children = [
// RPC + WebSocket backend (devframe). Serves the prebuilt SPA too, but in
// dev you open the Next server below for hot-reloading.
spawn(process.execPath, ['src/cli.ts', '--port', serverPort, '--host', host], {
cwd: root,
stdio: 'inherit',
env: process.env,
}),
// Next.js dev server (HMR). Points the client at the backend WebSocket.
spawn(process.execPath, [nextBin, 'dev', 'src/client', '--port', clientPort, '--hostname', host], {
cwd: root,
stdio: 'inherit',
env: { ...process.env, NEXT_PUBLIC_DEVFRAME_WS: serverPort },
}),
]
console.error(`\n @devframes/plugin-git dev`)
console.error(` UI (HMR): http://localhost:${clientPort}`)
console.error(` RPC backend: http://localhost:${serverPort}\n`)
let shuttingDown = false
function shutdown(code = 0) {
if (shuttingDown)
return
shuttingDown = true
for (const child of children)
child.kill('SIGTERM')
process.exit(code)
}
process.on('SIGINT', () => shutdown(0))
process.on('SIGTERM', () => shutdown(0))
for (const child of children) {
child.on('exit', code => shutdown(code ?? 0))
child.on('error', (error) => {
console.error(error)
shutdown(1)
})
}