diff --git a/package.json b/package.json index 3aae018a114947..1cb13654fa0ae6 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "absolute-path": "^0.0.0", "babel": "^5.8.23", "babel-core": "^5.8.23", + "browser-launcher2": "0.4.6", "bser": "^1.0.2", "chalk": "^1.1.1", "connect": "^2.8.3", 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 9f0fd6c14d1ca1..893a3c4c4b18b1 100644 --- a/packager/packager.js +++ b/packager/packager.js @@ -13,6 +13,7 @@ var path = require('path'); var childProcess = require('child_process'); var http = require('http'); var isAbsolutePath = require('absolute-path'); +var launcher = require('browser-launcher2'); var getFlowTypeCheckMiddleware = require('./getFlowTypeCheckMiddleware'); @@ -57,6 +58,9 @@ var options = parseCommandLine([{ command: 'reset-cache', description: 'Removes cached files', default: false, +}, { + command: 'dangerouslyDisableChromeDebuggerWebSecurity', + description: 'Disable the Chrome debugger\'s same-origin policy' }]); if (options.projectRoots) { @@ -191,17 +195,35 @@ function getDevToolsLauncher(options) { fs.createReadStream(workerPath).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'; + var chromeOptions = + options.dangerouslyDisableChromeDebuggerWebSecurity ? + ['--disable-web-security'] : + []; console.log('Launching Dev Tools...'); - childProcess.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); + return; } - console.log(stdout); - console.warn(stderr); + launch(debuggerURL, { + browser: 'chrome', + options: chromeOptions, + }, function(err, instance) { + if (err) { + console.error('Failed to launch chrome', err); + next(err); + return; + } + 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, +};