You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
955 B

4 years ago
  1. 'use strict';
  2. const open = require('opn');
  3. const isAbsoluteUrl = require('is-absolute-url');
  4. function runOpen(uri, options, log) {
  5. // https://github.com/webpack/webpack-dev-server/issues/1990
  6. let openOptions = { wait: false };
  7. let openOptionValue = '';
  8. if (typeof options.open === 'string') {
  9. openOptions = Object.assign({}, openOptions, { app: options.open });
  10. openOptionValue = `: "${options.open}"`;
  11. }
  12. const pages =
  13. typeof options.openPage === 'string'
  14. ? [options.openPage]
  15. : options.openPage || [''];
  16. return Promise.all(
  17. pages.map((page) => {
  18. const pageUrl = page && isAbsoluteUrl(page) ? page : `${uri}${page}`;
  19. return open(pageUrl, openOptions).catch(() => {
  20. log.warn(
  21. `Unable to open "${pageUrl}" in browser${openOptionValue}. If you are running in a headless environment, please do not use the --open flag`
  22. );
  23. });
  24. })
  25. );
  26. }
  27. module.exports = runOpen;