Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

127 рядки
3.5 KiB

  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 in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. var chalk = require('chalk');
  9. var execSync = require('child_process').execSync;
  10. var spawn = require('cross-spawn');
  11. var opn = require('opn');
  12. // https://github.com/sindresorhus/opn#app
  13. var 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/opn#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 = spawn('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 `open` to `opn` (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 opn
  87. // (It will always open new tab)
  88. try {
  89. var options = { app: browser };
  90. opn(url, options).catch(() => {}); // Prevent `unhandledRejection` error.
  91. return true;
  92. } catch (err) {
  93. return false;
  94. }
  95. }
  96. /**
  97. * Reads the BROWSER evironment 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. function openBrowser(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. }
  114. module.exports = openBrowser;