portdash 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Varsha Konda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # portdash
2
+
3
+ Scan localhost ports and show a dashboard of running dev servers (zero dependencies).
4
+
5
+ ## Install / Run
6
+
7
+ ```bash
8
+ npx portdash --open
9
+
10
+ # or
11
+ npm install -g portdash
12
+ portdash --open
13
+ ```
14
+
15
+ Node.js 18+.
16
+
17
+ ## Usage
18
+
19
+ ```bash
20
+ portdash # dashboard at http://127.0.0.1:4789
21
+ portdash --once # print and exit
22
+ portdash --once --json # JSON output
23
+ portdash 3000,5173,8080
24
+ portdash 3000-5000
25
+ ```
26
+
27
+ All options: `portdash --help`.
28
+
29
+ ## Development
30
+
31
+ ```bash
32
+ npm test
33
+ node src/portdash.mjs
34
+ ```
35
+
36
+ ## License
37
+
38
+ MIT
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "portdash",
3
+ "version": "0.1.0",
4
+ "description": "Localhost dev server dashboard",
5
+ "type": "module",
6
+ "bin": {
7
+ "portdash": "src/portdash.mjs"
8
+ },
9
+ "files": [
10
+ "src/portdash.mjs"
11
+ ],
12
+ "scripts": {
13
+ "start": "node src/portdash.mjs",
14
+ "test": "node --test test/portdash.test.mjs",
15
+ "prepublishOnly": "npm test"
16
+ },
17
+ "keywords": [
18
+ "dev",
19
+ "localhost",
20
+ "ports",
21
+ "dashboard",
22
+ "cli",
23
+ "vite",
24
+ "nextjs",
25
+ "storybook",
26
+ "development"
27
+ ],
28
+ "author": "Varsha Konda",
29
+ "license": "MIT",
30
+ "engines": {
31
+ "node": ">=18"
32
+ }
33
+ }
@@ -0,0 +1,635 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { createServer, request as httpRequest } from 'node:http';
4
+ import { request as httpsRequest } from 'node:https';
5
+ import { Socket } from 'node:net';
6
+ import { exec } from 'node:child_process';
7
+ import { parseArgs as parseCli } from 'node:util';
8
+
9
+ const DEFAULT_BIND_HOST = '127.0.0.1';
10
+ const DEFAULT_DASHBOARD_PORT = 4789;
11
+ const DEFAULT_TIMEOUT_MS = 700;
12
+ const DEFAULT_CONCURRENCY = 128;
13
+ const DEFAULT_RANGE = '3000-9999';
14
+ const SCAN_HOST = '127.0.0.1';
15
+
16
+ // ============================================================================
17
+ // CLI Argument Parsing
18
+ // ============================================================================
19
+
20
+ function showHelp() {
21
+ console.log(`portdash - localhost dev server dashboard
22
+
23
+ Usage:
24
+ portdash [targets] [options]
25
+
26
+ Targets (optional):
27
+ 3000 5173 Scan specific ports
28
+ 3000,5173,8080 Scan specific ports
29
+ 3000-5000 Scan a range (default: ${DEFAULT_RANGE})
30
+
31
+ Options:
32
+ --open, -o Open browser after starting
33
+ --once, -1 Print results and exit
34
+ --json, -j JSON output (implies --once)
35
+ --bind <host> Dashboard bind host (default: ${DEFAULT_BIND_HOST})
36
+ --port <port> Dashboard port (default: ${DEFAULT_DASHBOARD_PORT})
37
+ --ports <list> Comma-separated ports to scan (e.g., "3000,5173,6006")
38
+ --range <a-b> Port range to scan (default: ${DEFAULT_RANGE})
39
+ --timeout <ms> Per-port probe timeout (default: ${DEFAULT_TIMEOUT_MS})
40
+ --concurrency <n> Max concurrent probes (default: ${DEFAULT_CONCURRENCY})
41
+ --no-title Skip title fetching (faster)
42
+ --no-https Disable HTTPS fallback probing
43
+ --help, -h Show this help
44
+
45
+ Examples:
46
+ portdash --open
47
+ portdash 3000-5000 --open
48
+ portdash 3000 5173 8080 --once
49
+ portdash 3000,5173 --json
50
+ `);
51
+ }
52
+
53
+ function parseIntOption(name, value, { min, max }) {
54
+ const str = String(value);
55
+ if (!/^\d+$/.test(str)) {
56
+ throw new Error(`Invalid ${name}: ${value}`);
57
+ }
58
+ const num = Number.parseInt(str, 10);
59
+ if (num < min || num > max) {
60
+ throw new Error(`Invalid ${name}: ${value} (expected ${min}-${max})`);
61
+ }
62
+ return num;
63
+ }
64
+
65
+ function parseTargets({ ports, range, positionals }) {
66
+ const portParts = [];
67
+ let rangeValue = range ?? null;
68
+
69
+ if (ports) portParts.push(ports);
70
+
71
+ for (const rawToken of positionals) {
72
+ const token = rawToken.trim();
73
+ if (!token) continue;
74
+
75
+ const pieces = token.split(',').map(s => s.trim()).filter(Boolean);
76
+ for (const piece of pieces) {
77
+ if (/^\d+$/.test(piece)) {
78
+ portParts.push(piece);
79
+ continue;
80
+ }
81
+
82
+ if (/^\d+-\d+$/.test(piece)) {
83
+ if (rangeValue && rangeValue !== piece) {
84
+ throw new Error(`Only one range can be provided (got "${rangeValue}" and "${piece}")`);
85
+ }
86
+ rangeValue = piece;
87
+ continue;
88
+ }
89
+
90
+ throw new Error(`Invalid target: "${rawToken}"`);
91
+ }
92
+ }
93
+
94
+ return {
95
+ ports: portParts.length > 0 ? portParts.join(',') : null,
96
+ range: rangeValue,
97
+ };
98
+ }
99
+
100
+ function parseCliArgs(argv) {
101
+ const { values, positionals } = parseCli({
102
+ args: argv.slice(2),
103
+ allowPositionals: true,
104
+ strict: true,
105
+ options: {
106
+ help: { type: 'boolean', short: 'h' },
107
+ open: { type: 'boolean', short: 'o', default: false },
108
+ once: { type: 'boolean', short: '1', default: false },
109
+ json: { type: 'boolean', short: 'j', default: false },
110
+ bind: { type: 'string', default: DEFAULT_BIND_HOST },
111
+ host: { type: 'string' }, // legacy alias for --bind
112
+ port: { type: 'string', default: String(DEFAULT_DASHBOARD_PORT) },
113
+ ports: { type: 'string' },
114
+ range: { type: 'string' },
115
+ timeout: { type: 'string', default: String(DEFAULT_TIMEOUT_MS) },
116
+ concurrency: { type: 'string', default: String(DEFAULT_CONCURRENCY) },
117
+ 'no-title': { type: 'boolean', default: false },
118
+ https: { type: 'boolean', default: true }, // legacy: allow --https
119
+ 'no-https': { type: 'boolean', default: false },
120
+ },
121
+ });
122
+
123
+ let help = values.help;
124
+ let open = values.open;
125
+ let once = values.once;
126
+ let json = values.json;
127
+
128
+ const targetPositionals = [];
129
+ for (const raw of positionals) {
130
+ if (raw === 'help') help = true;
131
+ else if (raw === 'open') open = true;
132
+ else if (raw === 'once') once = true;
133
+ else if (raw === 'json') { json = true; once = true; }
134
+ else targetPositionals.push(raw);
135
+ }
136
+
137
+ const bindHost = values.host ?? values.bind ?? DEFAULT_BIND_HOST;
138
+ const port = parseIntOption('port', values.port, { min: 1, max: 65535 });
139
+ const timeout = parseIntOption('timeout', values.timeout, { min: 1, max: 60_000 });
140
+ const concurrency = parseIntOption('concurrency', values.concurrency, { min: 1, max: 65_536 });
141
+
142
+ const targets = parseTargets({ ports: values.ports, range: values.range, positionals: targetPositionals });
143
+
144
+ return {
145
+ help,
146
+ bindHost,
147
+ port,
148
+ ports: targets.ports,
149
+ range: targets.range,
150
+ timeout,
151
+ concurrency,
152
+ open: open && !(once || json),
153
+ once: once || json,
154
+ json,
155
+ noTitle: values['no-title'],
156
+ https: values['no-https'] ? false : values.https,
157
+ };
158
+ }
159
+
160
+ // ============================================================================
161
+ // Port Parsing
162
+ // ============================================================================
163
+
164
+ export function expandPorts({ ports, range }) {
165
+ const result = new Set();
166
+
167
+ if (ports) {
168
+ for (const part of ports.split(',').map(s => s.trim()).filter(Boolean)) {
169
+ const p = parseInt(part, 10);
170
+ if (p > 0 && p < 65536) result.add(p);
171
+ }
172
+ }
173
+
174
+ if (range) {
175
+ const match = range.match(/^(\d+)-(\d+)$/);
176
+ if (match) {
177
+ const start = parseInt(match[1], 10);
178
+ const end = parseInt(match[2], 10);
179
+ for (let p = start; p <= end && p < 65536; p++) {
180
+ if (p > 0) result.add(p);
181
+ }
182
+ }
183
+ }
184
+
185
+ if (result.size === 0) {
186
+ for (let p = 3000; p <= 9999; p++) result.add(p);
187
+ }
188
+
189
+ return Array.from(result).sort((a, b) => a - b);
190
+ }
191
+
192
+ // ============================================================================
193
+ // Concurrency Limiter
194
+ // ============================================================================
195
+
196
+ async function limitConcurrency(tasks, limit) {
197
+ const results = [];
198
+ const executing = new Set();
199
+
200
+ for (const task of tasks) {
201
+ const promise = task().then(result => {
202
+ executing.delete(promise);
203
+ return result;
204
+ });
205
+ executing.add(promise);
206
+ results.push(promise);
207
+
208
+ if (executing.size >= limit) {
209
+ await Promise.race(executing);
210
+ }
211
+ }
212
+
213
+ return Promise.all(results);
214
+ }
215
+
216
+ // ============================================================================
217
+ // TCP Probe
218
+ // ============================================================================
219
+
220
+ function tcpProbe(host, port, timeout) {
221
+ return new Promise(resolve => {
222
+ const socket = new Socket();
223
+ let resolved = false;
224
+
225
+ const done = (result) => {
226
+ if (!resolved) {
227
+ resolved = true;
228
+ socket.destroy();
229
+ resolve(result);
230
+ }
231
+ };
232
+
233
+ socket.setTimeout(timeout);
234
+ socket.on('connect', () => done(true));
235
+ socket.on('timeout', () => done(false));
236
+ socket.on('error', () => done(false));
237
+ socket.connect(port, host);
238
+ });
239
+ }
240
+
241
+ // ============================================================================
242
+ // HTTP/HTTPS Fetch
243
+ // ============================================================================
244
+
245
+ function fetchUrl(url, timeout, maxBytes = 65536) {
246
+ return new Promise(resolve => {
247
+ const isHttps = url.startsWith('https://');
248
+ const req = (isHttps ? httpsRequest : httpRequest)(url, {
249
+ timeout,
250
+ rejectUnauthorized: false,
251
+ headers: { 'User-Agent': 'portdash/1.0', 'Accept': 'text/html,*/*' },
252
+ }, res => {
253
+ const headers = res.headers;
254
+ const status = res.statusCode;
255
+ let body = '';
256
+
257
+ res.setEncoding('utf8');
258
+ res.on('data', chunk => {
259
+ body += chunk;
260
+ if (body.length > maxBytes) res.destroy();
261
+ });
262
+ res.on('end', () => resolve({ status, headers, body, url }));
263
+ res.on('error', () => resolve(null));
264
+ });
265
+
266
+ req.on('timeout', () => { req.destroy(); resolve(null); });
267
+ req.on('error', () => resolve(null));
268
+ req.end();
269
+ });
270
+ }
271
+
272
+ // ============================================================================
273
+ // Title Extraction
274
+ // ============================================================================
275
+
276
+ export function extractTitle(html) {
277
+ if (!html) return null;
278
+ const match = html.match(/<title[^>]*>([^<]*)<\/title>/i);
279
+ if (!match) return null;
280
+
281
+ let title = match[1]
282
+ .replace(/&amp;/g, '&')
283
+ .replace(/&lt;/g, '<')
284
+ .replace(/&gt;/g, '>')
285
+ .replace(/&quot;/g, '"')
286
+ .replace(/&#39;/g, "'")
287
+ .replace(/&nbsp;/g, ' ')
288
+ .replace(/\s+/g, ' ')
289
+ .trim();
290
+
291
+ return title.length > 80 ? title.slice(0, 77) + '...' : title;
292
+ }
293
+
294
+ // ============================================================================
295
+ // Port Probe
296
+ // ============================================================================
297
+
298
+ async function probePort(host, port, opts) {
299
+ const tcpOpen = await tcpProbe(host, port, opts.timeout);
300
+ if (!tcpOpen) {
301
+ const tcpOpen6 = await tcpProbe('::1', port, opts.timeout);
302
+ if (!tcpOpen6) return null;
303
+ host = '::1';
304
+ }
305
+
306
+ let result = { port, host, status: null, url: null, title: null };
307
+
308
+ // Try HTTP first
309
+ let httpUrl = `http://${host === '::1' ? '[::1]' : host}:${port}/`;
310
+ let res = await fetchUrl(httpUrl, opts.timeout);
311
+
312
+ // If HTTP failed, try HTTPS
313
+ if (!res && opts.https) {
314
+ const httpsUrl = `https://${host === '::1' ? '[::1]' : host}:${port}/`;
315
+ res = await fetchUrl(httpsUrl, opts.timeout);
316
+ }
317
+
318
+ if (!res) {
319
+ result.status = 'tcp-only';
320
+ result.url = httpUrl;
321
+ return result;
322
+ }
323
+
324
+ // Handle redirects (up to 2)
325
+ let redirects = 0;
326
+ while (res.status >= 300 && res.status < 400 && res.headers.location && redirects < 2) {
327
+ const loc = res.headers.location;
328
+ if (!loc.includes('localhost') && !loc.includes('127.0.0.1') && !loc.includes('[::1]')) break;
329
+ res = await fetchUrl(loc.startsWith('http') ? loc : new URL(loc, res.url).href, opts.timeout);
330
+ if (!res) break;
331
+ redirects++;
332
+ }
333
+
334
+ if (!res) return result;
335
+
336
+ result.status = res.status;
337
+ result.url = res.url;
338
+
339
+ if (!opts.noTitle) {
340
+ const contentType = res.headers['content-type'] || '';
341
+ if (contentType.includes('text/html')) {
342
+ result.title = extractTitle(res.body);
343
+ }
344
+ }
345
+
346
+ return result;
347
+ }
348
+
349
+ // ============================================================================
350
+ // Scanner
351
+ // ============================================================================
352
+
353
+ async function scanPorts(host, ports, opts) {
354
+ const startTime = Date.now();
355
+ const tasks = ports.map(port => () => probePort(host, port, opts));
356
+ const results = await limitConcurrency(tasks, opts.concurrency);
357
+ const duration = Date.now() - startTime;
358
+
359
+ return {
360
+ timestamp: new Date().toISOString(),
361
+ duration,
362
+ services: results.filter(Boolean).sort((a, b) => a.port - b.port),
363
+ };
364
+ }
365
+
366
+ // ============================================================================
367
+ // Console Output
368
+ // ============================================================================
369
+
370
+ function printTable(scan) {
371
+ const { services, duration } = scan;
372
+
373
+ if (services.length === 0) {
374
+ console.log('No dev servers found.');
375
+ return;
376
+ }
377
+
378
+ console.log(`Found ${services.length} server(s) in ${duration}ms:\n`);
379
+ console.log('PORT TITLE');
380
+ console.log('─'.repeat(50));
381
+
382
+ for (const s of services) {
383
+ const port = String(s.port).padEnd(6);
384
+ const title = (s.title || '—').slice(0, 42);
385
+ console.log(`${port} ${title}`);
386
+ }
387
+ }
388
+
389
+ // ============================================================================
390
+ // Dashboard HTML
391
+ // ============================================================================
392
+
393
+ function renderDashboard() {
394
+ return `<!DOCTYPE html>
395
+ <html lang="en">
396
+ <head>
397
+ <meta charset="UTF-8">
398
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
399
+ <title>portdash</title>
400
+ <style>
401
+ * { box-sizing: border-box; margin: 0; padding: 0; }
402
+ :root { --bg: #0a0a0a; --surface: #141414; --border: #262626; --text: #fafafa; --muted: #737373; --accent: #3b82f6; }
403
+ body { font-family: system-ui, sans-serif; background: var(--bg); color: var(--text); padding: 2rem; min-height: 100vh; }
404
+ .container { max-width: 900px; margin: 0 auto; }
405
+ h1 { font-size: 1.5rem; margin-bottom: 0.5rem; }
406
+ .meta { color: var(--muted); font-size: 0.875rem; margin-bottom: 1rem; }
407
+ .controls { display: flex; gap: 1rem; margin-bottom: 1rem; align-items: center; }
408
+ input[type="text"] { background: var(--surface); border: 1px solid var(--border); color: var(--text); padding: 0.5rem 1rem; border-radius: 0.375rem; width: 250px; }
409
+ input:focus { outline: none; border-color: var(--accent); }
410
+ table { width: 100%; border-collapse: collapse; }
411
+ th, td { text-align: left; padding: 0.75rem 1rem; border-bottom: 1px solid var(--border); }
412
+ th { color: var(--muted); font-weight: 500; font-size: 0.75rem; text-transform: uppercase; }
413
+ tr:hover { background: var(--surface); }
414
+ a { color: var(--accent); text-decoration: none; }
415
+ a:hover { text-decoration: underline; }
416
+ .port { font-family: monospace; }
417
+ .empty { text-align: center; padding: 3rem; color: var(--muted); }
418
+ .status { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #22c55e; margin-right: 0.5rem; }
419
+ </style>
420
+ </head>
421
+ <body>
422
+ <div class="container">
423
+ <h1>portdash</h1>
424
+ <div class="meta">
425
+ <span id="scanInfo">Scanning...</span>
426
+ </div>
427
+ <div class="controls">
428
+ <input type="text" id="filter" placeholder="Filter by port or title...">
429
+ </div>
430
+ <table>
431
+ <thead><tr><th>Port</th><th>Title</th></tr></thead>
432
+ <tbody id="tbody"></tbody>
433
+ </table>
434
+ <div id="empty" class="empty" style="display:none;">No dev servers found</div>
435
+ </div>
436
+ <script>
437
+ let allServices = [];
438
+ const tbody = document.getElementById('tbody');
439
+ const empty = document.getElementById('empty');
440
+ const scanInfo = document.getElementById('scanInfo');
441
+ const filter = document.getElementById('filter');
442
+
443
+ function render() {
444
+ const q = filter.value.toLowerCase();
445
+ const filtered = allServices.filter(s =>
446
+ String(s.port).includes(q) ||
447
+ (s.title || '').toLowerCase().includes(q)
448
+ );
449
+
450
+ if (filtered.length === 0) {
451
+ tbody.innerHTML = '';
452
+ empty.style.display = 'block';
453
+ return;
454
+ }
455
+
456
+ empty.style.display = 'none';
457
+ tbody.innerHTML = filtered.map(s => \`
458
+ <tr>
459
+ <td class="port"><span class="status"></span>\${s.port}</td>
460
+ <td><a href="\${s.url}" target="_blank">\${s.title || '—'}</a></td>
461
+ </tr>
462
+ \`).join('');
463
+ }
464
+
465
+ async function refresh() {
466
+ try {
467
+ const res = await fetch('/api/scan?fresh=1');
468
+ const data = await res.json();
469
+ allServices = data.services || [];
470
+ scanInfo.textContent = \`\${allServices.length} server(s) found in \${data.duration}ms • Last scan: \${new Date(data.timestamp).toLocaleTimeString()}\`;
471
+ render();
472
+ } catch (e) {
473
+ scanInfo.textContent = 'Scan failed';
474
+ }
475
+ }
476
+
477
+ filter.addEventListener('input', render);
478
+ refresh();
479
+ setInterval(refresh, 5000);
480
+ </script>
481
+ </body>
482
+ </html>`;
483
+ }
484
+
485
+ // ============================================================================
486
+ // Server
487
+ // ============================================================================
488
+
489
+ function formatHostForUrl(host) {
490
+ if (host.includes(':') && !(host.startsWith('[') && host.endsWith(']'))) return `[${host}]`;
491
+ return host;
492
+ }
493
+
494
+ async function listenWithFallback(server, host, startPort) {
495
+ let port = startPort;
496
+ for (let attempt = 0; attempt < 20; attempt++) {
497
+ try {
498
+ await new Promise((resolve, reject) => {
499
+ server.once('error', reject);
500
+ server.listen(port, host, () => {
501
+ server.removeListener('error', reject);
502
+ resolve();
503
+ });
504
+ });
505
+ return port;
506
+ } catch (e) {
507
+ if (e.code === 'EADDRINUSE') {
508
+ port++;
509
+ continue;
510
+ }
511
+ throw e;
512
+ }
513
+ }
514
+
515
+ throw new Error(`Could not find available port (tried ${startPort}-${port})`);
516
+ }
517
+
518
+ async function startServer(host, port, ports, opts) {
519
+ let cachedScan = null;
520
+ let cacheTime = 0;
521
+
522
+ const server = createServer(async (req, res) => {
523
+ const url = new URL(req.url, 'http://localhost');
524
+
525
+ if (url.pathname === '/healthz') {
526
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
527
+ res.end('ok');
528
+ return;
529
+ }
530
+
531
+ if (url.pathname === '/api/scan') {
532
+ const fresh = url.searchParams.get('fresh') === '1';
533
+ const now = Date.now();
534
+
535
+ if (fresh || !cachedScan || now - cacheTime > 3000) {
536
+ cachedScan = await scanPorts(SCAN_HOST, ports, opts);
537
+ cacheTime = now;
538
+ }
539
+
540
+ res.writeHead(200, { 'Content-Type': 'application/json' });
541
+ res.end(JSON.stringify(cachedScan));
542
+ return;
543
+ }
544
+
545
+ if (url.pathname === '/' || url.pathname === '/index.html') {
546
+ res.writeHead(200, { 'Content-Type': 'text/html' });
547
+ res.end(renderDashboard());
548
+ return;
549
+ }
550
+
551
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
552
+ res.end('Not Found');
553
+ });
554
+
555
+ let actualPort;
556
+ try {
557
+ actualPort = await listenWithFallback(server, host, port);
558
+ } catch (e) {
559
+ console.error(`Error: ${e.message}`);
560
+ process.exit(1);
561
+ }
562
+
563
+ const urlHost = host === '0.0.0.0' ? '127.0.0.1' : host === '::' ? '::1' : host;
564
+ const dashboardUrl = `http://${formatHostForUrl(urlHost)}:${actualPort}/`;
565
+ console.log(`portdash running at ${dashboardUrl}`);
566
+ console.log(`Scanning ${ports.length} ports on localhost`);
567
+
568
+ return { server, url: dashboardUrl };
569
+ }
570
+
571
+ // ============================================================================
572
+ // Browser Open
573
+ // ============================================================================
574
+
575
+ function openBrowser(url) {
576
+ const cmd = process.platform === 'darwin' ? 'open' :
577
+ process.platform === 'win32' ? 'start' : 'xdg-open';
578
+ exec(`${cmd} ${url}`);
579
+ }
580
+
581
+ // ============================================================================
582
+ // Main
583
+ // ============================================================================
584
+
585
+ async function main() {
586
+ let args;
587
+ try {
588
+ args = parseCliArgs(process.argv);
589
+ } catch (e) {
590
+ console.error(`Error: ${e.message}`);
591
+ showHelp();
592
+ process.exit(1);
593
+ }
594
+
595
+ if (args.help) {
596
+ showHelp();
597
+ process.exit(0);
598
+ }
599
+
600
+ const ports = expandPorts({ ports: args.ports, range: args.range });
601
+ const scanOpts = {
602
+ timeout: args.timeout,
603
+ concurrency: args.concurrency,
604
+ noTitle: args.noTitle,
605
+ https: args.https,
606
+ };
607
+
608
+ if (args.once) {
609
+ const scan = await scanPorts(SCAN_HOST, ports, scanOpts);
610
+ if (args.json) {
611
+ console.log(JSON.stringify(scan, null, 2));
612
+ } else {
613
+ printTable(scan);
614
+ }
615
+ process.exit(0);
616
+ }
617
+
618
+ const { url } = await startServer(args.bindHost, args.port, ports, scanOpts);
619
+
620
+ if (args.open) {
621
+ openBrowser(url);
622
+ }
623
+ }
624
+
625
+ // Only run main when executed directly (not when imported for testing)
626
+ const isMain = import.meta.url === `file://${process.argv[1]}` ||
627
+ process.argv[1]?.endsWith('portdash.mjs') ||
628
+ process.argv[1]?.endsWith('portdash');
629
+
630
+ if (isMain) {
631
+ main().catch(err => {
632
+ console.error('Error:', err.message);
633
+ process.exit(1);
634
+ });
635
+ }