diff --git a/getDevToolsMiddleware.js b/getDevToolsMiddleware.js index 2c1d693a..966e47c4 100644 --- a/getDevToolsMiddleware.js +++ b/getDevToolsMiddleware.js @@ -10,9 +10,21 @@ var execFile = require('child_process').execFile; var fs = require('fs'); +var opn = require('opn'); var path = require('path'); -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,19 +41,18 @@ 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); } - ); + }); res.end('OK'); } else { next(); diff --git a/launchChromeDevTools.applescript b/launchChromeDevTools.applescript deleted file mode 100755 index a8390790..00000000 --- a/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/webSocketProxy.js b/webSocketProxy.js index 0562f050..65c35464 100644 --- a/webSocketProxy.js +++ b/webSocketProxy.js @@ -22,7 +22,7 @@ function attachToServer(server, path) { try { cn.send(JSON.stringify(message)); } catch(e) { - console.warn('WARN: ' + e.message); + // Sometimes this call throws 'not opened' } }); } @@ -57,7 +57,14 @@ function attachToServer(server, path) { }); }); - return wss; + return { + server: wss, + isChromeConnected: () => + clients + .map(ws => ws.upgradeReq.headers['user-agent']) + .filter(Boolean) + .some(userAgent => userAgent.includes('Chrome')) + }; } module.exports = {