-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·140 lines (116 loc) · 3.34 KB
/
cli.js
File metadata and controls
executable file
·140 lines (116 loc) · 3.34 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
/**
* envx-ui - CLI Entry Point
*
* A minimal local UI for managing dotenvx environment files.
*
* Usage:
* node cli.js [--port PORT]
* npx envx-ui
*/
const { startServer } = require('./server');
// ============================================
// Parse Arguments
// ============================================
function parseArgs() {
const args = process.argv.slice(2);
const config = {
port: 0, // 0 = OS assigns random available port
cwd: process.cwd()
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--port' || arg === '-p') {
const portArg = args[i + 1];
if (portArg && !portArg.startsWith('-')) {
const port = parseInt(portArg, 10);
if (!isNaN(port) && port > 0 && port < 65536) {
config.port = port;
}
i++;
}
}
if (arg === '--help' || arg === '-h') {
showHelp();
process.exit(0);
}
if (arg === '--version' || arg === '-v') {
const pkg = require('./package.json');
console.log(`envx-ui v${pkg.version}`);
process.exit(0);
}
}
return config;
}
function showHelp() {
console.log(`
envx-ui - Local UI for managing dotenvx environment files
Usage:
node cli.js [options]
npx envx-ui [options]
Options:
-p, --port PORT Port to run server on (default: random)
-h, --help Show this help message
-v, --version Show version number
Examples:
node cli.js # Start on random port (more secure)
node cli.js --port 8080 # Start on specific port
npx envx-ui # Run via npx
Security:
- Uses random port by default to prevent browser extension attacks
- Protected with helmet.js security headers
- Only accessible from localhost
The UI will automatically open in your default browser.
Press Ctrl+C to stop the server.
`);
}
// ============================================
// Browser Launch
// ============================================
async function openBrowser(url) {
try {
// Dynamic import for ESM module
const open = await import('open');
await open.default(url);
} catch (err) {
// Fallback if open package fails
console.log(`\n Open in browser: ${url}\n`);
}
}
// ============================================
// Main
// ============================================
async function main() {
const config = parseArgs();
console.log('\n ⚡ envx-ui\n');
try {
// Start server (port 0 = OS assigns random available port)
const server = startServer(config.port, config.cwd, async (actualPort) => {
const url = `http://127.0.0.1:${actualPort}`;
console.log(` Server running at: ${url}`);
console.log(' Press Ctrl+C to stop\n');
// Open browser
await openBrowser(url);
});
// Graceful shutdown
const shutdown = () => {
console.log('\n Shutting down...');
server.close(() => {
console.log(' Server stopped\n');
process.exit(0);
});
// Force exit after timeout
setTimeout(() => {
console.log(' Forced exit');
process.exit(1);
}, 3000);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
} catch (err) {
console.error(` Error: ${err.message}\n`);
process.exit(1);
}
}
// Run
main();