const findProcess = require('find-process')

module.exports = async function (min, max) {
  let port
  let processesOnPort

  do {
    port = _getRandomPort(min, max)
    processesOnPort = await findProcess('port', port)
  } while (processesOnPort.length !== 0)

  return port
}

function _getRandomPort(min, max) {
  return Math.floor(Math.random() * (max - min) + min)
}
