Repository navigation
Expand file tree
/
Copy pathdev-runner.js
More file actions
72 lines (58 loc) · 1.89 KB
/
Copy pathdev-runner.js
File metadata and controls
72 lines (58 loc) · 1.89 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable @typescript-eslint/no-require-imports */
const { spawn } = require('child_process');
let nextProcess = null;
let isRestarting = false;
function startNextDev() {
console.log('[AutoClipper] Starting Next.js Dev Server...');
// Force clean the cache to prevent lingering errors
try {
const fs = require('fs');
if (fs.existsSync('.next')) {
fs.rmSync('.next', { recursive: true, force: true });
}
} catch {}
nextProcess = spawn('npx', ['next', 'dev'], {
shell: true,
stdio: ['inherit', 'pipe', 'pipe']
});
nextProcess.stdout.on('data', (data) => {
const output = data.toString();
process.stdout.write(output);
// Check for the specific error
if (output.includes('Uncaught SyntaxError: Invalid or unexpected token') || output.includes('SyntaxError')) {
handleCrash('SyntaxError detected in Next.js build!');
}
});
nextProcess.stderr.on('data', (data) => {
const output = data.toString();
process.stderr.write(output);
if (output.includes('Uncaught SyntaxError: Invalid or unexpected token') || output.includes('SyntaxError')) {
handleCrash('SyntaxError detected in Next.js build!');
}
});
nextProcess.on('close', (code) => {
if (!isRestarting) {
console.log(`[AutoClipper] Next.js process exited with code ${code}`);
process.exit(code);
}
});
}
function handleCrash(reason) {
if (isRestarting) return;
isRestarting = true;
console.log(`\n\n[AutoClipper] 🚨 ${reason}`);
console.log('[AutoClipper] 🔄 Auto-restarting Next.js Server gracefully in 2 seconds...\n\n');
if (nextProcess) {
nextProcess.kill('SIGTERM');
}
setTimeout(() => {
isRestarting = false;
startNextDev();
}, 2000);
}
// Intercept termination to cleanly exit children
process.on('SIGINT', () => {
if (nextProcess) nextProcess.kill('SIGTERM');
process.exit();
});
startNextDev();