Skip to content

Commit

Permalink
Require Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 12, 2020
1 parent 45e1e1d commit f4ea719
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
4 changes: 2 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const cli = meow(`
To kill a port, prefix it with a colon. For example: :8080.
Run without arguments to use the interactive mode.
In interactive mode, 🚦A.B% indicates high CPU usage, 🐏C.D% indicates high memory usage.
In interactive mode, 🚦n% indicates high CPU usage and 🐏n% indicates high memory usage.
The process name is case insensitive.
`, {
Expand Down Expand Up @@ -54,7 +54,7 @@ if (cli.input.length === 0) {
return;
}

if (/Couldn't find a process with port/.test(error.message)) {
if (error.message.includes('Couldn\'t find a process with port')) {
console.error(error.message);
process.exit(1);
}
Expand Down
52 changes: 27 additions & 25 deletions interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ const cliTruncate = require('cli-truncate');
const pidFromPort = require('pid-from-port');
const fkill = require('fkill');

const isWindows = process.platform === 'win32';
const commandLineMargins = 4;

const nameFilter = (input, proc) => {
const nameFilter = (input, process_) => {
const isPort = input[0] === ':';

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

return proc.name.toLowerCase().includes(input.toLowerCase());
return process_.name.toLowerCase().includes(input.toLowerCase());
};

const preferNotMatching = matches => (a, b) => {
Expand All @@ -26,9 +27,9 @@ const preferNotMatching = matches => (a, b) => {
};

const deprioritizedProcesses = new Set(['iTerm', 'iTerm2', 'fkill']);
const isDeprioritizedProcess = proc => deprioritizedProcesses.has(proc.name);
const isDeprioritizedProcess = process_ => deprioritizedProcesses.has(process_.name);
const preferNotDeprioritized = preferNotMatching(isDeprioritizedProcess);
const preferHighPerformanceImpact = (a, b) => numSort.desc(a.cpu + a.memory, b.cpu + b.memory);
const preferHighPerformanceImpact = (a, b) => numSort.descending(a.cpu + a.memory, b.cpu + b.memory);
const preferLowAlphanumericNames = (a, b) => a.name.localeCompare(b.name);

const preferHeurisicallyInterestingProcesses = (a, b) => {
Expand All @@ -49,41 +50,42 @@ const preferHeurisicallyInterestingProcesses = (a, b) => {

const filterProcesses = (input, processes, flags) => {
const filters = {
name: proc => input ? nameFilter(input, proc) : true,
verbose: proc => input ? (process.platform === 'win32' ? proc.name : proc.cmd).toLowerCase().includes(input.toLowerCase()) : true
name: process_ => input ? nameFilter(input, process_) : true,
verbose: process_ => input ? (isWindows ? process_.name : process_.cmd).toLowerCase().includes(input.toLowerCase()) : true
};

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

return processes
.filter(proc => !(
proc.name.endsWith('-helper') ||
proc.name.endsWith('Helper') ||
proc.name.endsWith('HelperApp')
.filter(process_ => !(
process_.name.endsWith('-helper') ||
process_.name.endsWith('Helper') ||
process_.name.endsWith('HelperApp')
))
.filter(flags.verbose ? filters.verbose : filters.name)
.sort(preferHeurisicallyInterestingProcesses)
.map(proc => {
.map(process_ => {
const renderPercentage = percents => {
const digits = Math.floor(percents * 10).toString().padStart(2, '0');
const whole = digits.substr(0, digits.length - 1);
const fraction = digits.substr(digits.length - 1);
const whole = digits.slice(0, digits.length - 1);
const fraction = digits.slice(digits.length - 1);
return fraction === '0' ? `${whole}%` : `${whole}.${fraction}%`;
};

const lineLength = process.stdout.columns || 80;
const ports = proc.ports.length === 0 ? '' : (' ' + proc.ports.slice(0, 4).map(x => `:${x}`).join(' '));
const memory = (proc.memory !== undefined && (proc.memory > memoryThreshold)) ? ` 🐏${renderPercentage(proc.memory)}` : '';
const cpu = (proc.cpu !== undefined && (proc.cpu > cpuThreshold)) ? `🚦${renderPercentage(proc.cpu)}` : '';
const margins = commandLineMargins + proc.pid.toString().length + ports.length + memory.length + cpu.length;
const ports = process_.ports.length === 0 ? '' : (' ' + process_.ports.slice(0, 4).map(x => `:${x}`).join(' '));
const memory = (process_.memory !== undefined && (process_.memory > memoryThreshold)) ? ` 🐏${renderPercentage(process_.memory)}` : '';
const cpu = (process_.cpu !== undefined && (process_.cpu > cpuThreshold)) ? `🚦${renderPercentage(process_.cpu)}` : '';
const margins = commandLineMargins + process_.pid.toString().length + ports.length + memory.length + cpu.length;
const length = lineLength - margins;
const name = cliTruncate(flags.verbose && process.platform !== 'win32' ? proc.cmd : proc.name, length, {position: 'middle'});
const spacer = lineLength === process.stdout.columns ? ''.padEnd(length - name.length) : '';
const name = cliTruncate(flags.verbose && !isWindows ? process_.cmd : process_.name, length, {position: 'middle', preferTruncationOnSpace: true});
const extraMargin = 2;
const spacer = lineLength === process.stdout.columns ? ''.padEnd(length - name.length - extraMargin) : '';

return {
name: `${name} ${chalk.dim(proc.pid)}${spacer}${chalk.dim(ports)}${cpu}${memory}`,
value: proc.pid
name: `${name} ${chalk.dim(process_.pid)}${spacer}${chalk.dim(ports)}${cpu}${memory}`,
value: process_.pid
};
});
};
Expand Down Expand Up @@ -148,7 +150,7 @@ const init = async flags => {
psList({all: false})
]);

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

Expand Down
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Fabulously kill processes. Cross-platform.",
"license": "MIT",
"repository": "sindresorhus/fkill-cli",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
Expand All @@ -13,7 +14,7 @@
"fkill": "cli.js"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -40,24 +41,24 @@
"proc"
],
"dependencies": {
"chalk": "^2.4.2",
"cli-truncate": "^1.0.0",
"chalk": "^3.0.0",
"cli-truncate": "^2.1.0",
"esc-exit": "^2.0.0",
"fkill": "^6.0.0",
"inquirer": "^6.2.2",
"fkill": "^7.0.0",
"inquirer": "^7.0.4",
"inquirer-autocomplete-prompt": "^1.0.1",
"meow": "^5.0.0",
"num-sort": "^1.0.0",
"meow": "^6.0.0",
"num-sort": "^2.0.0",
"pid-from-port": "^1.1.1",
"ps-list": "^6.2.0"
},
"devDependencies": {
"ava": "^1.4.0",
"delay": "^4.1.0",
"execa": "^1.0.0",
"get-port": "^4.2.0",
"noop-process": "^3.1.0",
"process-exists": "^3.1.0",
"xo": "^0.24.0"
"execa": "^4.0.0",
"get-port": "^5.1.1",
"noop-process": "^4.0.0",
"process-exists": "^4.0.0",
"xo": "^0.25.4"
}
}
9 changes: 3 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@

Works on macOS, Linux, and Windows.


## Install

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


## Usage

```
Expand All @@ -43,24 +41,23 @@ $ fkill --help
To kill a port, prefix it with a colon. For example: :8080.
Run without arguments to use the interactive interface.
In interactive mode, 🚦n% indicates high CPU usage and 🐏n% indicates high memory usage.
The process name is case insensitive.
```


## Interactive UI

Run `fkill` without arguments to launch the interactive UI.

![](screenshot.svg)


## Related

- [fkill](https://github.com/sindresorhus/fkill) - API for this module
- [alfred-fkill](https://github.com/SamVerschueren/alfred-fkill) - Alfred workflow for this module


## Created by
## Maintainers

- [Sindre Sorhus](https://sindresorhus.com)
- [Daniel Baker](https://github.com/coffeedoughnuts)
1 change: 0 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test('kill from port', async t => {
const {pid} = childProcess.spawn('node', ['fixture.js', port]);
await execa('./cli.js', ['--force', pid]);
await noopProcessKilled(t, pid);
t.is(await getPort({port}), port);
});

test('error when process is not found', async t => {
Expand Down

0 comments on commit f4ea719

Please sign in to comment.