diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8cf4926..75bf22a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,10 @@ jobs: matrix: node-version: [16.x, 18.x, 20.x, 21.x, 22.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 steps: - uses: actions/checkout@v3 diff --git a/.mocharc.yml b/.mocharc.yml index f8726ea..f945fba 100644 --- a/.mocharc.yml +++ b/.mocharc.yml @@ -1,3 +1,4 @@ timeout: 4000 require: - ts-node/register +full-trace: true diff --git a/README.md b/README.md index 23348fc..91f96a6 100644 --- a/README.md +++ b/README.md @@ -51,21 +51,23 @@ npm install kill-port-process -g You can use the CLI calling it with `kill-port `. -It takes a single port or a list of ports separated by a space. Valid flags are `-p` and `--port` but are both optional. - ```bash +# single port $ kill-port 1234 -# or multiple ports, separated by space(s) +# multiple ports, separated by space(s) $ kill-port 1234 2345 -# or +# with "-p" flag $ kill-port -p 1234 -# or +# with with "--port" flag $ kill-port --port 1234 +# with graceful flag +$ kill-port 1234 --graceful ``` #### Flags -* `--graceful` kill the process gracefully. +* `-p` or `--port` (optional): used to specify the port(s) to kill. Takes a single port or a list of ports separated by a space. +* `--graceful` (optional) kill the process gracefully. * **Unix:** Sends a `-15` signal to kill (`SIGTERM`) rather than `-9` (`SIGKILL`) * **Win:** Currently no use diff --git a/package-lock.json b/package-lock.json index 67a4e23..6b88800 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "kill-port-process", - "version": "3.2.1", + "version": "4.0.0-beta.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "kill-port-process", - "version": "3.2.1", + "version": "4.0.0-beta.0", "license": "ISC", "dependencies": { "get-them-args": "1.3.2", diff --git a/package.json b/package.json index 8bd8387..72de3d6 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/bin/kill-port-process.ts b/src/bin/kill-port-process.ts index 4fa9085..05d1bc4 100644 --- a/src/bin/kill-port-process.ts +++ b/src/bin/kill-port-process.ts @@ -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); })(); diff --git a/src/lib/killer.ts b/src/lib/killer.ts index addcadd..5b6bd50 100644 --- a/src/lib/killer.ts +++ b/src/lib/killer.ts @@ -9,7 +9,7 @@ type KillOptions = { } export class Killer { - protected ports: number[]; + private readonly ports: number[]; constructor(ports: number[]) { this.ports = ports; @@ -17,12 +17,13 @@ export class Killer { 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) { @@ -67,14 +68,32 @@ 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); } resolve(undefined); }); + /** + * @see https://www.commandlinux.com/man-page/man1/xargs.1.html for possible exit codes + */ + function handleErrorCode(code: number | null) { + if (!code) { + return null; + } + + 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()}`); }