Skip to content

Commit

Permalink
Replace applescript with browser-launcher2
Browse files Browse the repository at this point in the history
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 facebook#510 where the dev tools
would only open once per server session.

Add a '--dangerouslyDisableChromeDebuggerWebSecurity' flag to
packager.js to enable Chrome '--disable-web-security' flag.

This allows users to inspect network requests in Chrome by commenting
the xhr polyfill in InitializeJavaScriptAppEngine.js:
  facebook#934 (comment)

Usage:

    node packager.js --dangerouslyDisableChromeDebuggerWebSecurity

or:

    packager.sh --dangerouslyDisableChromeDebuggerWebSecurity
  • Loading branch information
elliottsj committed Aug 23, 2015
1 parent 2b0e8dd commit a290ebd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 56 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 0 additions & 47 deletions packager/launchChromeDevTools.applescript

This file was deleted.

32 changes: 26 additions & 6 deletions packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -64,6 +65,9 @@ var options = parseCommandLine([{
type: 'string',
default: require.resolve('./transformer.js'),
description: 'Specify a custom transformer to be used (absolute path)'
}, {
command: 'dangerouslyDisableChromeDebuggerWebSecurity',
description: 'Disable the Chrome debugger\'s same-origin policy'
}]);

if (options.projectRoots) {
Expand Down Expand Up @@ -192,17 +196,33 @@ 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';
var chromeOptions =
options.dangerouslyDisableChromeDebuggerWebSecurity ?
['--disable-web-security'] :
[];
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',
options: chromeOptions,
}, function(err, instance) {
if (err) {
console.error('Failed to launch chrome', err);
next(err);
}
res.end('OK');
});
});
res.end('OK');
} else {
next();
}
Expand Down
13 changes: 10 additions & 3 deletions packager/webSocketProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
};

0 comments on commit a290ebd

Please sign in to comment.