import { promisify } from 'util'; import * as treeKill from 'tree-kill'; import { logError, logInfo, logSuccess } from './log-utils'; import { check as portCheck } from 'tcp-port-used'; export const kill = require('kill-port'); const KILL_PORT_DELAY = 5000; export const promisifiedTreeKill: ( pid: number, signal: string ) => Promise = promisify(treeKill); export async function killPort(port: number): Promise { if (await portCheck(port)) { try { logInfo(`Attempting to close port ${port}`); await kill(port); await new Promise((resolve) => setTimeout(() => resolve(), KILL_PORT_DELAY) ); if (await portCheck(port)) { logError(`Port ${port} still open`); } else { logSuccess(`Port ${port} successfully closed`); return true; } } catch { logError(`Port ${port} closing failed`); } return false; } else { return true; } } export async function killPorts(port?: number): Promise { return port ? await killPort(port) : (await killPort(3333)) && (await killPort(4200)); }