Skip to content

Instantly share code, notes, and snippets.

@nicola
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicola/8c527f9eaf5ff8992338 to your computer and use it in GitHub Desktop.
Save nicola/8c527f9eaf5ff8992338 to your computer and use it in GitHub Desktop.
Discover a B2G simulator port in NodeJS
var discoverPorts = function (callback) {
var ports = [];
if (os == 'darwin') {
var output = exec(LSOF_CMD, {silent: true}).output;
var regex = /^b2g[-bin]?.*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:([0-9]*)/;
var lines = output.split('\n');
for (var line=0; line < lines.length; line++) {
var matches = regex.exec(lines[line]);
if (matches && +matches[1] != 2828)
ports.push(+matches[1])
}
} else
if (os == 'linux') {
var output = exec(NETSTAT_CMD, {silent: true}).output;
var regex = /tcp.*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:([0-9]+).*LISTEN.*\/b2g[-bin]?/
var lines = output.split('\n');
for (var line=0; line < lines.length; line++) {
var matches = regex.exec(lines[line]);
if (matches && +matches[1] != 2828)
ports.push(+matches[1])
}
} else {
return callback(new Error("OS not supported for running"))
}
callback(null, ports)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment