import { execSync, ExecSyncOptions } from "child_process";

type Params = {
    port: string | number;
    exec_options?: ExecSyncOptions;
};

export default async function killPort({ port, exec_options }: Params) {
    const kill_ports_cmd = `ss -tlnp | awk -v p=${port} 'NR>1 {split($4,a,":"); if(a[length(a)]==p) print $6}' | grep -oP 'pid=\\K[0-9]+' | xargs -r kill -9`;

    execSync(kill_ports_cmd, {
        ...exec_options,
    });
}
