-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstart-dev-server.js
More file actions
117 lines (100 loc) · 3.67 KB
/
start-dev-server.js
File metadata and controls
117 lines (100 loc) · 3.67 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { spawn } = require('child_process');
const http = require('http');
console.log('🚀 Starting OpenReadme Development Server');
console.log('=========================================');
// Function to check if server is running
function checkServer(port = 3000) {
return new Promise((resolve) => {
const req = http.request(`http://localhost:${port}`, (res) => {
resolve(true);
});
req.on('error', () => resolve(false));
req.end();
});
}
// Function to wait for server to start
async function waitForServer(port = 3000, maxAttempts = 30) {
console.log(`⏳ Waiting for server to start on port ${port}...`);
for (let i = 0; i < maxAttempts; i++) {
const isRunning = await checkServer(port);
if (isRunning) {
console.log(`✅ Server is running on http://localhost:${port}`);
return true;
}
await new Promise(resolve => setTimeout(resolve, 1000));
process.stdout.write('.');
}
console.log(`\n❌ Server did not start within ${maxAttempts} seconds`);
return false;
}
async function startServer() {
// Check if server is already running
const alreadyRunning = await checkServer();
if (alreadyRunning) {
console.log('✅ Development server is already running on http://localhost:3000');
return true;
}
console.log('📦 Starting Next.js development server...');
// Start the development server
const serverProcess = spawn('npm', ['run', 'dev'], {
stdio: 'pipe',
shell: true
});
serverProcess.stdout.on('data', (data) => {
const output = data.toString();
console.log(output.trim());
// Check for common startup messages
if (output.includes('Ready') || output.includes('started server')) {
console.log('🎉 Server startup detected!');
}
});
serverProcess.stderr.on('data', (data) => {
const error = data.toString();
if (!error.includes('warn') && !error.includes('Warning')) {
console.error('❌ Server error:', error.trim());
}
});
serverProcess.on('close', (code) => {
console.log(`\n📊 Server process exited with code ${code}`);
});
// Wait for server to be ready
const serverReady = await waitForServer();
if (serverReady) {
console.log('\n🎯 Development server is ready for testing!');
console.log('📋 You can now run:');
console.log(' - node test-api-endpoints.js (to test API endpoints)');
console.log(' - Open http://localhost:3000 in your browser');
console.log('\n⚠️ Press Ctrl+C to stop the server');
// Keep the process alive
process.on('SIGINT', () => {
console.log('\n🛑 Stopping development server...');
serverProcess.kill();
process.exit(0);
});
return true;
} else {
console.log('\n❌ Failed to start development server');
serverProcess.kill();
return false;
}
}
// Handle command line arguments
const args = process.argv.slice(2);
if (args.includes('--check-only')) {
// Just check if server is running
checkServer().then(running => {
if (running) {
console.log('✅ Development server is running on http://localhost:3000');
process.exit(0);
} else {
console.log('❌ Development server is not running');
process.exit(1);
}
});
} else {
// Start the server
startServer().catch(error => {
console.error('💥 Failed to start server:', error);
process.exit(1);
});
}