Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 14, 2021
1 parent a99e7f1 commit 4a33ba2
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 92 deletions.
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 16
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
59 changes: 33 additions & 26 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const fkill = require('fkill');
import process from 'node:process';
import meow from 'meow';
import fkill from 'fkill';

const cli = meow(`
Usage
Expand All @@ -27,45 +27,52 @@ const cli = meow(`
The process name is case insensitive.
`, {
importMeta: import.meta,
inferType: true,
flags: {
force: {
type: 'boolean',
alias: 'f'
alias: 'f',
},
verbose: {
type: 'boolean',
alias: 'v'
alias: 'v',
},
silent: {
type: 'boolean',
alias: 's'
alias: 's',
},
forceAfterTimeout: {
type: 'number',
alias: 't'
}
}
alias: 't',
},
},
});

if (cli.input.length === 0) {
require('./interactive').init(cli.flags);
} else {
const forceAfterTimeout = cli.flags.forceAfterTimeout === undefined ? undefined : cli.flags.forceAfterTimeout * 1000;
const promise = fkill(cli.input, {...cli.flags, forceAfterTimeout, ignoreCase: true});
(async () => {
if (cli.input.length === 0) {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
(await import('./interactive.js')).init(cli.flags);
} else {
const forceAfterTimeout = cli.flags.forceAfterTimeout === undefined ? undefined : cli.flags.forceAfterTimeout * 1000;
const promise = fkill(cli.input, {...cli.flags, forceAfterTimeout, ignoreCase: true});

if (!cli.flags.force) {
promise.catch(error => {
if (cli.flags.silent) {
return;
}
if (!cli.flags.force) {
try {
await promise;
} catch (error) {
if (cli.flags.silent) {
return;
}

if (error.message.includes('Couldn\'t find a process with port')) {
console.error(error.message);
process.exit(1);
}
if (error.message.includes('Could not find a process with port')) {
console.error(error.message);
process.exit(1);
}

return require('./interactive').handleFkillError(cli.input);
});
// eslint-disable-next-line node/no-unsupported-features/es-syntax
(await import('./interactive.js')).handleFkillError(cli.input);
}
}
}
}
})();
4 changes: 2 additions & 2 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const http = require('http');
import process from 'node:process';
import http from 'node:http';

const server = http.createServer((request, response) => {
response.end();
Expand Down
59 changes: 30 additions & 29 deletions interactive.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';
const chalk = require('chalk');
const inquirer = require('inquirer');
const psList = require('ps-list');
const numSort = require('num-sort');
const escExit = require('esc-exit');
const cliTruncate = require('cli-truncate');
const pidFromPort = require('pid-port');
const fkill = require('fkill');
const processExists = require('process-exists');
import process from 'node:process';
import chalk from 'chalk';
import inquirer from 'inquirer';
import inquirerAutocompletePrompt from 'inquirer-autocomplete-prompt';
import psList from 'ps-list';
import {numberSortDescending} from 'num-sort';
import escExit from 'esc-exit';
import cliTruncate from 'cli-truncate';
import {allPortsWithPid} from 'pid-port';
import fkill from 'fkill';
import processExists from 'process-exists';

const isWindows = process.platform === 'win32';
const commandLineMargins = 4;
Expand Down Expand Up @@ -46,7 +47,7 @@ const nameFilter = (input, process_) => {
const isPort = input[0] === ':';

if (isPort) {
return process_.ports.find(x => x.startsWith(input.slice(1)));
return process_.ports.find(port => port.startsWith(input.slice(1)));
}

return process_.name.toLowerCase().includes(input.toLowerCase());
Expand All @@ -67,15 +68,15 @@ const preferHighPerformanceImpact = (a, b) => {
const hasMemory = typeof a.memory === 'number' && typeof b.memory === 'number';

if (hasCpu && hasMemory) {
return numSort.descending(a.cpu + a.memory, b.cpu + b.memory);
return numberSortDescending(a.cpu + a.memory, b.cpu + b.memory);
}

if (hasCpu) {
return numSort.descending(a.cpu, b.cpu);
return numberSortDescending(a.cpu, b.cpu);
}

if (hasMemory) {
return numSort.descending(a.memory, b.memory);
return numberSortDescending(a.memory, b.memory);
}

return 0;
Expand All @@ -100,19 +101,19 @@ const preferHeurisicallyInterestingProcesses = (a, b) => {
const filterProcesses = (input, processes, flags) => {
const filters = {
name: process_ => input ? nameFilter(input, process_) : true,
verbose: process_ => input ? (isWindows ? process_.name : process_.cmd).toLowerCase().includes(input.toLowerCase()) : true
verbose: process_ => input ? (isWindows ? process_.name : process_.cmd).toLowerCase().includes(input.toLowerCase()) : true,
};

const memoryThreshold = flags.verbose ? 0 : 1;
const cpuThreshold = flags.verbose ? 0 : 3;

return processes
.filter(process_ => !(
process_.name.endsWith('-helper') ||
process_.name.endsWith('Helper') ||
process_.name.endsWith('HelperApp')
process_.name.endsWith('-helper')
|| process_.name.endsWith('Helper')
|| process_.name.endsWith('HelperApp')
))
// eslint-disable-next-line unicorn/no-fn-reference-in-iterator
// eslint-disable-next-line unicorn/no-array-callback-reference
.filter(flags.verbose ? filters.verbose : filters.name)
.sort(preferHeurisicallyInterestingProcesses)
.map(process_ => {
Expand All @@ -135,7 +136,7 @@ const filterProcesses = (input, processes, flags) => {

return {
name: `${name} ${chalk.dim(process_.pid)}${spacer}${chalk.dim(ports)}${cpu}${memory}`,
value: process_.pid
value: process_.pid,
};
});
};
Expand All @@ -150,13 +151,13 @@ const handleFkillError = async processes => {
const answer = await inquirer.prompt([{
type: 'confirm',
name: 'forceKill',
message: 'Error killing process. Would you like to use the force?'
message: 'Error killing process. Would you like to use the force?',
}]);

if (answer.forceKill === true) {
await fkill(processes, {
force: true,
ignoreCase: true
ignoreCase: true,
});
}
}
Expand Down Expand Up @@ -195,7 +196,7 @@ const performKillSequence = async processes => {
const answer = await inquirer.prompt([{
type: 'confirm',
name: 'forceKill',
message: `${problemText} Would you like to use the force?`
message: `${problemText} Would you like to use the force?`,
}]);

if (!answer.forceKill) {
Expand All @@ -204,19 +205,19 @@ const performKillSequence = async processes => {

await fkill(processes, {
force: true,
ignoreCase: true
ignoreCase: true,
});
};

const listProcesses = async (processes, flags) => {
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
inquirer.registerPrompt('autocomplete', inquirerAutocompletePrompt);

const answer = await inquirer.prompt([{
name: 'processes',
message: 'Running processes:',
type: 'autocomplete',
pageSize: 10,
source: async (answers, input) => filterProcesses(input, processes, flags)
source: async (answers, input) => filterProcesses(input, processes, flags),
}]);

performKillSequence(answer.processes);
Expand All @@ -238,12 +239,12 @@ const init = async flags => {
};

const [pids, processes] = await Promise.all([
pidFromPort.all(),
psList({all: false})
allPortsWithPid(),
psList({all: false}),
]);

const procs = processes.map(process_ => ({...process_, ports: getPortsFromPid(process_.pid, pids)}));
listProcesses(procs, flags);
};

module.exports = {init, handleFkillError};
export {init, handleFkillError};
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
41 changes: 20 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
"description": "Fabulously kill processes. Cross-platform.",
"license": "MIT",
"repository": "sindresorhus/fkill-cli",
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
},
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"bin": {
"fkill": "cli.js"
},
"engines": {
"node": ">=10"
"node": "^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -43,24 +42,24 @@
"proc"
],
"dependencies": {
"chalk": "^4.1.0",
"cli-truncate": "^2.1.0",
"esc-exit": "^2.0.2",
"fkill": "^7.2.1",
"inquirer": "^7.3.3",
"inquirer-autocomplete-prompt": "^1.3.0",
"meow": "^8.1.0",
"num-sort": "^2.1.0",
"pid-port": "^0.1.0",
"chalk": "^4.1.2",
"cli-truncate": "^3.1.0",
"esc-exit": "^3.0.0",
"fkill": "^8.0.0",
"inquirer": "^8.2.0",
"inquirer-autocomplete-prompt": "^1.4.0",
"meow": "^10.1.1",
"num-sort": "^3.0.0",
"pid-port": "^0.2.0",
"ps-list": "^7.2.0"
},
"devDependencies": {
"ava": "^2.4.0",
"delay": "^4.4.0",
"execa": "^5.0.0",
"get-port": "^5.1.1",
"noop-process": "^4.0.0",
"process-exists": "^4.0.0",
"xo": "^0.36.1"
"ava": "^3.15.0",
"delay": "^5.0.0",
"execa": "^5.1.1",
"get-port": "^6.0.0",
"noop-process": "^5.0.0",
"process-exists": "^4.1.0",
"xo": "^0.45.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Works on macOS, Linux, and Windows.

## Install

```
$ npm install --global fkill-cli
```sh
npm install --global fkill-cli
```

## Usage
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import childProcess from 'child_process';
import childProcess from 'node:child_process';
import test from 'ava';
import execa from 'execa';
import delay from 'delay';
Expand Down Expand Up @@ -33,14 +33,14 @@ test('kill from port', async t => {
test('error when process is not found', async t => {
await t.throwsAsync(
execa('./cli.js', ['--force', 'notFoundProcess']),
/Killing process notFoundProcess failed: Process doesn't exist/
{message: /Killing process notFoundProcess failed: Process doesn't exist/},
);
});

test('force killing process at unused port throws error', async t => {
await t.throwsAsync(
execa('./cli.js', ['--force', ':1337']),
/Killing process :1337 failed: Process doesn't exist/
{message: /Killing process :1337 failed: Process doesn't exist/},
);
});

Expand Down

0 comments on commit 4a33ba2

Please sign in to comment.