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)

`isClientConnected()` addresses the problem in facebook#510 where the dev tools
would only open once per server session.
  • Loading branch information
elliottsj committed Aug 22, 2015
1 parent 4b420cc commit cb5d8db
Show file tree
Hide file tree
Showing 4 changed files with 29 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.

24 changes: 18 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 @@ -192,17 +193,28 @@ 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';
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',
}, 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 is connected
return clients.length >= 2;
}

module.exports = {
attachToServer: attachToServer
};
attachToServer: attachToServer,
isDebuggerConnected: isDebuggerConnected,
};

0 comments on commit cb5d8db

Please sign in to comment.