Need gulp 4.0.2 task to wait for express 4.17.1 server to start, then open a browser.
Here's what I have that works (but I think it's pretty old school)...
// Start server in development or test mode
gulp.task('start:server', () => {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
config = require(`./${serverPath}/config/environment`).default;
nodemon(`--inspect --trace-deprecation --trace-warnings -w ${serverPath} ${serverPath}`)
.on('log', onServerLog);
});
// Run nodemon with debugging (server/config/express.js runs webpack.make.js)
gulp.task('serve',
gulp.series(
gulp.parallel('eslint', 'eslint:tests', 'client:inject:scss', 'dist:client:assets:fonts', 'env:common'),
gulp.parallel('start:server', 'start:client'),
'watch'
)
);
// Perform HTTP GET to check for app readiness
function checkAppReady(cb) {
let options = {
host: 'localhost',
port: config.port
};
http.get(options, () => cb(true))
.on('error', () => cb(false));
}
// Check every 250ms until app server is ready
function whenServerReady(cb) {
let serverReady = false;
let appReadyInterval = setInterval(() =>
checkAppReady(ready => {
if(!ready || serverReady) {
return;
}
clearInterval(appReadyInterval);
serverReady = true;
cb();
}),
250);
}
// Wait until server is responding then open browser on client to our starting page
gulp.task('start:client', done => {
whenServerReady(() => {
opn(`http://localhost:${config.browserSyncPort}`/*, {app: 'google chrome'}*/);
done();
});
});
While this works well, I'm wondering if there's a more streamlined way to accomplish the same thing. The last StackOverflow post on the subject is more than 3 years old.