Skip to content

Commit

Permalink
Added support for a --local flag (#327)
Browse files Browse the repository at this point in the history
* Add local to cli options

* Update ava snapshots

* Create server on localhost mode

* Stop logging network IP on local mode
  • Loading branch information
fmiras authored and leo committed Feb 25, 2018
1 parent 12f1c67 commit 297748b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
9 changes: 7 additions & 2 deletions bin/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ detect(port).then(open => {
inUse,
flags.clipless !== true,
flags.open,
flags.ssl
flags.ssl,
flags.local
]

server.listen(port, listening.bind(this, ...listenArgs))
if (flags.local) {
server.listen(port, 'localhost', listening.bind(this, ...listenArgs))
} else {
server.listen(port, listening.bind(this, ...listenArgs))
}
})
15 changes: 9 additions & 6 deletions lib/listening.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = coroutine(function*(
inUse,
clipboard,
open,
ssl
ssl,
isLocal
) {
const details = server.address()
const isTTY = process.stdout.isTTY
Expand Down Expand Up @@ -66,12 +67,14 @@ module.exports = coroutine(function*(
const localURL = `http${ssl ? 's' : ''}://localhost:${details.port}`
message += `- ${chalk.bold('Local: ')} ${localURL}`

try {
const ipAddress = ip.address()
const url = `http${ssl ? 's' : ''}://${ipAddress}:${details.port}`
if (!isLocal) {
try {
const ipAddress = ip.address()
const url = `http${ssl ? 's' : ''}://${ipAddress}:${details.port}`

message += `\n- ${chalk.bold('On Your Network: ')} ${url}`
} catch (err) {}
message += `\n- ${chalk.bold('On Your Network: ')} ${url}`
} catch (err) {}
}

if (isTTY && clipboard) {
try {
Expand Down
11 changes: 9 additions & 2 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ exports.options = [
name: ['T', 'ssl'],
description: 'Serve content using SSL',
defaultValue: false
},
{
name: 'local',
description: 'Serve content only on localhost',
defaultValue: false
}
]

Expand All @@ -67,7 +72,8 @@ exports.minimist = {
u: 'unzipped',
n: 'clipless',
o: 'open',
t: 'treeless'
t: 'treeless',
l: 'local'
},
boolean: [
'auth',
Expand All @@ -78,6 +84,7 @@ exports.minimist = {
'clipless',
'open',
'treeless',
'ssl'
'ssl',
'local'
]
}
7 changes: 7 additions & 0 deletions test/snapshots/options.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Generated by [AVA](https://ava.li).
C: 'cors',
S: 'silent',
a: 'auth',
l: 'local',
n: 'clipless',
o: 'open',
s: 'single',
Expand All @@ -29,6 +30,7 @@ Generated by [AVA](https://ava.li).
'open',
'treeless',
'ssl',
'local',
],
}

Expand Down Expand Up @@ -97,4 +99,9 @@ Generated by [AVA](https://ava.li).
'ssl',
],
},
{
defaultValue: false,
description: 'Serve content only on localhost',
name: 'local',
},
]
Binary file modified test/snapshots/options.js.snap
Binary file not shown.

0 comments on commit 297748b

Please sign in to comment.