index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  3. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. var {
  6. execSync
  7. } = require('child_process');
  8. var OSX_CHROME = 'google chrome';
  9. var Actions = Object.freeze({
  10. NONE: 0,
  11. BROWSER: 1
  12. });
  13. var getBrowserEnv = () => {
  14. // Attempt to honor this environment variable.
  15. // It is specific to the operating system.
  16. // See https://github.com/sindresorhus/open#app for documentation.
  17. var value = process.env.BROWSER;
  18. var args = process.env.BROWSER_ARGS ? process.env.BROWSER_ARGS.split(' ') : [];
  19. var action;
  20. if (!value) {
  21. // Default.
  22. action = Actions.BROWSER;
  23. } else if (value.toLowerCase() === 'none') {
  24. action = Actions.NONE;
  25. } else {
  26. action = Actions.BROWSER;
  27. }
  28. return {
  29. action,
  30. value,
  31. args
  32. };
  33. };
  34. var normalizeURLToMatch = target => {
  35. // We may encounter URL parse error but want to fallback to default behavior
  36. try {
  37. // Url module is deprecated on newer version of NodeJS, only use it when URL class is not supported (like Node 8)
  38. var URL = // eslint-disable-next-line node/prefer-global/url
  39. typeof global.URL === 'undefined' ? require('url').URL : global.URL;
  40. var url = new URL(target);
  41. return url.origin;
  42. } catch (_unused) {
  43. return target;
  44. }
  45. }; // Copy from
  46. // https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/openBrowser.js#L64
  47. // eslint-disable-next-line unicorn/prevent-abbreviations
  48. var startBrowserProcess = function startBrowserProcess(browser, url) {
  49. var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  50. var args = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
  51. // If we're on OS X, the user hasn't specifically
  52. // requested a different browser, we can try opening
  53. // Chrome with AppleScript. This lets us reuse an
  54. // existing tab when possible instead of creating a new one.
  55. var shouldTryOpenChromiumWithAppleScript = process.platform === 'darwin' && (typeof browser !== 'string' || browser === OSX_CHROME);
  56. if (shouldTryOpenChromiumWithAppleScript) {
  57. // Will use the first open browser found from list
  58. var supportedChromiumBrowsers = ['Google Chrome Canary', 'Google Chrome', 'Microsoft Edge', 'Brave Browser', 'Vivaldi', 'Chromium'];
  59. for (var chromiumBrowser of supportedChromiumBrowsers) {
  60. try {
  61. // Try our best to reuse existing tab
  62. // on OSX Chromium-based browser with AppleScript
  63. execSync('ps cax | grep "' + chromiumBrowser + '"');
  64. execSync("osascript ../openChrome.applescript \"".concat(encodeURI(url), "\" \"").concat(process.env.OPEN_MATCH_HOST_ONLY === 'true' ? encodeURI(normalizeURLToMatch(url)) : encodeURI(url), "\" \"").concat(chromiumBrowser, "\""), {
  65. cwd: __dirname,
  66. stdio: 'ignore'
  67. });
  68. return Promise.resolve(true); // eslint-disable-next-line no-unused-vars
  69. } catch (error) {// Ignore errors.
  70. // It it breaks, it will fallback to `opn` anyway
  71. }
  72. }
  73. } // Another special case: on OS X, check if BROWSER has been set to "open".
  74. // In this case, instead of passing `open` to `opn` (which won't work),
  75. // just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
  76. // https://github.com/facebook/create-react-app/pull/1690#issuecomment-283518768
  77. if (process.platform === 'darwin' && browser === 'open') {
  78. browser = undefined;
  79. } // Fallback to opn
  80. // (It will always open new tab)
  81. var options = _objectSpread({
  82. app: {
  83. name: browser,
  84. arguments: args
  85. },
  86. wait: false
  87. }, opts);
  88. return require('open')(url, options);
  89. };
  90. module.exports = (target, options) => {
  91. var {
  92. action,
  93. value,
  94. args
  95. } = getBrowserEnv();
  96. switch (action) {
  97. case Actions.NONE:
  98. // Special case: BROWSER="none" will prevent opening completely.
  99. return false;
  100. case Actions.BROWSER:
  101. return startBrowserProcess(value, target, options, args);
  102. default:
  103. throw new Error('Not implemented.');
  104. }
  105. };