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 Sep 27, 2015
1 parent 4978855 commit 1ff0f35
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 57 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.23",
"babel-core": "^5.8.23",
"browser-launcher2": "0.4.6",
"bser": "^1.0.2",
"chalk": "^1.1.1",
"connect": "^2.8.3",
Expand Down
47 changes: 0 additions & 47 deletions packager/launchChromeDevTools.applescript

This file was deleted.

79 changes: 69 additions & 10 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 childProcess = require('child_process');
var http = require('http');
var isAbsolutePath = require('absolute-path');
var launcher = require('browser-launcher2');

var getFlowTypeCheckMiddleware = require('./getFlowTypeCheckMiddleware');

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -153,7 +157,7 @@ var server = runServer(options, function() {
console.log('\nReact packager ready.\n');
});

webSocketProxy.attachToServer(server, '/debugger-proxy');
var proxy = webSocketProxy.attachToServer(server, '/debugger-proxy');

function loadRawBody(req, res, next) {
req.rawBody = '';
Expand All @@ -178,7 +182,47 @@ function openStackFrameInEditor(req, res, next) {
}
}

function closeChromeInstance(instance) {
return new Promise(function(resolve, reject) {
if (!instance) {
resolve();
return;
}
instance.stop(function(err) {
if (err) {
reject(err);
return;
}
resolve();
});
});
}

function launchChrome(url, options) {
return new Promise(function(resolve, reject) {
launcher(function(err, launch) {
if (err) {
console.error('Failed to initialize browser-launcher2', err);
reject(err);
return;
}
launch(url, {
browser: 'chrome',
options: options,
}, function(err, instance) {
if (err) {
console.error('Failed to launch chrome', err);
reject(err);
return;
}
resolve(instance);
});
});
});
}

function getDevToolsLauncher(options) {
var chromeInstance;
return function(req, res, next) {
if (req.url === '/debugger-ui') {
var debuggerPath = path.join(__dirname, 'debugger.html');
Expand All @@ -191,17 +235,32 @@ function getDevToolsLauncher(options) {
fs.createReadStream(workerPath).pipe(res);

} else if (req.url === '/launch-chrome-devtools') {
if (proxy.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) {
if (err) {
console.log('Failed to run ' + script, err);
}
console.log(stdout);
console.warn(stderr);
});
res.end('OK');
closeChromeInstance(chromeInstance)
.then(function() {
return launchChrome(debuggerURL, chromeOptions)
})
.then(function(instance) {
// Keep a reference to the Chrome instance and unset it if Chrome stops
chromeInstance = instance;
chromeInstance.on('stop', function() {
chromeInstance = null;
});
res.end('OK');
})
.catch(function(err) {
next(err);
});
} else {
next();
}
Expand Down
7 changes: 7 additions & 0 deletions packager/webSocketProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function attachToServer(server, path) {
});
});
});

return {
isDebuggerConnected: function() {
// Debugger is connected if the app and at least one browser are connected
return clients.length >= 2;
}
};
}

module.exports = {
Expand Down

0 comments on commit 1ff0f35

Please sign in to comment.