Commit f49b50e0 authored by Dan Allen's avatar Dan Allen

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
parent 40309d5e
'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'
}
......@@ -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({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment