|
| 1 | +const { program } = require('commander'); |
| 2 | +const open = require('open'); |
| 3 | +const readline = require('readline'); |
| 4 | +const net = require('net'); |
| 5 | +const Socket = net.Socket; |
| 6 | +const serverModule = require('./modules/server'); |
| 7 | +const settingsModule = require('./modules/settings'); |
| 8 | +const logsModule = require('./modules/logs'); |
| 9 | + |
| 10 | + |
| 11 | +process.on('SIGINT', async function() { |
| 12 | + serverModule.stopAll(); |
| 13 | +}); |
| 14 | + |
| 15 | +const rl = readline.createInterface({ |
| 16 | + input: process.stdin, |
| 17 | + output: process.stdout |
| 18 | +}); |
| 19 | + |
| 20 | +async function isPortTaken(port) { |
| 21 | + return new Promise((resolve) => { |
| 22 | + const socket = new Socket(); |
| 23 | + socket.on("timeout", () => { |
| 24 | + resolve(false); |
| 25 | + socket.destroy(); |
| 26 | + }); |
| 27 | + socket.on("connect", () => { |
| 28 | + resolve(true); |
| 29 | + }); |
| 30 | + socket.on("error", error => { |
| 31 | + if (error.code !== "ECONNREFUSED") resolve(true); |
| 32 | + else resolve(false); |
| 33 | + }); |
| 34 | + socket.connect(port, "0.0.0.0"); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +async function findAvailablePort(startPort) { |
| 39 | + let port = startPort; |
| 40 | + let maxPort = startPort + 100; |
| 41 | + while (await isPortTaken(port)) { |
| 42 | + if (port >= maxPort) { |
| 43 | + throw new Error('No available ports found'); |
| 44 | + } |
| 45 | + port++; |
| 46 | + } |
| 47 | + return port; |
| 48 | +} |
| 49 | + |
| 50 | +const originalLog = console.log; |
| 51 | + |
| 52 | +console.log = function (...args) { |
| 53 | + originalLog.apply(console, args); |
| 54 | + rl.prompt(); |
| 55 | +}; |
| 56 | + |
| 57 | +program |
| 58 | + .option('-p, --port <port>', 'Set server port', parseInt) |
| 59 | + .option('-d, --dir <dir>', 'Set server dir', String) |
| 60 | + .option('-r, --run', 'Run the server', false) |
| 61 | + .parse(process.argv); |
| 62 | +(async () => { |
| 63 | + await settingsModule.init(); |
| 64 | + await settingsModule.read(); |
| 65 | + const port = await findAvailablePort(3000); |
| 66 | + const subPort = program.opts().port || 8080; |
| 67 | + const autoServer = program.opts().run || false; |
| 68 | + const pathServer = program.opts().dir || settingsModule.getObjects().path; |
| 69 | + await serverModule.startMain(port, pathServer, autoServer, subPort); |
| 70 | + |
| 71 | + |
| 72 | + if (settingsModule.getObjects().app.auto_web) { |
| 73 | + await open(`http://localhost:${port}`); |
| 74 | + } |
| 75 | + |
| 76 | + let exit = false; |
| 77 | + readline.emitKeypressEvents(process.stdin); |
| 78 | + process.stdin.on('keypress', (ch, key) => { |
| 79 | + if (key && key.name === 'escape') { |
| 80 | + console.log('Close app? (y)'); |
| 81 | + exit = true |
| 82 | + rl.prompt(); |
| 83 | + } else if (key && key.name === 'y' && exit) { |
| 84 | + console.log('\nExiting application...'); |
| 85 | + rl.close(); |
| 86 | + } else { |
| 87 | + exit = false |
| 88 | + } |
| 89 | + }); |
| 90 | + process.stdin.setRawMode(true); |
| 91 | + |
| 92 | + rl.on('line', async(input) => { |
| 93 | + if (input.trim() === 'open') { |
| 94 | + await open(`http://localhost:${port}`); |
| 95 | + } else if (input.trim() === 'exit') { |
| 96 | + console.log('Exiting application...'); |
| 97 | + } else if (input.trim() === 'stop') { |
| 98 | + await serverModule.stop(); |
| 99 | + } else if (input.trim() === 'run') { |
| 100 | + await serverModule.run(); |
| 101 | + } else if (input.trim() === 'help' || input.trim() === '-h') { |
| 102 | + const help = |
| 103 | + 'exit - Close application\n' + |
| 104 | + 'run - Start server\n' + |
| 105 | + 'stop - Stop server'; |
| 106 | + console.log(help); |
| 107 | + } else { |
| 108 | + console.log('Unknown command. To get available commands -h'); |
| 109 | + } |
| 110 | + rl.prompt(); |
| 111 | + }); |
| 112 | + rl.prompt(); |
| 113 | + |
| 114 | + rl.on('close', async() => { |
| 115 | + console.log('Application closed.'); |
| 116 | + await logsModule.write('Application closed.') |
| 117 | + process.exit(0); |
| 118 | + }); |
| 119 | +})(); |
0 commit comments