UNPKG

forge-deploy-cli

Version:

Professional CLI for local deployments with automatic subdomain routing, SSL certificates, and infrastructure management

203 lines 7.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPublicIP = getPublicIP; exports.getSystemIP = getSystemIP; exports.getExternalIP = getExternalIP; exports.isWindows = isWindows; exports.isElevated = isElevated; exports.checkSystemPrivileges = checkSystemPrivileges; exports.requireElevatedPrivileges = requireElevatedPrivileges; exports.getSystemInfo = getSystemInfo; const os_1 = __importDefault(require("os")); const child_process_1 = require("child_process"); const chalk_1 = __importDefault(require("chalk")); /** * Get the system's public IP address for subdomain routing */ async function getPublicIP() { try { console.log(chalk_1.default.gray('Detecting public IP address...')); // Try multiple services for reliability const services = [ 'https://api.ipify.org', 'https://ipinfo.io/ip', 'https://icanhazip.com', 'https://checkip.amazonaws.com' ]; for (const service of services) { try { const response = await fetch(service, { signal: AbortSignal.timeout(5000) }); const ip = (await response.text()).trim(); if (ip && /^\d+\.\d+\.\d+\.\d+$/.test(ip)) { console.log(chalk_1.default.gray(`Public IP detected: ${ip}`)); return ip; } } catch { continue; } } throw new Error('All public IP services failed'); } catch (error) { console.warn(chalk_1.default.yellow('Warning: Could not detect public IP, falling back to local IP')); return getSystemIP(); } } /** * Get the system's local IP address (fallback) */ function getSystemIP() { try { const interfaces = os_1.default.networkInterfaces(); // Priority order: ethernet, wifi, then others const priorityOrder = ['Ethernet', 'Wi-Fi', 'WiFi', 'wlan0', 'eth0']; for (const name of priorityOrder) { const iface = interfaces[name]; if (iface) { for (const alias of iface) { if (alias.family === 'IPv4' && !alias.internal) { return alias.address; } } } } // Fallback: find any non-internal IPv4 address for (const name of Object.keys(interfaces)) { const iface = interfaces[name]; if (iface) { for (const alias of iface) { if (alias.family === 'IPv4' && !alias.internal) { return alias.address; } } } } // Final fallback return '127.0.0.1'; } catch (error) { console.warn(chalk_1.default.yellow('Warning: Could not detect system IP, using localhost')); return '127.0.0.1'; } } /** * Get external IP address if needed */ async function getExternalIP() { try { // Try multiple services for reliability const services = [ 'https://api.ipify.org', 'https://ipinfo.io/ip', 'https://icanhazip.com' ]; for (const service of services) { try { const response = await fetch(service, { signal: AbortSignal.timeout(5000) }); const ip = (await response.text()).trim(); if (ip && /^\d+\.\d+\.\d+\.\d+$/.test(ip)) { return ip; } } catch { continue; } } throw new Error('All services failed'); } catch (error) { console.warn(chalk_1.default.yellow('Warning: Could not detect external IP')); return getSystemIP(); } } /** * Check if running on Windows */ function isWindows() { return os_1.default.platform() === 'win32'; } /** * Check if running as administrator/root */ function isElevated() { try { if (isWindows()) { // Check if running as administrator on Windows (0, child_process_1.execSync)('net session', { stdio: 'pipe' }); return true; } else { // Check if running as root on Unix-like systems return !!(process.getuid && process.getuid() === 0); } } catch { return false; } } /** * Check if required system privileges are available and warn user */ function checkSystemPrivileges() { const elevated = isElevated(); if (!elevated) { console.log(chalk_1.default.yellow('WARNING: System Infrastructure Setup Warning:')); if (isWindows()) { console.log(chalk_1.default.gray(' For full infrastructure setup, run PowerShell as Administrator:')); console.log(chalk_1.default.cyan(' Right-click PowerShell → "Run as administrator"')); console.log(chalk_1.default.cyan(' Then run: npm install -g forge-deploy-cli')); } else { console.log(chalk_1.default.gray(' For full infrastructure setup, run with sudo:')); console.log(chalk_1.default.cyan(' sudo npm install -g forge-deploy-cli')); console.log(chalk_1.default.cyan(' Or: sudo forge infra --all')); } console.log(chalk_1.default.gray(' Without elevated privileges, some features may be limited.')); console.log(); } return elevated; } /** * Ensure command is run with proper privileges or provide guidance */ function requireElevatedPrivileges(command) { if (!isElevated()) { console.log(chalk_1.default.red(`ERROR: ${command} requires elevated privileges`)); console.log(); if (isWindows()) { console.log(chalk_1.default.blue('Windows: Run as Administrator')); console.log(chalk_1.default.gray('1. Right-click Command Prompt or PowerShell')); console.log(chalk_1.default.gray('2. Select "Run as administrator"')); console.log(chalk_1.default.cyan(`3. Run: forge ${command}`)); } else { console.log(chalk_1.default.blue('Linux/macOS: Run with sudo')); console.log(chalk_1.default.cyan(`sudo forge ${command}`)); } console.log(); process.exit(1); } } /** * Get system information for debugging */ function getSystemInfo() { return { platform: os_1.default.platform(), arch: os_1.default.arch(), hostname: os_1.default.hostname(), uptime: os_1.default.uptime(), localIP: getSystemIP(), isElevated: isElevated(), nodeVersion: process.version, memory: { total: Math.round(os_1.default.totalmem() / 1024 / 1024 / 1024), free: Math.round(os_1.default.freemem() / 1024 / 1024 / 1024) } }; } //# sourceMappingURL=system.js.map