openBrowser.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  53. })
  54. return true
  55. }
  56. function startBrowserProcess (browser, url) {
  57. // If we're on OS X, the user hasn't specifically
  58. // requested a different browser, we can try opening
  59. // Chrome with AppleScript. This lets us reuse an
  60. // existing tab when possible instead of creating a new one.
  61. const shouldTryOpenChromeWithAppleScript =
  62. process.platform === 'darwin' &&
  63. (typeof browser !== 'string' || browser === OSX_CHROME)
  64. if (shouldTryOpenChromeWithAppleScript) {
  65. try {
  66. // Try our best to reuse existing tab
  67. // on OS X Google Chrome with AppleScript
  68. execSync('ps cax | grep "Google Chrome"')
  69. execSync('osascript openChrome.applescript "' + encodeURI(url) + '"', {
  70. cwd: __dirname,
  71. stdio: 'ignore'
  72. })
  73. return true
  74. } catch (err) {
  75. // Ignore errors.
  76. }
  77. }
  78. // Another special case: on OS X, check if BROWSER has been set to "open".
  79. // In this case, instead of passing the string `open` to `open` function (which won't work),
  80. // just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
  81. // https://github.com/facebook/create-react-app/pull/1690#issuecomment-283518768
  82. if (process.platform === 'darwin' && browser === 'open') {
  83. browser = undefined
  84. }
  85. // Fallback to open
  86. // (It will always open new tab)
  87. try {
  88. const options = { app: { name: browser }, url: true }
  89. open(url, options).catch(() => {}) // Prevent `unhandledRejection` error.
  90. return true
  91. } catch (err) {
  92. return false
  93. }
  94. }
  95. /**
  96. * Reads the BROWSER environment variable and decides what to do with it. Returns
  97. * true if it opened a browser or ran a node.js script, otherwise false.
  98. */
  99. exports.openBrowser = function (url) {
  100. const { action, value } = getBrowserEnv()
  101. switch (action) {
  102. case Actions.NONE:
  103. // Special case: BROWSER="none" will prevent opening completely.
  104. return false
  105. case Actions.SCRIPT:
  106. return executeNodeScript(value, url)
  107. case Actions.BROWSER:
  108. return startBrowserProcess(value, url)
  109. default:
  110. throw new Error('Not implemented.')
  111. }
  112. }