🛠️ Add SSHHostConfig typedef and improve argument parsing with validation for target

This commit is contained in:
2026-01-28 05:29:43 +00:00
parent c8194b3083
commit fbd1d8c94f

View File

@@ -29,6 +29,16 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
// Configuration // Configuration
/**
* @typedef {Object} SSHHostConfig
* @property {string} host
* @property {number} port
* @property {string} username
* @property {string} privateKeyPath
* @property {string} password
*/
/** @type {Record<'internal'|'external', SSHHostConfig>} */
const SSH_HOSTS = { const SSH_HOSTS = {
internal: { internal: {
host: '10.0.0.81', host: '10.0.0.81',
@@ -55,8 +65,12 @@ const DIST_DIR = path.join(SOURCE_DIR, 'dist');
* Parse command line arguments * Parse command line arguments
* @returns {Object} Parsed arguments * @returns {Object} Parsed arguments
*/ */
/**
* @returns {{ target: keyof typeof SSH_HOSTS, port: number, sslPort: number|null }}
*/
function parseArgs() { function parseArgs() {
const args = process.argv.slice(2); const args = process.argv.slice(2);
/** @type {{ target: keyof typeof SSH_HOSTS, port: number, sslPort: number|null }} */
const config = { const config = {
target: 'internal', target: 'internal',
port: 8080, port: 8080,
@@ -65,7 +79,15 @@ function parseArgs() {
for (let i = 0; i < args.length; i++) { for (let i = 0; i < args.length; i++) {
if (args[i] === '--target' && args[i + 1]) { if (args[i] === '--target' && args[i + 1]) {
config.target = args[i + 1]; /** @type {any} */
const t = args[i + 1];
if (t === 'internal' || t === 'external') {
config.target = t;
} else {
throw new Error(
`Invalid target: ${t}. Must be 'internal' or 'external'.`
);
}
i++; i++;
} else if (args[i] === '--port' && args[i + 1]) { } else if (args[i] === '--port' && args[i + 1]) {
config.port = parseInt(args[i + 1], 10); config.port = parseInt(args[i + 1], 10);
@@ -76,13 +98,6 @@ function parseArgs() {
} }
} }
// Validate target
if (!SSH_HOSTS[config.target]) {
throw new Error(
`Invalid target: ${config.target}. Must be 'internal' or 'external'.`
);
}
// Validate port // Validate port
if (isNaN(config.port) || config.port < 1 || config.port > 65535) { if (isNaN(config.port) || config.port < 1 || config.port > 65535) {
throw new Error( throw new Error(