Skip to content

Commit

Permalink
Allow specifying which browser to open (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrynx authored and devongovett committed Mar 19, 2018
1 parent e7f9c64 commit 9858937
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ program
.option('--https', 'serves files over HTTPS')
.option('--cert <path>', 'path to certificate to use with HTTPS')
.option('--key <path>', 'path to private key to use with HTTPS')
.option('--open', 'automatically open in default browser')
.option(
'--open [browser]',
'automatically open in specified browser, defaults to default browser'
)
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
Expand Down Expand Up @@ -162,10 +165,11 @@ async function bundle(main, command) {
if (command.name() === 'serve') {
const server = await bundler.serve(command.port || 1234, command.https);
if (command.open) {
require('opn')(
await require('./utils/openInBrowser')(
`${command.https ? 'https' : 'http'}://localhost:${
server.address().port
}`
}`,
command.open
);
}
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/openInBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const opn = require('opn');

const openInBrowser = async (url, browser) => {
try {
const options = typeof browser === 'string' ? {app: browser} : undefined;

await opn(url, options);
} catch (err) {
console.error(`Unexpected error while opening in browser: ${browser}`);
console.error(err);
}
};

module.exports = openInBrowser;

0 comments on commit 9858937

Please sign in to comment.