/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import http from 'http' ; import { launchDebugger , logger } from '@react-native-community/cli-tools' ; import { exec } from 'child_process' ; function launchDefaultDebugger ( host , port , args = '' ) { const hostname = host || 'localhost' ; const debuggerURL = `http://${ hostname }:${ port }/debugger-ui${ args }` ; logger . info ( 'Launching Dev Tools...' ) ; launchDebugger ( debuggerURL ) ; } function escapePath ( pathname ) { return `"${ pathname }"` ; } type LaunchDevToolsOptions = { host ? : string ; port : number ; watchFolders : ReadonlyArray < string > ; } ; function launchDevTools ( { host , port , watchFolders } , isDebuggerConnected ) { const customDebugger = process . env . REACT_DEBUGGER ; if ( customDebugger ) { startCustomDebugger ( { watchFolders , customDebugger } ) ; } else if ( ! isDebuggerConnected ( ) ) { launchDefaultDebugger ( host , port ) ; } } function startCustomDebugger ( { watchFolders , customDebugger , } ) { const folders = watchFolders . map ( escapePath ) . join ( ' ' ) ; const command = `${ customDebugger } ${ folders }` ; logger . info ( 'Starting custom debugger by executing:' , command ) ; exec ( command , function ( error ) { if ( error !== null ) { logger . error ( 'Error while starting custom debugger:' , error . stack || '' ) ; } } ) ; } export default function getDevToolsMiddleware ( options , isDebuggerConnected ) { return function devToolsMiddleware ( _req , res ) { launchDevTools ( options , isDebuggerConnected ) ; res . end ( 'OK' ) ; } ; }