devToolsMiddleware.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getDevToolsMiddleware;
  6. function _cliTools() {
  7. const data = require("@react-native-community/cli-tools");
  8. _cliTools = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _child_process() {
  14. const data = require("child_process");
  15. _child_process = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. /**
  21. * Copyright (c) Facebook, Inc. and its affiliates.
  22. *
  23. * This source code is licensed under the MIT license found in the
  24. * LICENSE file in the root directory of this source tree.
  25. */
  26. function launchDefaultDebugger(host, port, args = '') {
  27. const hostname = host || 'localhost';
  28. const debuggerURL = `http://${hostname}:${port}/debugger-ui${args}`;
  29. _cliTools().logger.info('Launching Dev Tools...');
  30. (0, _cliTools().launchDebugger)(debuggerURL);
  31. }
  32. function escapePath(pathname) {
  33. // " Can escape paths with spaces in OS X, Windows, and *nix
  34. return `"${pathname}"`;
  35. }
  36. function launchDevTools({
  37. host,
  38. port,
  39. watchFolders
  40. }, isDebuggerConnected) {
  41. // Explicit config always wins
  42. const customDebugger = process.env.REACT_DEBUGGER;
  43. if (customDebugger) {
  44. startCustomDebugger({
  45. watchFolders,
  46. customDebugger
  47. });
  48. } else if (!isDebuggerConnected()) {
  49. // Debugger is not yet open; we need to open a session
  50. launchDefaultDebugger(host, port);
  51. }
  52. }
  53. function startCustomDebugger({
  54. watchFolders,
  55. customDebugger
  56. }) {
  57. const folders = watchFolders.map(escapePath).join(' ');
  58. const command = `${customDebugger} ${folders}`;
  59. _cliTools().logger.info('Starting custom debugger by executing:', command);
  60. (0, _child_process().exec)(command, function (error) {
  61. if (error !== null) {
  62. _cliTools().logger.error('Error while starting custom debugger:', error.stack || '');
  63. }
  64. });
  65. }
  66. function getDevToolsMiddleware(options, isDebuggerConnected) {
  67. return function devToolsMiddleware(_req, res) {
  68. launchDevTools(options, isDebuggerConnected);
  69. res.end('OK');
  70. };
  71. }
  72. //# sourceMappingURL=devToolsMiddleware.js.map