UNPKG

1.23 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 welcome = require('../lib/welcome');
13const { version } = require('../package.json');
14// scope
15const terminal = chalk.cyan('athena:');
16
17function kill(pid, milliseconds) {
18 // kill process
19 process.kill(pid);
20
21 // preserve time span to ensure freedom
22 return new Promise((resolve) => setTimeout(resolve, milliseconds));
23}
24
25async function pipeline(port) {
26 welcome();
27
28 try {
29 const matches = find('port', port);
30 const success = chalk.magenta(`machine ${port} free success....`);
31
32 await Promise.all(matches.map((match) => kill(match.pid, 2500)));
33
34 /* eslint-disable no-console */
35 console.log(`${terminal} ${success}`);
36 /* eslint-enable no-console */
37 } catch (err) {
38 /* eslint-disable no-console */
39 console.log(
40 `${terminal} ${chalk.red(`kill pipeline failed, ${err.message.trim()}`)}`
41 );
42 /* eslint-enable no-console */
43 process.exit(6);
44 }
45}
46
47program
48 .version(version)
49 .usage('<port>')
50 .action(pipeline)
51 .parse(process.argv);