localhost-port-check 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +52 -0
  2. package/index.js +105 -0
  3. package/package.json +25 -0
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # localhost-checker
2
+
3
+ CLI tool to check listening localhost ports on macOS.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install
9
+ npm link # optional: install globally as 'lh' command
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```bash
15
+ # Run directly
16
+ node index.js
17
+
18
+ # Or with npm
19
+ npm start
20
+
21
+ # If globally installed
22
+ lh
23
+ ```
24
+
25
+ ## Output
26
+
27
+ Displays a table of all listening ports with:
28
+ - **Port**: Port number (red for < 1024, green otherwise)
29
+ - **Address**: Bind address (127.0.0.1 = local only, * = all interfaces)
30
+ - **PID**: Process ID
31
+ - **Command**: Process name
32
+ - **User**: Running user
33
+
34
+ ## Example
35
+
36
+ ```
37
+ 🔍 Localhost Listening Ports
38
+
39
+ ┌───────┬───────────┬───────┬───────────┬───────┐
40
+ │ Port │ Address │ PID │ Command │ User │
41
+ ├───────┼───────────┼───────┼───────────┼───────┤
42
+ │ 3000 │ 127.0.0.1 │ 12345 │ node │ user │
43
+ ├───────┼───────────┼───────┼───────────┼───────┤
44
+ │ 3306 │ 127.0.0.1 │ 3946 │ mysqld │ user │
45
+ └───────┴───────────┴───────┴───────────┴───────┘
46
+
47
+ Total: 2 port(s) listening
48
+ ```
49
+
50
+ ## License
51
+
52
+ ISC
package/index.js ADDED
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { execSync } = require('child_process');
4
+ const chalk = require('chalk');
5
+ const Table = require('cli-table3');
6
+
7
+ function getListeningPorts() {
8
+ try {
9
+ // macOS: use lsof to get listening ports
10
+ const output = execSync(
11
+ 'lsof -iTCP -sTCP:LISTEN -P -n 2>/dev/null || true',
12
+ { encoding: 'utf-8' }
13
+ );
14
+
15
+ const lines = output.trim().split('\n').filter(Boolean);
16
+ if (lines.length <= 1) return [];
17
+
18
+ const ports = [];
19
+ const seen = new Set();
20
+
21
+ // Skip header line
22
+ for (let i = 1; i < lines.length; i++) {
23
+ const parts = lines[i].split(/\s+/);
24
+ if (parts.length < 9) continue;
25
+
26
+ const command = parts[0];
27
+ const pid = parts[1];
28
+ const user = parts[2];
29
+ const name = parts[8] || '';
30
+
31
+ // Extract port from address (e.g., "127.0.0.1:3000" or "*:8080")
32
+ const portMatch = name.match(/:(\d+)$/);
33
+ if (!portMatch) continue;
34
+
35
+ const port = portMatch[1];
36
+ const address = name.replace(`:${port}`, '') || '*';
37
+
38
+ // Deduplicate by pid+port
39
+ const key = `${pid}:${port}`;
40
+ if (seen.has(key)) continue;
41
+ seen.add(key);
42
+
43
+ ports.push({
44
+ port: parseInt(port),
45
+ address,
46
+ pid,
47
+ command,
48
+ user,
49
+ });
50
+ }
51
+
52
+ return ports.sort((a, b) => a.port - b.port);
53
+ } catch (error) {
54
+ console.error(chalk.red('Error getting listening ports:'), error.message);
55
+ return [];
56
+ }
57
+ }
58
+
59
+ function displayPorts(ports) {
60
+ console.log();
61
+ console.log(chalk.bold.cyan('🔍 Localhost Listening Ports'));
62
+ console.log();
63
+
64
+ if (ports.length === 0) {
65
+ console.log(chalk.yellow(' No listening ports found.'));
66
+ console.log();
67
+ return;
68
+ }
69
+
70
+ const table = new Table({
71
+ head: [
72
+ chalk.white.bold('Port'),
73
+ chalk.white.bold('Address'),
74
+ chalk.white.bold('PID'),
75
+ chalk.white.bold('Command'),
76
+ chalk.white.bold('User'),
77
+ ],
78
+ style: {
79
+ head: [],
80
+ border: ['gray'],
81
+ },
82
+ });
83
+
84
+ for (const p of ports) {
85
+ const portColor = p.port < 1024 ? chalk.red : chalk.green;
86
+ const isLocalhost = p.address === '127.0.0.1' || p.address === 'localhost';
87
+
88
+ table.push([
89
+ portColor.bold(p.port.toString()),
90
+ isLocalhost ? chalk.blue(p.address) : chalk.yellow(p.address),
91
+ chalk.gray(p.pid),
92
+ chalk.white(p.command),
93
+ chalk.gray(p.user),
94
+ ]);
95
+ }
96
+
97
+ console.log(table.toString());
98
+ console.log();
99
+ console.log(chalk.gray(` Total: ${ports.length} port(s) listening`));
100
+ console.log();
101
+ }
102
+
103
+ // Main
104
+ const ports = getListeningPorts();
105
+ displayPorts(ports);
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "localhost-port-check",
3
+ "version": "1.0.0",
4
+ "description": "CLI tool to check listening localhost ports on macOS",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "lh": "./index.js",
8
+ "localhost-port-check": "./index.js"
9
+ },
10
+ "scripts": {
11
+ "start": "node index.js"
12
+ },
13
+ "keywords": ["localhost", "ports", "cli", "macos", "lsof"],
14
+ "author": "HizumeKazushi",
15
+ "license": "ISC",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/HizumeKazushi/localhost_check.git"
19
+ },
20
+ "type": "commonjs",
21
+ "dependencies": {
22
+ "chalk": "^4.1.2",
23
+ "cli-table3": "^0.6.5"
24
+ }
25
+ }