|
| 1 | +import { Command } from 'commander'; |
| 2 | +import * as http from 'http'; |
| 3 | +import * as https from 'https'; |
| 4 | +import chalk from 'chalk' |
| 5 | + |
| 6 | +function getSmartUIServerAddress() { |
| 7 | + const serverAddress = process.env.SMARTUI_SERVER_ADDRESS || 'http://localhost:49152'; |
| 8 | + return serverAddress; |
| 9 | +} |
| 10 | + |
| 11 | +function makeHttpRequest(url: string, timeout: number): Promise<{ status: number; data: any }> { |
| 12 | + return new Promise((resolve, reject) => { |
| 13 | + const urlObj = new URL(url); |
| 14 | + const isHttps = urlObj.protocol === 'https:'; |
| 15 | + const client = isHttps ? https : http; |
| 16 | + |
| 17 | + const req = client.request(url, { timeout }, (res) => { |
| 18 | + let data = ''; |
| 19 | + |
| 20 | + res.on('data', (chunk) => { |
| 21 | + data += chunk; |
| 22 | + }); |
| 23 | + |
| 24 | + res.on('end', () => { |
| 25 | + let parsedData; |
| 26 | + try { |
| 27 | + parsedData = JSON.parse(data); |
| 28 | + } catch { |
| 29 | + parsedData = data; |
| 30 | + } |
| 31 | + |
| 32 | + resolve({ |
| 33 | + status: res.statusCode || 0, |
| 34 | + data: parsedData |
| 35 | + }); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + req.on('error', (error) => { |
| 40 | + reject(error); |
| 41 | + }); |
| 42 | + |
| 43 | + req.on('timeout', () => { |
| 44 | + req.destroy(); |
| 45 | + const timeoutError = new Error('Request timeout'); |
| 46 | + (timeoutError as any).code = 'ECONNABORTED'; |
| 47 | + reject(timeoutError); |
| 48 | + }); |
| 49 | + |
| 50 | + req.end(); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +const command = new Command(); |
| 55 | + |
| 56 | +command |
| 57 | + .name('exec:pingTest') |
| 58 | + .description('Ping the SmartUI server to check if it is running using default http client') |
| 59 | + .action(async function(this: Command) { |
| 60 | + try { |
| 61 | + console.log(chalk.yellow("Pinging server using default http client...")); |
| 62 | + const serverAddress = getSmartUIServerAddress(); |
| 63 | + console.log(chalk.yellow(`Pinging server at ${serverAddress} from terminal using default http client...`)); |
| 64 | + |
| 65 | + // Send GET request to the /ping endpoint |
| 66 | + const response = await makeHttpRequest(`${serverAddress}/ping`, 15000); |
| 67 | + |
| 68 | + // Log the response from the server |
| 69 | + if (response.status === 200) { |
| 70 | + console.log(chalk.green('SmartUI Server is running')); |
| 71 | + console.log(chalk.green(`Response: ${JSON.stringify(response.data)}`)); // Log response data if needed |
| 72 | + } else { |
| 73 | + console.log(chalk.red('Failed to reach the server')); |
| 74 | + } |
| 75 | + } catch (error: any) { |
| 76 | + // Handle any errors during the HTTP request |
| 77 | + if (error.code === 'ECONNABORTED') { |
| 78 | + console.error(chalk.red('Error: SmartUI server did not respond in 15 seconds')); |
| 79 | + } else { |
| 80 | + console.error(chalk.red('SmartUI server is not running')); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | +export default command; |
0 commit comments