diff --git a/src/commander/server.ts b/src/commander/server.ts index 8178162..475dfcb 100644 --- a/src/commander/server.ts +++ b/src/commander/server.ts @@ -8,6 +8,7 @@ import getGitInfo from '../tasks/getGitInfo.js'; import createBuildExec from '../tasks/createBuildExec.js'; import snapshotQueue from '../lib/snapshotQueue.js'; import { startPolling, startPingPolling } from '../lib/utils.js'; +import startTunnel from '../tasks/startTunnel.js' const command = new Command(); @@ -27,12 +28,13 @@ command ctx.snapshotQueue = new snapshotQueue(ctx); ctx.totalSnapshots = 0 ctx.isStartExec = true - + let tasks = new Listr( [ authExec(ctx), startServer(ctx), getGitInfo(ctx), + ...(ctx.config.tunnel && ctx.config.tunnel?.type === 'auto' ? [startTunnel(ctx)] : []), createBuildExec(ctx), ], @@ -51,7 +53,7 @@ command try { await tasks.run(ctx); if (ctx.build && ctx.build.id) { - startPingPolling(ctx); + startPingPolling(ctx, 'exec-start'); } if (ctx.options.fetchResults && ctx.build && ctx.build.id) { startPolling(ctx, '', false, '') @@ -59,6 +61,7 @@ command } catch (error) { console.error('Error during server execution:', error); + process.exit(1); } }); diff --git a/src/commander/stopServer.ts b/src/commander/stopServer.ts index 3aae301..0ff3150 100644 --- a/src/commander/stopServer.ts +++ b/src/commander/stopServer.ts @@ -33,8 +33,10 @@ command } } catch (error: any) { // Handle any errors during the HTTP request - if (error.code === 'ECONNABORTED') { + if (error && error.code === 'ECONNABORTED') { console.error(chalk.red('Error: SmartUI server did not respond in 15 seconds')); + } if (error && error.code === 'ECONNREFUSED') { + console.error(chalk.red('Error: Looks like smartui cli server is already stopped')); } else { console.error(chalk.red('Error while stopping server')); } diff --git a/src/lib/schemaValidation.ts b/src/lib/schemaValidation.ts index cdfc42d..0f64783 100644 --- a/src/lib/schemaValidation.ts +++ b/src/lib/schemaValidation.ts @@ -239,6 +239,11 @@ const ConfigSchema = { type: "string", errorMessage: "Invalid config; logFile should be a string value" }, + environment: { + type: "string", + enum: ["stage", "prod"], + errorMessage: "Invalid config; environment should be a string value either stage or prod" + } }, required: ["type"], additionalProperties: false diff --git a/src/lib/server.ts b/src/lib/server.ts index 2914526..8a78246 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -5,7 +5,7 @@ import { readFileSync, truncate } from 'fs' import { Context } from '../types.js' import { validateSnapshot } from './schemaValidation.js' import { pingIntervalId } from './utils.js'; -import { startPolling } from './utils.js'; +import { stopTunnelHelper } from './utils.js'; const uploadDomToS3ViaEnv = process.env.USE_LAMBDA_INTERNAL || false; export default async (ctx: Context): Promise> => { @@ -151,6 +151,11 @@ export default async (ctx: Context): Promise { +export async function startPingPolling(ctx: Context, event: string): Promise { try { ctx.log.debug('Sending initial ping to server...'); await ctx.client.ping(ctx.build.id, ctx.log); @@ -336,9 +336,9 @@ export async function startPingPolling(ctx: Context): Promise { // Start the polling interval pingIntervalId = setInterval(async () => { try { - ctx.log.debug('Sending ping to server...'); + ctx.log.debug('Sending ping to server...'+ event); await ctx.client.ping(ctx.build.id, ctx.log); - ctx.log.debug('Ping sent successfully.'); + ctx.log.debug('Ping sent successfully.'+ event); } catch (error: any) { ctx.log.error(`Error during ping polling: ${error.message}`); } @@ -390,6 +390,11 @@ export async function startTunnelBinary(ctx: Context) { ctx.config.tunnel.tunnelName = randomTunnelName } + if (tunnelConfig?.environment) { + tunnelArguments.environment = tunnelConfig.environment + } + + ctx.log.debug(`tunnel config ${JSON.stringify(tunnelArguments)}`) if (ctx.config.tunnel?.type === 'auto') { @@ -434,7 +439,7 @@ export async function startPollingForTunnel(ctx: Context, build_id: string, base const status = await tunnelInstance.stop(); ctx.log.debug('Tunnel is Stopped ? ' + status); - + return; } } catch (error: any) { if (error.message.includes('ENOTFOUND')) { @@ -450,10 +455,10 @@ export async function startPollingForTunnel(ctx: Context, build_id: string, base export async function stopTunnelHelper(ctx: Context) { const tunnelRunningStatus = await tunnelInstance.isRunning(); - ctx.log.debug('Running status of tunnel before stopping ? ' + tunnelRunningStatus); + ctx.log.debug('stop-tunnel:: Running status of tunnel before stopping ? ' + tunnelRunningStatus); const status = await tunnelInstance.stop(); - ctx.log.debug('Tunnel is Stopped ? ' + status); + ctx.log.debug('stop-tunnel:: Tunnel is Stopped ? ' + status); } /** diff --git a/src/tasks/createBuildExec.ts b/src/tasks/createBuildExec.ts index 1e58fb0..e999123 100644 --- a/src/tasks/createBuildExec.ts +++ b/src/tasks/createBuildExec.ts @@ -36,6 +36,9 @@ export default (ctx: Context): ListrTask