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 14, 2015
1 parent f03c7b5 commit 483d61f
Show file tree
Hide file tree
Showing 5 changed files with 38 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 @@ -11,8 +11,20 @@
var execFile = require('child_process').execFile;
var fs = require('fs');
var path = require('path');
var opn = require('opn');

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.

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

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

function isDebuggerConnected() {
// Debugger is connected if the app and at least one browser are connected
// to the websocket proxy.
return proxy.clientsCount() >= 2;
}

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

return wss;
return {
server: wss,
clientsCount: function() {
return clients.length;
}
};
}

module.exports = {
Expand Down

0 comments on commit 483d61f

Please sign in to comment.