Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: unix-kill resolve on exit. add error listener #103

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x, 21.x]
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x, 21.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
os: [macos-latest, ubuntu-latest, windows-latest]
os:
- macos-latest
- ubuntu-latest
- windows-latest
-
hilleer marked this conversation as resolved.
Show resolved Hide resolved

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
timeout: 4000
require:
- ts-node/register
full-trace: true
120 changes: 2 additions & 118 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kill-port-process",
"version": "3.2.1",
"version": "4.0.0-beta.0",
"description": "Easily kill hanging processes on ports - on any platform!",
"main": "dist/lib/index.js",
"bin": {
Expand All @@ -11,11 +11,11 @@
"build": "tsc",
"eslint": "eslint . --ext .ts",
"lint": "npm run eslint",
"git-tags": "git push && git push --tags",
"test": "mocha test/*.spec.ts",
"pretest": "npm run build",
"prepare": "npm run build && npm test",
"preversion": "npm run build && npm test",
"postversion": "git push && git push --tags"
"preversion": "npm run build && npm test"
},
"keywords": [
"process",
Expand Down
3 changes: 3 additions & 0 deletions src/bin/kill-port-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { killPortProcess, Options } from '../lib/index';
const flags = parseFlagsFromArgs(args);
const options = formatOptions(flags);

console.log('flags::', flags);
console.log('options::', options);

await killPortProcess(ports, options);
})();

Expand Down
27 changes: 22 additions & 5 deletions src/lib/killer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ type KillOptions = {
}

export class Killer {
protected ports: number[];
private readonly ports: number[];

constructor(ports: number[]) {
this.ports = ports;
}

public async kill(options: KillOptions) {
const killFunc = platform() === 'win32' ? this.win32Kill : this.unixKill;

const promises = this.ports.map((port) => killFunc(port, options.signal));

return Promise.all(promises);
}

private async win32Kill(port: number, signal: Signal) {
private async win32Kill(port: number) {
const pid = await pidFromPort(port).catch((error) => console.error('Failed to get pid of port', port, error));

if (!pid) {
Expand Down Expand Up @@ -67,14 +68,30 @@ export class Killer {

xargs.stdout.pipe(process.stdin);
xargs.stderr.on('data', logStderrData('xargs'));
xargs.on('close', (code) => {
if (code !== 0) {
return reject();

xargs.on('exit', (code) => {
const error = handleErrorCode(code);
if (error) {
reject(error);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reject(error);
return reject(error);

}

resolve(undefined);
});

function handleErrorCode(code: number | null) {
if (!code) {
return null;
}

// see possible exit codes: https://www.commandlinux.com/man-page/man1/xargs.1.html
switch (code) {
case 1:
return new Error(`xargs process exited with code ${code}`)
case 127:
return new Error('xargs command not found');
}
}

function logStderrData(command: string) {
return (data: any) => console.error(`${command} - ${data.toString()}`);
}
Expand Down
Loading