Skip to content

Commit

Permalink
Update to resolve port synchronously.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaridmargolin committed Feb 26, 2017
1 parent 2c3105b commit 958175e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
22 changes: 5 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const spawn = require('child_process').spawn
const _ = require('lodash')
const which = require('which')
const exitHook = require('exit-hook')
const portfinder = require('portfinder')
const portfinderSync = require('portfinder-sync')
const log = require('npmlog')

// lib
Expand All @@ -35,33 +35,21 @@ module.exports = function (cmd, options) {
devtoolsOptions['debug-brk'] = options['nodeArgs'].includes('--debug-brk')

const devtools = new Devtools(devtoolsOptions)
const inspectPromise = findOpenPort()
.then((port) => inspectProcess(cmd, options, port, devtools))

inspectPromise.devtools = devtools
return inspectPromise
}

const findOpenPort = function () {
// node defaults to 9229 so lets start there
portfinder.basePort = 9229

return new Promise(function (resolve, reject) {
portfinder.getPort((err, port) => err ? reject(err) : resolve(port))
})
return _.extend(inspectProcess(cmd, options, devtools), { devtools })
}

const inspectProcess = function (cmd, options, port, devtools) {
const inspectProcess = function (cmd, options, devtools) {
const getPathToCmd = function (cmd) {
try { return which.sync(cmd) } catch (e) { return path.resolve(cmd) }
}

return new Promise(function (resolve, reject) {
process.env['FORCE_COLOR'] = 1

const port = portfinderSync.getPort(9229)
const debugArgs = ['--inspect=' + port, '--debug-brk']
const nodeArgs = options['nodeArgs']
const childArgs = options['childArgs']
const debugArgs = ['--inspect=' + port, '--debug-brk']
const args = nodeArgs.concat(debugArgs, [getPathToCmd(cmd)], childArgs)
const proc = spawn('node', args)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"lodash": "^4.17.2",
"nodeflags": "^0.0.1",
"npmlog": "^4.0.2",
"portfinder": "^1.0.10",
"portfinder-sync": "0.0.2",
"selenium-webdriver": "^3.0.1",
"which": "^1.2.12",
"yargs": "^6.5.0"
Expand Down

0 comments on commit 958175e

Please sign in to comment.