openBrowser.js 3.5 KB

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