UNPKG

1.12 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/**
4 * @description - kill process by port
5 * @author - huang.jian <hjj491229492@hotmail.com>
6 */
7
8const program = require('commander');
9const chalk = require('chalk');
10const find = require('find-process');
11// internal
12const { version } = require('../package.json');
13// scope
14const terminal = chalk.cyan('athena:');
15
16function kill(pid, milliseconds) {
17 // kill process
18 process.kill(pid);
19
20 // preserve time span to ensure freedom
21 return new Promise((resolve) => setTimeout(resolve, milliseconds));
22}
23
24async function pipeline(port) {
25 try {
26 const matches = await find('port', port);
27 const success = await chalk.magenta(`Machine ${port} free success....`);
28
29 await Promise.all(matches.map((match) => kill(match.pid, 2500)));
30
31 /* eslint-disable no-console */
32 console.log(`${terminal} ${success}`);
33 /* eslint-enable no-console */
34 } catch (err) {
35 /* eslint-disable no-console */
36 console.error(err.stack);
37 /* eslint-disable no-console */
38 process.exit(1);
39 }
40}
41
42program
43 .version(version)
44 .usage('<port>')
45 .action(pipeline)
46 .parse(process.argv);