diff --git a/package.json b/package.json index 0c0bf85e902400..8ba67bb6a9f7e5 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "joi": "^6.6.1", "jstransform": "^11.0.3", "module-deps": "^3.9.1", + "opn": "^3.0.2", "optimist": "^0.6.1", "progress": "^1.1.8", "promise": "^7.0.4", diff --git a/packager/getDevToolsMiddleware.js b/packager/getDevToolsMiddleware.js index 2c1d693aeae6fc..a9226aa85b7add 100644 --- a/packager/getDevToolsMiddleware.js +++ b/packager/getDevToolsMiddleware.js @@ -11,8 +11,20 @@ var execFile = require('child_process').execFile; var fs = require('fs'); var path = require('path'); +var opn = require('opn'); -module.exports = function(options) { +function getChromeAppName() { + switch (process.platform) { + case 'darwin': + return 'google chrome'; + case 'win32': + return 'chrome'; + default: + return 'google-chrome'; + } +} + +module.exports = function(options, isDebuggerConnected) { return function(req, res, next) { if (req.url === '/debugger-ui') { var debuggerPath = path.join(__dirname, 'debugger.html'); @@ -29,20 +41,19 @@ module.exports = function(options) { 'If you still need this, please let us know.' ); } else if (req.url === '/launch-chrome-devtools') { + if (isDebuggerConnected()) { + // Dev tools are already open; no need to open another session + res.end('OK'); + return; + } var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui'; - var script = 'launchChromeDevTools.applescript'; console.log('Launching Dev Tools...'); - execFile( - path.join(__dirname, script), [debuggerURL], - function(err, stdout, stderr) { - if (err) { - console.log('Failed to run ' + script, err); - } - console.log(stdout); - console.warn(stderr); + opn(debuggerURL, {app: [getChromeAppName()]}, function(err) { + if (err) { + console.error('Google Chrome exited with error:', err); + next(err) } - ); - res.end('OK'); + }); } else { next(); } diff --git a/packager/launchChromeDevTools.applescript b/packager/launchChromeDevTools.applescript deleted file mode 100755 index a839079053f25d..00000000000000 --- a/packager/launchChromeDevTools.applescript +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env osascript - --- Copyright (c) 2015-present, Facebook, Inc. --- All rights reserved. --- --- This source code is licensed under the BSD-style license found in the --- LICENSE file in the root directory of this source tree. An additional grant --- of patent rights can be found in the PATENTS file in the same directory. - -on run argv - set theURL to item 1 of argv - - tell application "Chrome" - - if (count every window) = 0 then - make new window - end if - - -- Find a tab currently running the debugger - set found to false - set theTabIndex to -1 - repeat with theWindow in every window - set theTabIndex to 0 - repeat with theTab in every tab of theWindow - set theTabIndex to theTabIndex + 1 - if theTab's URL is theURL then - set found to true - exit repeat - end if - end repeat - - if found then - exit repeat - end if - end repeat - - if found then - set index of theWindow to 1 - set theWindow's active tab index to theTabIndex - else - tell window 1 - activate - make new tab with properties {URL:theURL} - end tell - end if - end tell -end run diff --git a/packager/packager.js b/packager/packager.js index 70324e644022db..3233fafed9b6ca 100644 --- a/packager/packager.js +++ b/packager/packager.js @@ -160,7 +160,13 @@ var server = runServer(options, function() { console.log('\nReact packager ready.\n'); }); -webSocketProxy.attachToServer(server, '/debugger-proxy'); +var proxy = webSocketProxy.attachToServer(server, '/debugger-proxy'); + +function isDebuggerConnected() { + // Debugger is connected if the app and at least one browser are connected + // to the websocket proxy. + return proxy.clientsCount() >= 2; +} function getAppMiddleware(options) { var transformerPath = options.transformer; @@ -191,7 +197,7 @@ function runServer( ) { var app = connect() .use(loadRawBodyMiddleware) - .use(getDevToolsMiddleware(options)) + .use(getDevToolsMiddleware(options, isDebuggerConnected)) .use(openStackFrameInEditorMiddleware) .use(statusPageMiddleware) .use(systraceProfileMiddleware) diff --git a/packager/webSocketProxy.js b/packager/webSocketProxy.js index 0562f050add6eb..567708267f02ee 100644 --- a/packager/webSocketProxy.js +++ b/packager/webSocketProxy.js @@ -57,7 +57,12 @@ function attachToServer(server, path) { }); }); - return wss; + return { + server: wss, + clientsCount: function() { + return clients.length; + } + }; } module.exports = {