'use strict'; const detectPort = require("detect-port"); const killPort = require("kill-port"); let verbose = false; async function portClaim(port, _verbose = false) { if (!port) { throw new Error("Port is required"); } verbose = _verbose; logger(`Checking port ${port} is available...`); const isPortTaken = (await detectPort(port)).toString() !== port.toString(); if (isPortTaken) { logger(`Port ${port} is taken`); logger(`Killing port ${port}...`); await killPort(port); } else { logger(`Port ${port} is available`); } } function logger(message) { if (verbose) { console.log(message); } } module.exports = portClaim;