Last active
August 29, 2015 14:04
-
-
Save nicola/8c527f9eaf5ff8992338 to your computer and use it in GitHub Desktop.
Discover a B2G simulator port in NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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