🛠️ Simplify argument parsing and validation logic by removing redundant type annotations and relocating target validation
This commit is contained in:
@@ -29,16 +29,6 @@ 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',
|
||||||
@@ -65,12 +55,8 @@ 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,
|
||||||
@@ -79,15 +65,7 @@ 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]) {
|
||||||
/** @type {any} */
|
config.target = args[i + 1];
|
||||||
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);
|
||||||
@@ -98,6 +76,13 @@ 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(
|
||||||
|
|||||||
Reference in New Issue
Block a user