tix-react-ssr
Version:
Tiket.com React Project Scripts
63 lines (50 loc) • 1.65 kB
JavaScript
const path = require('path');
const cp = require('child_process');
const webpackConfig = require('./webpack.config');
// Should match the text string used in `src/server.js/server.listen(...)`
const RUNNING_REGEXP = /The server is running at http:\/\/(.*?)\//;
let server;
let pending = true;
const [, serverConfig] = webpackConfig;
const serverPath = path.join(serverConfig.output.path, serverConfig.name + '.js');
// Launch or restart the Node.js server
function runServer() {
return new Promise(resolve => {
function onStdOut(data) {
const time = new Date().toTimeString();
const match = data.toString('utf8').match(RUNNING_REGEXP);
process.stdout.write(time.replace(/.*(\d{2}:\d{2}:\d{2}).*/, '[$1] '));
process.stdout.write(data);
if (match) {
server.host = match[1];
server.stdout.removeListener('data', onStdOut);
server.stdout.on('data', x => process.stdout.write(x));
pending = false;
resolve(server)
}
}
if (server) {
server.kill('SIGTERM')
}
server = cp.spawn('node', [serverPath], {
env: Object.assign({ NODE_ENV: 'development' }, process.env),
silent: false,
});
if (pending) {
server.once('exit', (code, signal) => {
if (pending) {
throw new Error(`Server terminated unexpectedly with code: ${code} signal: ${signal}`)
}
})
}
server.stdout.on('data', onStdOut);
server.stderr.on('data', x => process.stderr.write(x));
return server
})
}
process.on('exit', () => {
if (server) {
server.kill('SIGTERM')
}
});
module.exports = runServer;