All files / server config.js

100% Statements 35/35
93.33% Branches 28/30
100% Functions 1/1
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    2x 22x   22x                 22x 16x 16x   16x   3x 3x   2x 2x   2x 1x 1x   2x   2x 2x 2x 1x   2x   2x   2x 2x 2x   2x   1x 1x       4x 4x 2x     4x     22x 3x   22x        
import { printHelp } from './utils.js';
 
const parseArgs = () => {
	const args = process.argv.slice(2);
	// Defaults
	const config = {
		ssl: false,
		sslDir: '/etc/ssl/private',
		saveInterval: 30 * 60 * 1000, // 30 minutes in milliseconds
		sessionName: 'joint',
		debug: false,
		port: 1337,
	};
 
	for (let i = 0; i < args.length; i++) {
		const arg = args[i];
		const nextArg = args[i + 1];
 
		switch (arg) {
			case '--debug':
				config.debug = true;
				break;
			case '--ssl':
				config.ssl = true;
				break;
			case '--ssl-dir':
				if (nextArg && !nextArg.startsWith('--')) {
					config.sslDir = nextArg;
					i++; // Skip next argument as we consumed it
				}
				break;
			case '--save-interval':
				Eif (nextArg && !nextArg.startsWith('--')) {
					const minutes = parseInt(nextArg);
					if (!isNaN(minutes) && minutes > 0) {
						config.saveInterval = minutes * 60 * 1000;
					}
					i++; // Skip next argument as we consumed it
				}
				break;
			case '--session-name':
				Eif (nextArg && !nextArg.startsWith('--')) {
					config.sessionName = nextArg;
					i++; // Skip next argument as we consumed it
				}
				break;
			case '--help':
				printHelp();
				break;
			default:
				// Check if it's a port number
				{
					const port = parseInt(arg);
					if (!isNaN(port) && port > 0 && port <= 65535) {
						config.port = port;
					}
				}
				break;
		}
	}
	if (config.debug) {
		console.log('Server configuration:', config);
	}
	return config;
};
 
export { parseArgs };