|
1 | 1 | import { spawn } from 'child_process'; |
2 | 2 | import path from 'path'; |
| 3 | +import fs from 'fs'; |
3 | 4 | import { fileURLToPath } from 'url'; |
4 | 5 | import { createRequire } from 'module'; |
5 | 6 |
|
@@ -37,14 +38,45 @@ export function registerDevCommand(cli) { |
37 | 38 | const nextCmd = 'npm'; |
38 | 39 | const args = ['run', 'dev', '--', '-p', options.port]; |
39 | 40 |
|
40 | | - const child = spawn(nextCmd, args, { |
41 | | - stdio: 'inherit', |
42 | | - env, |
43 | | - cwd: nextAppDir // CRITICAL: Run in the Next.js app directory |
44 | | - }); |
| 41 | + let child; |
| 42 | + let isRestarting = false; |
| 43 | + let debounceTimer; |
45 | 44 |
|
46 | | - child.on('close', (code) => { |
47 | | - process.exit(code); |
48 | | - }); |
| 45 | + const startServer = () => { |
| 46 | + child = spawn(nextCmd, args, { |
| 47 | + stdio: 'inherit', |
| 48 | + env, |
| 49 | + cwd: nextAppDir // CRITICAL: Run in the Next.js app directory |
| 50 | + }); |
| 51 | + |
| 52 | + child.on('close', (code) => { |
| 53 | + if (isRestarting) { |
| 54 | + isRestarting = false; |
| 55 | + startServer(); |
| 56 | + } else { |
| 57 | + // Only exit if we are not restarting. |
| 58 | + // Null code means killed by signal (like our kill() call), but we handle that via flag. |
| 59 | + process.exit(code || 0); |
| 60 | + } |
| 61 | + }); |
| 62 | + }; |
| 63 | + |
| 64 | + startServer(); |
| 65 | + |
| 66 | + // Watch for config changes |
| 67 | + const configFile = path.resolve(process.cwd(), 'objectdocs.json'); |
| 68 | + if (fs.existsSync(configFile)) { |
| 69 | + console.log(`Watching config: ${configFile}`); |
| 70 | + fs.watch(configFile, (eventType) => { |
| 71 | + if (eventType === 'change') { |
| 72 | + clearTimeout(debounceTimer); |
| 73 | + debounceTimer = setTimeout(() => { |
| 74 | + console.log('\nConfig changed. Restarting server...'); |
| 75 | + isRestarting = true; |
| 76 | + child.kill(); |
| 77 | + }, 500); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
49 | 81 | }); |
50 | 82 | } |
0 commit comments