node-loop-detective 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +47 -21
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -2,32 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
-
const { parseArgs } = require('node:util');
|
|
6
5
|
const { Detective } = require('../src/detective');
|
|
7
6
|
const { Reporter } = require('../src/reporter');
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
8
|
+
// Simple arg parser compatible with Node.js 16+
|
|
9
|
+
function parseCliArgs(argv) {
|
|
10
|
+
const args = argv.slice(2);
|
|
11
|
+
const values = {
|
|
12
|
+
pid: null,
|
|
13
|
+
port: null,
|
|
14
|
+
duration: '10',
|
|
15
|
+
threshold: '50',
|
|
16
|
+
interval: '100',
|
|
17
|
+
json: false,
|
|
18
|
+
watch: false,
|
|
19
|
+
help: false,
|
|
20
|
+
version: false,
|
|
21
|
+
};
|
|
22
|
+
const positionals = [];
|
|
23
|
+
|
|
24
|
+
const flagMap = {
|
|
25
|
+
'-p': 'pid', '--pid': 'pid',
|
|
26
|
+
'-P': 'port', '--port': 'port',
|
|
27
|
+
'-d': 'duration', '--duration': 'duration',
|
|
28
|
+
'-t': 'threshold', '--threshold': 'threshold',
|
|
29
|
+
'-i': 'interval', '--interval': 'interval',
|
|
30
|
+
};
|
|
31
|
+
const boolMap = {
|
|
32
|
+
'-j': 'json', '--json': 'json',
|
|
33
|
+
'-w': 'watch', '--watch': 'watch',
|
|
34
|
+
'-h': 'help', '--help': 'help',
|
|
35
|
+
'-v': 'version', '--version': 'version',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < args.length; i++) {
|
|
39
|
+
const arg = args[i];
|
|
40
|
+
if (flagMap[arg]) {
|
|
41
|
+
values[flagMap[arg]] = args[++i] || '';
|
|
42
|
+
} else if (boolMap[arg]) {
|
|
43
|
+
values[boolMap[arg]] = true;
|
|
44
|
+
} else if (!arg.startsWith('-')) {
|
|
45
|
+
positionals.push(arg);
|
|
46
|
+
} else {
|
|
47
|
+
console.error(`Unknown option: ${arg}\n`);
|
|
48
|
+
printUsage();
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return { values, positionals };
|
|
28
54
|
}
|
|
29
55
|
|
|
30
|
-
const { values, positionals } =
|
|
56
|
+
const { values, positionals } = parseCliArgs(process.argv);
|
|
31
57
|
|
|
32
58
|
if (values.version) {
|
|
33
59
|
const pkg = require('../package.json');
|