From d51d32f4330539a37e47ec1bc470fac67d9b2f78 Mon Sep 17 00:00:00 2001 From: Spencer Elliott Date: Fri, 21 Aug 2015 20:19:31 -0400 Subject: [PATCH] Replace applescript with browser-launcher2 This allows... 1. launching Chrome on platforms other than OS X 2. users to launch their own instance of Chrome (e.g. via command line) rather than being forced to use the default instance (i.e. `tell application "Chrome"` always used the default instance) `isDebuggerConnected()` addresses the problem in #510 where the dev tools would only open once per server session. --- package.json | 1 + packager/launchChromeDevTools.applescript | 47 ----------------------- packager/packager.js | 24 +++++++++--- packager/webSocketProxy.js | 13 +++++-- 4 files changed, 29 insertions(+), 56 deletions(-) delete mode 100755 packager/launchChromeDevTools.applescript diff --git a/package.json b/package.json index db609d22e5888f..00f744523315f7 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "absolute-path": "0.0.0", "babel": "5.8.21", "babel-core": "5.8.21", + "browser-launcher2": "0.4.6", "chalk": "1.0.0", "connect": "2.8.3", "debug": "2.1.0", 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 fcff7e58f26a7d..0f93a3306fd033 100644 --- a/packager/packager.js +++ b/packager/packager.js @@ -13,6 +13,7 @@ var path = require('path'); var execFile = require('child_process').execFile; var http = require('http'); var isAbsolutePath = require('absolute-path'); +var launcher = require('browser-launcher2'); var getFlowTypeCheckMiddleware = require('./getFlowTypeCheckMiddleware'); @@ -192,17 +193,28 @@ function getDevToolsLauncher(options) { res.writeHead(200, {'Content-Type': 'text/html'}); fs.createReadStream(debuggerPath).pipe(res); } else if (req.url === '/launch-chrome-devtools') { + if (webSocketProxy.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) { + launcher(function(err, launch) { if (err) { - console.log('Failed to run ' + script, err); + console.error('Failed to initialize browser-launcher2', err); + next(err); } - console.log(stdout); - console.warn(stderr); + launch(debuggerURL, { + browser: 'chrome', + }, function(err, instance) { + if (err) { + console.error('Failed to launch chrome', err); + next(err); + } + res.end('OK'); + }); }); - res.end('OK'); } else { next(); } diff --git a/packager/webSocketProxy.js b/packager/webSocketProxy.js index 22151c4ea74d26..13ab25a22899cf 100644 --- a/packager/webSocketProxy.js +++ b/packager/webSocketProxy.js @@ -10,12 +10,13 @@ var WebSocketServer = require('ws').Server; +var clients = []; + function attachToServer(server, path) { var wss = new WebSocketServer({ server: server, path: path }); - var clients = []; function sendSpecial(message) { clients.forEach(function (cn) { @@ -59,6 +60,12 @@ function attachToServer(server, path) { }); } +function isDebuggerConnected() { + // Debugger is connected if the app and at least one browser are connected + return clients.length >= 2; +} + module.exports = { - attachToServer: attachToServer -}; + attachToServer: attachToServer, + isDebuggerConnected: isDebuggerConnected, +};