Skip to content

Commit

Permalink
Open local address in default browser (#198)
Browse files Browse the repository at this point in the history
* argument added to open URL directly

* message added to console

* local disabled by default

* errors fixed

* option name changed to 'open'

* requested changes resolved

* Error fixed
  • Loading branch information
webtaculars authored and leo committed May 23, 2017
1 parent 2026885 commit e7a3c36
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
9 changes: 5 additions & 4 deletions bin/serve.js
Expand Up @@ -50,7 +50,7 @@ args
)
.option('silent', `Don't log anything to the console`)
.option('no-clipboard', `Don't copy address to clipboard`, false)

.option('open', 'Open local address in browser', false)
const flags = args.parse(process.argv, {
minimist: {
alias: {
Expand All @@ -59,9 +59,10 @@ const flags = args.parse(process.argv, {
S: 'silent',
s: 'single',
u: 'unzipped',
n: 'no-clipboard'
n: 'no-clipboard',
o: 'open'
},
boolean: ['auth', 'cors', 'silent', 'single', 'unzipped', 'no-clipboard']
boolean: ['auth', 'cors', 'silent', 'single', 'unzipped', 'no-clipboard', 'open']
}
})

Expand Down Expand Up @@ -108,7 +109,7 @@ detect(port).then(open => {
server.listen(
port,
coroutine(function*() {
yield listening(server, current, inUse, flags.noClipboard !== true)
yield listening(server, current, inUse, flags.noClipboard !== true, flags.open)
})
)
})
9 changes: 8 additions & 1 deletion lib/listening.js
Expand Up @@ -8,8 +8,9 @@ const pathType = require('path-type')
const chalk = require('chalk')
const boxen = require('boxen')
const { coroutine } = require('bluebird')
const opn = require('opn')

module.exports = coroutine(function*(server, current, inUse, clipboard) {
module.exports = coroutine(function*(server, current, inUse, clipboard, open) {
const details = server.address()
const isTTY = process.stdout.isTTY

Expand Down Expand Up @@ -67,6 +68,12 @@ module.exports = coroutine(function*(server, current, inUse, clipboard) {
} catch (err) {}
}

if (isTTY && open) {
try {
opn(localURL)
} catch (err) {}
}

console.log(
boxen(message, {
padding: 1,
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -66,6 +66,7 @@
"micro-compress": "1.0.0",
"mime-types": "2.1.15",
"node-version": "1.0.0",
"opn": "5.0.0",
"path-type": "2.0.0",
"send": "0.15.3",
"update-notifier": "2.1.0"
Expand Down

0 comments on commit e7a3c36

Please sign in to comment.