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

add support for killing by port (using a colon to indicate port usage) #27

Closed
wants to merge 5 commits into from

Conversation

dnbkr
Copy link
Contributor

@dnbkr dnbkr commented May 31, 2017

This is related to issue #19

@dnbkr dnbkr requested a review from sindresorhus May 31, 2017 14:20
@dnbkr
Copy link
Contributor Author

dnbkr commented May 31, 2017

The build is currently failing on the Node v4 check because the package i'm using to get the pid by port is importing like this: { methodName } = require('package') and Node v4 doesn't like that.

I am the author of the package, so can release a new version that supports older nodes, but wanted to check that you definitely want to keep supporting older versions of node?

cli.js Outdated
fkill(cli.input, cli.flags).catch(() => handleFkillError(cli.input));
let pids = [];
const promises = [];
for (let i = 0; i < cli.input.length; i++) {
Copy link
Contributor

@kevva kevva May 31, 2017

Choose a reason for hiding this comment

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

Just do for (const x of cli.input).

@kevva
Copy link
Contributor

kevva commented May 31, 2017

The build is currently failing on the Node v4 check because the package i'm using to get the pid by port is importing like this: { methodName } = require('package') and Node v4 doesn't like that.

Either we do a major here and remove Node.js v4 support, or if we want to add this feature for v4 users too, you have to bump your module to support it.

cli.js Outdated
let pids = [];
const promises = [];
for (let i = 0; i < cli.input.length; i++) {
if (cli.input[i][0] === ':') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Made a little regex for this https://github.com/kevva/port-regex :).

cli.js Outdated
const promises = [];
for (let i = 0; i < cli.input.length; i++) {
if (cli.input[i][0] === ':') {
promises.push(portToProc(parseInt(cli.input[i].slice(1), 0)));
Copy link
Contributor

Choose a reason for hiding this comment

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

0 is an invalid radix. Use 10 for decimal numbers.

cli.js Outdated
promisedPids = promisedPids.filter(pid => pid);
pids = pids.concat(promisedPids);
fkill(pids, cli.flags).catch(() => handleFkillError(cli.input));
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I would write it as:

if (cli.input.length === 0) {
    init(cli.flags);
} else {
    Promise.all(cli.input.map(x => x[0] === ':' ? portToProc(parseInt(x.slice(1), 10)) : x))
        .then(pids => fkill(pids, cli.flags).catch(() => handleFkillError(cli.input)));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Overall I prefer this approach; I'll update the branch

cli.js Outdated
}
}
Promise.all(promises).then(promisedPids => {
promisedPids = promisedPids.filter(pid => pid);
Copy link
Contributor

Choose a reason for hiding this comment

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

What are you filtering here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

portproc will return undefined if the input has no valid output (i.e. the port is not in use, or the pid is not using a port), so this filters out falsey responses from portproc

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it reject if it can't find a process?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure; I think if you ask to kill a valid target and an invalid target then the operation shouldn't fail; the valid target should be killed. But silently failing doesn't feel great, I agree. As a command line tool, rather than a library, perhaps it should be a little more permissive to user input? What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, your module should reject and we should handle it in here (i.e not failing), imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't rejecting a Promise like throwing an error? I'm not sure it's an error if you ask for the process using port X and there is no process using that port. That feels to me like it should return undefined.

If we want to treat undefined in this use case as an error, we can do - though I think i'd be more in favour or silently ignoring the command. At least if there's positive hits in the request too?

Am I making sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, maybe. I usually throw but I see where you coming from. Maybe @sindresorhus @SamVerschueren has any thoughts?

@dnbkr
Copy link
Contributor Author

dnbkr commented Jun 1, 2017

Either we do a major here and remove Node.js v4 support, or if we want to add this feature for v4 users too, you have to bump your module to support it.

Seeing as v4 is still on LTS until April next year, I have updated portproc to support v4; will update this PR accordingly

Promise.all(cli.input.map(x => x[0] === ':' ? portToProc(parseInt(x.slice(1), 10)) : x))
.then(pids => pids.filter(pid => pid))
.then(pids => fkill(pids, cli.flags))
.catch(() => handleFkillError(cli.input));
Copy link
Contributor

@kevva kevva Jun 1, 2017

Choose a reason for hiding this comment

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

This will swallow errors from portToProc which may be unintended here. We only want to handle errors from fkill.

@@ -8,6 +8,7 @@ const psList = require('ps-list');
const numSort = require('num-sort');
const escExit = require('esc-exit');
const cliTruncate = require('cli-truncate');
const portToProc = require('portproc').portToProc;
Copy link
Owner

Choose a reason for hiding this comment

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

Would have been more readable as:

const portToProcess = require('portproc').portToProcess;

Not really worth saving a few characters for limited gain.

Copy link
Owner

Choose a reason for hiding this comment

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

Can you extract the CLI in portproc into a separate package? I don't want to needlessly have commander in the dependency tree. See: sindresorhus/ama#17

@sindresorhus
Copy link
Owner

Great! Can you update the CLI help output to include support for the port stuff (readme too)? The interactive interface also needs support for ports.

@sindresorhus
Copy link
Owner

@coffeedoughnuts Still interested in finishing this up? :)

@dnbkr
Copy link
Contributor Author

dnbkr commented Sep 22, 2017 via email

@sindresorhus
Copy link
Owner

Closing to clean up stale PRs. Happy to reopen if you ever get to this :)

@sindresorhus sindresorhus deleted the feature/allow-searching-by-port-#19 branch December 16, 2017 11:27
@sindresorhus sindresorhus mentioned this pull request Feb 1, 2018
@dnbkr
Copy link
Contributor Author

dnbkr commented Mar 21, 2018

Hey

Just wanted to apologise for leaving this to go stale; I've been away from public code work for about a year while I moved jobs/cities/had a baby.

I've refactored my package portproc to no longer include the command line utility, and also refactored the module itself to be a little more resilient; however I notice you're using a different package for port-to-proc mapping now. If there's any need to switch back to portproc, give me a shout and I can PR to put it back in (though the new package looks like it does much the same task, so will likely be fine as-is)

Cheers!
Daniel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants