From: Dan Allen Date: Wed, 26 Jun 2019 00:18:35 +0000 (-0600) Subject: configure preview server to allow access from any host X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=f49b50e0e4e5a6d07983b1f94232df361d4c0fb8;p=eg-antora.git configure preview server to allow access from any host - set host of preview server to 0.0.0.0 - move server config to top-level constant - patch gulp-connect log to report URL with localhost and URL with local IP --- diff --git a/gulp.d/tasks/serve.js b/gulp.d/tasks/serve.js index 82ee933..f9b987a 100644 --- a/gulp.d/tasks/serve.js +++ b/gulp.d/tasks/serve.js @@ -1,10 +1,36 @@ 'use strict' const connect = require('gulp-connect') +const os = require('os') + +const ANY_HOST = '0.0.0.0' +const URL_RX = /(https?):\/\/(?:[^/: ]+)(:\d+)?/ module.exports = (root, opts = {}, watch = undefined) => (done) => { - connect.server({ ...opts, root }, function () { + connect.server({ ...opts, middleware: opts.host === ANY_HOST ? decorateLog : undefined, root }, function () { this.server.on('close', done) if (watch) watch() }) } + +function decorateLog (_, app) { + const _log = app.log + app.log = (msg) => { + if (msg.startsWith('Server started ')) { + const localIp = getLocalIp() + const replacement = '$1://localhost$2' + (localIp ? ` and $1://${localIp}$2` : '') + msg = msg.replace(URL_RX, replacement) + } + _log(msg) + } + return [] +} + +function getLocalIp () { + for (let records of Object.values(os.networkInterfaces())) { + for (let record of records) { + if (!record.internal && record.family === 'IPv4') return record.address + } + } + return 'localhost' +} diff --git a/gulpfile.js b/gulpfile.js index 7d57e55..a60bd19 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -11,6 +11,7 @@ const previewDestDir = 'public' const srcDir = 'src' const destDir = `${previewDestDir}/_` const { reload: livereload } = process.env.LIVERELOAD === 'true' ? require('gulp-connect') : {} +const serverConfig = { host: '0.0.0.0', port: 5252, livereload } const task = require('./gulp.d/tasks') const glob = { @@ -91,7 +92,7 @@ const previewBuildTask = createTask({ const previewServeTask = createTask({ name: 'preview:serve', - call: task.serve(previewDestDir, { port: 5252, livereload }, () => watch(glob.all, previewBuildTask)), + call: task.serve(previewDestDir, serverConfig, () => watch(glob.all, previewBuildTask)), }) const previewTask = createTask({