Skip to content

Commit

Permalink
Allow --open option to specify the browser to use (#825)
Browse files Browse the repository at this point in the history
* Add optionally browser parameter to --open

* Fix code style issues

* Fix last code style issue

* Replace array spread

* Fix whitespace issue

* Add .idea to gitignore and remove files

* Fix whitespaces

* Ignore and remove package-lock.json

* refactor argument composition

* removing trailing spaces

* improve open option description to match pr
  • Loading branch information
frankwinter authored and shellscape committed Aug 30, 2017
1 parent cf5dda8 commit f00fcb3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node_modules
/client/sockjs.bundle.js
/coverage
*.pem
.idea/
/package-lock.json
23 changes: 18 additions & 5 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ yargs.options({
describe: 'close when stdin ends'
},
open: {
type: 'boolean',
describe: 'Open default browser'
type: 'string',
describe: 'Open the default browser, or optionally specify a browser name'
},
useLocalIp: {
type: 'boolean',
Expand Down Expand Up @@ -324,11 +324,15 @@ function processOptions(webpackOptions) {

if (argv['disable-host-check']) { options.disableHostCheck = true; }

if (argv.open || argv['open-page']) {
if (argv['open-page']) {
options.open = true;
options.openPage = argv['open-page'];
}

if (typeof argv.open !== 'undefined') {
options.open = argv.open !== '' ? argv.open : true;
}

if (options.open && !options.openPage) { options.openPage = ''; }

if (argv.useLocalIp) { options.useLocalIp = true; }
Expand Down Expand Up @@ -452,9 +456,18 @@ function reportReadiness(uri, options) {

if (options.bonjour) { console.log('Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'); }
}

if (options.open) {
open(uri + options.openPage).catch(() => {
console.log('Unable to open browser. If you are running in a headless environment, please do not use the open flag.');
let openOptions = {};
let openMessage = 'Unable to open browser';

if (typeof options.open === 'string') {
openOptions = { app: options.open };
openMessage += `: ${options.open}`;
}

open(uri + (options.openPage || ''), openOptions).catch(() => {
console.log(`${openMessage}. If you are running in a headless environment, please do not use the open flag.`);
});
}
}
Expand Down
11 changes: 9 additions & 2 deletions lib/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,15 @@
"type": "boolean"
},
"open": {
"description": "Let the CLI open your browser.",
"type": "boolean"
"description": "Let the CLI open your browser with the URL.",
"anyOf": [
{
"type": "string"
},
{
"type": "boolean"
}
]
},
"useLocalIp": {
"description": "Let the browser open with your local IP.",
Expand Down

0 comments on commit f00fcb3

Please sign in to comment.