-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·105 lines (91 loc) · 1.99 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env node
'use strict';
/**
* Module dependencies.
*/
const portUtil = require('./dist/index.js');
const getThemArgs = require('get-them-args');
/**
* Parse command-line arguments.
*/
const args = getThemArgs(process.argv.slice(2));
/**
* Verbose mode for logging.
* @type {boolean}
*/
const verbose = args.verbose || false;
/**
* Ports to process, extracted from arguments.
* @type {Array<string>|string}
*/
let port = args.port ? args.port.toString().split(',') : args.unknown;
/**
* Method to use for processing (e.g., 'tcp').
* @type {string}
*/
const method = args.method || 'tcp';
/**
* Speed mode: 'fast' or 'safe'.
* @type {string}
*/
const speed = args.speed || args.fast ? 'fast' : 'safe';
/**
* Interactive mode toggle.
* @type {boolean}
*/
const interactive = args.interactive || false;
/**
* Dry-run mode toggle.
* @type {boolean}
*/
const dryRun = args.dryRun || false;
/**
* Graceful handling toggle.
* @type {boolean}
*/
const graceful = args.graceful || false;
/**
* Filter criteria for processing ports.
* @type {string}
*/
const filter = args.filter || '';
/**
* Range of ports for processing, if applicable.
* @type {string|null}
*/
const range = args.filter || null;
/**
* Action to perform: 'kill' or other specified action.
* @type {string}
*/
const action = args.kill ? 'kill' : args.action || 'check';
/**
* Ensure port is an array for consistent processing.
*/
if (!Array.isArray(port)) {
port = [port];
}
/**
* Process each port using portUtil with the specified options.
*/
Promise.all(
port.map((current) => {
return portUtil(current, {
method,
speed,
action,
interactive,
dryRun,
verbose,
graceful,
filter,
range
})
.then((result) => {
verbose && console.log(`Process on port ${action} ${current}`);
})
.catch((error) => {
verbose && console.log(`Could not process on port ${current}. ${error.message}.`);
});
})
);