Skip to content

Commit 04f2439

Browse files
committed
add new ping test which uses default http client
1 parent 2f3ffe7 commit 04f2439

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
22
dist
3+
.idea
4+
*.tgz

src/commander/commander.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import startServer from './server.js';
99
import stopServer from './stopServer.js'
1010
import ping from './ping.js'
1111
import merge from './merge.js'
12+
import pingTest from './pingTest.js'
1213

1314
const program = new Command();
1415

@@ -36,6 +37,7 @@ program
3637
.addCommand(configAppFigma)
3738
.addCommand(uploadWebFigmaCommand)
3839
.addCommand(uploadAppFigmaCommand)
40+
.addCommand(pingTest)
3941

4042

4143

src/commander/pingTest.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)