Skip to content

Commit 23878a1

Browse files
committed
add support for opening a browser
1 parent 679120e commit 23878a1

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

package-lock.json

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"lodash": "^4.17.15",
1313
"morgan": "^1.10.0",
1414
"nopt": "^3.0.1",
15+
"open": "^7.4.2",
1516
"readline-stream": "^1.0.2",
1617
"serialize-javascript": ">=3.1.0",
1718
"serve-static": "^1.14.1",

swank.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const http = require('http')
44
const connect = require('connect')
55
const serveStatic = require('serve-static')
66
const colors = require('colors/safe')
7+
const open = require('open')
78
const childProcess = require('child_process')
89
const ReadlineStream = require('readline-stream')
910
const { isObject } = require('lodash')
@@ -203,6 +204,7 @@ function processArgs () {
203204
path: path,
204205
help: Boolean,
205206
log: Boolean,
207+
open: Boolean,
206208
ngrok: Boolean,
207209
watch: Boolean,
208210
interval: Number
@@ -213,6 +215,7 @@ function processArgs () {
213215
d: '--path',
214216
h: '--help',
215217
l: '--log',
218+
o: '--open',
216219
n: '--ngrok',
217220
w: '--watch',
218221
i: '--interval',
@@ -231,10 +234,11 @@ function processArgs () {
231234
// start by returning usage info if requested
232235
if (opts.help) {
233236
console.log(`
234-
Usage: swank [[--ngrok | -n]] [[--watch | -w]] [[--silent]] [[--interval | -i SECONDS]] [[--port | -p PORT]] [[ [[--path | -d]] root_directory]]
237+
Usage: swank [[--silent]] [[--open | -o]] [[--ngrok | -n]] [[--watch | -w]] [[--interval | -i SECONDS]] [[--port | -p PORT]] [[ [[--path | -d]] root_directory]]
235238
236239
--ngrok: pipe your server through [ngrok's](https://www.npmjs.org/package/ngrok) local tunnel
237240
--watch: a watch+livereload server. Includes "livereload.js" in HTML files, starts the livereload server, and watches your directory, causing a reload when files change
241+
--open: open a browser after the server starts
238242
--interval: watch interval. Defaults to 1s
239243
--silent: disable logging of requests
240244
--port: specify the local port to use. Defaults to $PORT or 8000
@@ -244,8 +248,13 @@ function processArgs () {
244248
}
245249

246250
serve(opts)
247-
.then(swank => console.log(colors.green(`\n${swank.url}\n\n`)))
248-
.catch(err => console.error(err, colors.red(err.message)))
251+
.then(swank => {
252+
console.log(colors.green(`\n${swank.url}\n\n`))
253+
if (opts.open) {
254+
return open(swank.url)
255+
}
256+
})
257+
.catch(err => console.error(colors.red(err.message)))
249258
}
250259

251260
serve.Swank = Swank

0 commit comments

Comments
 (0)