(options = {})
| 5 | const chalk = require('chalk'); |
| 6 | |
| 7 | async function startServer(options = {}) { |
| 8 | const { |
| 9 | port: requestedPort, |
| 10 | host = 'localhost', |
| 11 | open: shouldOpen = true |
| 12 | } = options; |
| 13 | |
| 14 | // Find available port |
| 15 | const port = await getPort({ |
| 16 | port: requestedPort ? parseInt(requestedPort) : getPort.makeRange(3000, 3100) |
| 17 | }); |
| 18 | |
| 19 | const app = express(); |
| 20 | |
| 21 | |
| 22 | const distPath = path.join(__dirname, '../dist'); |
| 23 | app.use(express.static(distPath)); |
| 24 | |
| 25 | app.get('*', (req, res) => { |
| 26 | res.sendFile(path.join(distPath, 'index.html')); |
| 27 | }); |
| 28 | |
| 29 | app.use((err, req, res, next) => { |
| 30 | console.error(chalk.red('Server error:'), err); |
| 31 | res.status(500).send('Internal Server Error'); |
| 32 | }); |
| 33 | |
| 34 | return new Promise((resolve, reject) => { |
| 35 | const server = app.listen(port, host, () => { |
| 36 | const url = `http://${host}:${port}`; |
| 37 | |
| 38 | console.log(''); |
| 39 | console.log(chalk.green('✅ DataKit is running!')); |
| 40 | console.log(''); |
| 41 | console.log(chalk.cyan(' Local: ') + chalk.white(url)); |
| 42 | if (host === 'localhost') { |
| 43 | console.log(chalk.cyan(' Network: ') + chalk.white(`http://0.0.0.0:${port}`)); |
| 44 | } |
| 45 | console.log(''); |
| 46 | console.log(chalk.gray(' Press Ctrl+C to stop the server')); |
| 47 | console.log(''); |
| 48 | |
| 49 | if (shouldOpen) { |
| 50 | console.log(chalk.blue('🌐 Opening browser...')); |
| 51 | open(url).catch(err => { |
| 52 | console.log(chalk.yellow('⚠️ Could not open browser automatically')); |
| 53 | console.log(chalk.gray(' Please navigate to the URL above manually')); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | // Handle graceful shutdown |
| 58 | process.on('SIGINT', () => { |
| 59 | console.log('\n' + chalk.yellow('🛑 Shutting down DataKit...')); |
| 60 | server.close(() => { |
| 61 | console.log(chalk.green('✅ DataKit stopped successfully')); |
| 62 | process.exit(0); |
| 63 | }); |
| 64 | }); |
no test coverage detected