Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
.idea
*.tgz
2 changes: 2 additions & 0 deletions src/commander/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import startServer from './server.js';
import stopServer from './stopServer.js'
import ping from './ping.js'
import merge from './merge.js'
import pingTest from './pingTest.js'

const program = new Command();

Expand Down Expand Up @@ -36,6 +37,7 @@ program
.addCommand(configAppFigma)
.addCommand(uploadWebFigmaCommand)
.addCommand(uploadAppFigmaCommand)
.addCommand(pingTest)



Expand Down
85 changes: 85 additions & 0 deletions src/commander/pingTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Command } from 'commander';
import * as http from 'http';
import * as https from 'https';
import chalk from 'chalk'

function getSmartUIServerAddress() {
const serverAddress = process.env.SMARTUI_SERVER_ADDRESS || 'http://localhost:49152';
return serverAddress;
}

function makeHttpRequest(url: string, timeout: number): Promise<{ status: number; data: any }> {
return new Promise((resolve, reject) => {
const urlObj = new URL(url);
const isHttps = urlObj.protocol === 'https:';
const client = isHttps ? https : http;

const req = client.request(url, { timeout }, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
let parsedData;
try {
parsedData = JSON.parse(data);
} catch {
parsedData = data;
}

resolve({
status: res.statusCode || 0,
data: parsedData
});
});
});

req.on('error', (error) => {
reject(error);
});

req.on('timeout', () => {
req.destroy();
const timeoutError = new Error('Request timeout');
(timeoutError as any).code = 'ECONNABORTED';
reject(timeoutError);
});

req.end();
});
}

const command = new Command();

command
.name('exec:pingTest')
.description('Ping the SmartUI server to check if it is running using default http client')
.action(async function(this: Command) {
try {
console.log(chalk.yellow("Pinging server using default http client..."));
const serverAddress = getSmartUIServerAddress();
console.log(chalk.yellow(`Pinging server at ${serverAddress} from terminal using default http client...`));

// Send GET request to the /ping endpoint
const response = await makeHttpRequest(`${serverAddress}/ping`, 15000);

// Log the response from the server
if (response.status === 200) {
console.log(chalk.green('SmartUI Server is running'));
console.log(chalk.green(`Response: ${JSON.stringify(response.data)}`)); // Log response data if needed
} else {
console.log(chalk.red('Failed to reach the server'));
}
} catch (error: any) {
// Handle any errors during the HTTP request
if (error.code === 'ECONNABORTED') {
console.error(chalk.red('Error: SmartUI server did not respond in 15 seconds'));
} else {
console.error(chalk.red('SmartUI server is not running'));
}
}
});

export default command;