|
| 1 | +import { Command } from 'commander'; |
| 2 | +import { colors } from '../utils/colors'; |
| 3 | +import { createSpinner } from '../utils/spinner'; |
| 4 | +import { logger } from '../utils/logger'; |
| 5 | +import { config } from '../utils/config'; |
| 6 | +import { ApiClient } from '../services/api'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Ping command to test API connectivity |
| 10 | + */ |
| 11 | +export const pingCommand = new Command('ping') |
| 12 | + .description('Test connectivity to vulnerability analysis API') |
| 13 | + .option('--verbose', 'show detailed connection information') |
| 14 | + .action(async (options) => { |
| 15 | + console.log(colors.title('🏓 Vulnify API Connectivity Test')); |
| 16 | + console.log(''); |
| 17 | + |
| 18 | + if (options.verbose) { |
| 19 | + console.log(colors.muted('Configuration:')); |
| 20 | + console.log(colors.muted(` API URL: ${config.getApiUrl()}`)); |
| 21 | + console.log(colors.muted(` Timeout: ${config.getTimeout()}ms`)); |
| 22 | + console.log(''); |
| 23 | + } |
| 24 | + |
| 25 | + const spinner = createSpinner('🔍 Testing API connectivity...'); |
| 26 | + spinner.start(); |
| 27 | + |
| 28 | + try { |
| 29 | + // Test connectivity to API - simple HTTP check |
| 30 | + const apiClient = new ApiClient(); |
| 31 | + const startTime = Date.now(); |
| 32 | + |
| 33 | + // Try to make a simple request to test connectivity |
| 34 | + // We'll catch the error but if we get a response (even an error), it means the API is reachable |
| 35 | + try { |
| 36 | + await apiClient.analyze({ |
| 37 | + ecosystem: 'npm', |
| 38 | + dependencies: [] |
| 39 | + }); |
| 40 | + } catch (error) { |
| 41 | + // If we get a validation error, it means the API is reachable |
| 42 | + if (error instanceof Error && ( |
| 43 | + error.message.includes('dependencies') || |
| 44 | + error.message.includes('At least one dependency is required') |
| 45 | + )) { |
| 46 | + // This is expected - empty dependencies array causes validation error |
| 47 | + // but it means the API is responding |
| 48 | + } else { |
| 49 | + throw error; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const responseTime = Date.now() - startTime; |
| 54 | + |
| 55 | + spinner.stop(); |
| 56 | + console.log(''); |
| 57 | + |
| 58 | + // Display API results |
| 59 | + console.log(colors.info('📡 API Service:')); |
| 60 | + console.log(colors.success(` ✅ Available (${responseTime}ms)`)); |
| 61 | + |
| 62 | + // Overall status |
| 63 | + console.log(''); |
| 64 | + console.log(colors.success('🎉 API service is available!')); |
| 65 | + console.log(colors.info('💡 Ready to analyze dependencies for vulnerabilities')); |
| 66 | + |
| 67 | + console.log(''); |
| 68 | + console.log(colors.muted('Use "vulnify test" to start analyzing your project')); |
| 69 | + |
| 70 | + } catch (error) { |
| 71 | + spinner.fail('❌ Connectivity test failed'); |
| 72 | + |
| 73 | + console.log(''); |
| 74 | + console.log(colors.error('Error details:')); |
| 75 | + if (error instanceof Error) { |
| 76 | + console.log(colors.error(` ${error.message}`)); |
| 77 | + } else { |
| 78 | + console.log(colors.error(' Unknown error occurred')); |
| 79 | + } |
| 80 | + |
| 81 | + console.log(''); |
| 82 | + console.log(colors.warning('💡 Troubleshooting:')); |
| 83 | + console.log(' • Check your internet connection'); |
| 84 | + console.log(' • Verify the API endpoint is accessible'); |
| 85 | + console.log(' • Try again in a few moments'); |
| 86 | + console.log(' • Use --verbose for more details'); |
| 87 | + |
| 88 | + if (options.verbose) { |
| 89 | + logger.error('Ping command failed', { |
| 90 | + error: error instanceof Error ? error.message : 'Unknown error', |
| 91 | + stack: error instanceof Error ? error.stack : undefined |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
0 commit comments