openBrowser.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. /**
  3. * Copyright (c) 2015-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file at
  7. * https://github.com/facebook/create-react-app/blob/master/LICENSE
  8. */
  9. var __importDefault = (this && this.__importDefault) || function (mod) {
  10. return (mod && mod.__esModule) ? mod : { "default": mod };
  11. };
  12. Object.defineProperty(exports, "__esModule", { value: true });
  13. exports.openBrowser = void 0;
  14. const path_1 = __importDefault(require("path"));
  15. const open_1 = __importDefault(require("open"));
  16. const execa_1 = __importDefault(require("execa"));
  17. const chalk_1 = __importDefault(require("chalk"));
  18. const child_process_1 = require("child_process");
  19. // https://github.com/sindresorhus/open#app
  20. const OSX_CHROME = 'google chrome';
  21. function getBrowserEnv() {
  22. // Attempt to honor this environment variable.
  23. // It is specific to the operating system.
  24. // See https://github.com/sindresorhus/open#app for documentation.
  25. const value = process.env.BROWSER;
  26. let action;
  27. if (!value) {
  28. // Default.
  29. action = 1 /* BROWSER */;
  30. }
  31. else if (value.toLowerCase().endsWith('.js')) {
  32. action = 2 /* SCRIPT */;
  33. }
  34. else if (value.toLowerCase() === 'none') {
  35. action = 0 /* NONE */;
  36. }
  37. else {
  38. action = 1 /* BROWSER */;
  39. }
  40. return { action, value };
  41. }
  42. function executeNodeScript(scriptPath, url) {
  43. const extraArgs = process.argv.slice(2);
  44. const child = execa_1.default('node', [scriptPath, ...extraArgs, url], {
  45. stdio: 'inherit'
  46. });
  47. child.on('close', (code) => {
  48. if (code !== 0) {
  49. console.log();
  50. console.log(chalk_1.default.red('The script specified as BROWSER environment variable failed.'));
  51. console.log(chalk_1.default.cyan(scriptPath) + ' exited with code ' + code + '.');
  52. console.log();
  53. return;
  54. }
  55. });
  56. return true;
  57. }
  58. function startBrowserProcess(browser, url) {
  59. // If we're on OS X, the user hasn't specifically
  60. // requested a different browser, we can try opening
  61. // Chrome with AppleScript. This lets us reuse an
  62. // existing tab when possible instead of creating a new one.
  63. const shouldTryOpenChromeWithAppleScript = process.platform === 'darwin' &&
  64. (typeof browser !== 'string' || browser === OSX_CHROME);
  65. if (shouldTryOpenChromeWithAppleScript) {
  66. try {
  67. // Try our best to reuse existing tab
  68. // on OS X Google Chrome with AppleScript
  69. child_process_1.execSync('ps cax | grep "Google Chrome"');
  70. child_process_1.execSync('osascript openChrome.applescript "' + encodeURI(url) + '"', {
  71. cwd: path_1.default.resolve(__dirname, '../../bin'),
  72. stdio: 'ignore'
  73. });
  74. return true;
  75. }
  76. catch (err) {
  77. // Ignore errors
  78. }
  79. }
  80. // Another special case: on OS X, check if BROWSER has been set to "open".
  81. // In this case, instead of passing the string `open` to `open` function (which won't work),
  82. // just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
  83. // https://github.com/facebook/create-react-app/pull/1690#issuecomment-283518768
  84. if (process.platform === 'darwin' && browser === 'open') {
  85. browser = undefined;
  86. }
  87. // Fallback to open
  88. // (It will always open new tab)
  89. try {
  90. var options = { app: browser, url: true };
  91. open_1.default(url, options).catch(() => { }); // Prevent `unhandledRejection` error.
  92. return true;
  93. }
  94. catch (err) {
  95. return false;
  96. }
  97. }
  98. /**
  99. * Reads the BROWSER environment variable and decides what to do with it. Returns
  100. * true if it opened a browser or ran a node.js script, otherwise false.
  101. */
  102. function openBrowser(url) {
  103. const { action, value } = getBrowserEnv();
  104. switch (action) {
  105. case 0 /* NONE */:
  106. // Special case: BROWSER="none" will prevent opening completely.
  107. return false;
  108. case 2 /* SCRIPT */:
  109. return executeNodeScript(value, url);
  110. case 1 /* BROWSER */:
  111. return startBrowserProcess(value, url);
  112. default:
  113. throw new Error('Not implemented.');
  114. }
  115. }
  116. exports.openBrowser = openBrowser;
  117. //# sourceMappingURL=openBrowser.js.map