UNPKG

sap-cap-debugger

Version:

NPX tool for remote debugging SAP CAP applications on Cloud Foundry (CAP v7 and earlier)

62 lines 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PortManager = void 0; const command_1 = require("../utils/command"); class PortManager { constructor(logger) { this.logger = logger; this.commandExecutor = new command_1.CommandExecutor(logger); } async isPortInUse(port) { const result = await this.commandExecutor.execute('netstat', ['-an']); if (result.success) { return result.output.includes(`:${port}`) && result.output.includes('LISTEN'); } return false; } async killProcessesOnPort(port) { this.logger.info(`Killing processes on port ${port}...`); // Try to find processes using the port const lsofResult = await this.commandExecutor.execute('lsof', ['-ti', `:${port}`]); if (lsofResult.success && lsofResult.output.trim()) { const pids = lsofResult.output.trim().split('\n'); for (const pid of pids) { if (pid.trim()) { this.logger.debug(`Killing process ${pid} on port ${port}`); await this.commandExecutor.execute('kill', ['-9', pid.trim()]); } } } // Also kill any cf ssh processes await this.commandExecutor.execute('pkill', ['-f', 'cf ssh']); return true; } async verifyPort(port, maxAttempts = 15) { this.logger.info(`Verifying port ${port}...`); let attempt = 1; while (attempt <= maxAttempts) { if (await this.isPortInUse(port)) { this.logger.success(`Port ${port} is in use`); return true; } this.logger.info(`Waiting for port to be available... (attempt ${attempt}/${maxAttempts})`); await new Promise(resolve => setTimeout(resolve, 2000)); attempt++; } this.logger.warning(`Port ${port} verification failed, but continuing...`); return false; } async cleanupPort(port) { this.logger.info(`Cleaning up port ${port}...`); await this.killProcessesOnPort(port); // Verify cleanup if (await this.isPortInUse(port)) { this.logger.warning(`Port ${port} is still in use after cleanup`); } else { this.logger.success(`Port ${port} is free`); } } } exports.PortManager = PortManager; //# sourceMappingURL=port-manager.js.map