Skip to content

Commit

Permalink
Replace applescript with https://github.com/sindresorhus/opn
Browse files Browse the repository at this point in the history
This allows opening the Chrome debugger on OS X, Linux, and Windows.

`isDebuggerConnected()` addresses the problem in facebook#510 where the dev tools
would only open once per server session.
  • Loading branch information
elliottsj committed Oct 15, 2015
1 parent f03c7b5 commit 3ee9096
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 62 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"joi": "^6.6.1",
"jstransform": "^11.0.3",
"module-deps": "^3.9.1",
"opn": "^3.0.2",
"optimist": "^0.6.1",
"progress": "^1.1.8",
"promise": "^7.0.4",
Expand Down
35 changes: 23 additions & 12 deletions packager/getDevToolsMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -29,20 +41,19 @@ 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);
next(err)
}
);
res.end('OK');
});
} else {
next();
}
Expand Down
47 changes: 0 additions & 47 deletions packager/launchChromeDevTools.applescript

This file was deleted.

8 changes: 6 additions & 2 deletions packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ var server = runServer(options, function() {
console.log('\nReact packager ready.\n');
});

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

function isDebuggerConnected() {
return wsProxy.getClientUserAgents().some(userAgent => userAgent.includes('Chrome'));
}

function getAppMiddleware(options) {
var transformerPath = options.transformer;
Expand Down Expand Up @@ -191,7 +195,7 @@ function runServer(
) {
var app = connect()
.use(loadRawBodyMiddleware)
.use(getDevToolsMiddleware(options))
.use(getDevToolsMiddleware(options, isDebuggerConnected))
.use(openStackFrameInEditorMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
Expand Down
8 changes: 7 additions & 1 deletion packager/webSocketProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ function attachToServer(server, path) {
});
});

return wss;
return {
server: wss,
getClientUserAgents: () =>
clients
.map(ws => ws.upgradeReq.headers['user-agent'])
.filter(Boolean)
};
}

module.exports = {
Expand Down

0 comments on commit 3ee9096

Please sign in to comment.