-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kill, getPIDs): find and kill processes by ports and PIDs (#247)
* feat(kill): terminate ports and PIDs * chore: expose all process methods * chore(cli): add `--kill-port` * chore(cli): add `--kill-pid` * chore: remove silient throw * chore: allow multiple ports and PIDs * ci: add test when no port is passed to end method * chore: adapt `--kill-port` and `--kill-pid` * docs: improve README.md * chore: fix CLI PIDs typeof * docs: improve README.md * docs: add processes documentation * chore: include tea * chore: add range feature
- Loading branch information
1 parent
0e81815
commit 13305cf
Showing
18 changed files
with
580 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* c8 ignore start */ | ||
|
||
import { isWindows } from '../helpers/runner.js'; | ||
import { | ||
getPIDs as getPIDsService, | ||
killPID as killPIDService, | ||
populateRange, | ||
setPortsAndPIDs, | ||
} from '../services/pid.js'; | ||
|
||
const getPIDsByPorts = async (port: number | number[]): Promise<number[]> => { | ||
const ports = setPortsAndPIDs(port); | ||
const PIDs: number[] = []; | ||
|
||
await Promise.all( | ||
ports.map(async (p) => { | ||
PIDs.push( | ||
...(await (isWindows | ||
? getPIDsService.windows(p) | ||
: getPIDsService.unix(p))) | ||
); | ||
}) | ||
); | ||
|
||
return PIDs; | ||
}; | ||
|
||
const getPIDsByRange = async ( | ||
startsAt: number, | ||
endsAt: number | ||
): Promise<number[]> => { | ||
const ports = populateRange(startsAt, endsAt); | ||
|
||
return await getPIDs(ports); | ||
}; | ||
|
||
/** | ||
* Returns an array containing the ID of all processes listening to the specified port | ||
*/ | ||
export const getPIDs = Object.assign(getPIDsByPorts, { | ||
/** | ||
* Returns an array containing the ID of all processes listening to the specified range port | ||
*/ | ||
range: getPIDsByRange, | ||
}); | ||
|
||
const killPID = async (PID: number | number[]): Promise<void> => { | ||
const PIDs = setPortsAndPIDs(PID); | ||
|
||
await Promise.all( | ||
PIDs.map(async (p) => { | ||
isWindows | ||
? await killPIDService.windows(p) | ||
: await killPIDService.unix(p); | ||
}) | ||
); | ||
}; | ||
|
||
const killPort = async (port: number | number[]): Promise<void> => { | ||
const PIDs = await getPIDs(port); | ||
|
||
for (const PID of PIDs) { | ||
if (!PID) continue; | ||
|
||
await killPID(PID); | ||
} | ||
}; | ||
|
||
const killRange = async (startsAt: number, endsAt: number): Promise<void> => { | ||
const PIDs = await getPIDs.range(startsAt, endsAt); | ||
|
||
for (const PID of PIDs) { | ||
if (!PID) continue; | ||
|
||
await killPID(PID); | ||
} | ||
}; | ||
|
||
export const kill = { | ||
/** | ||
* Terminates the specified process ID | ||
*/ | ||
pid: killPID, | ||
/** | ||
* Terminates all processes listening on the specified port | ||
*/ | ||
port: killPort, | ||
/** | ||
* Terminates all processes listening on the specified range ports | ||
*/ | ||
range: killRange, | ||
}; | ||
|
||
/* c8 ignore stop */ |
Oops, something went wrong.