Skip to content

Commit 6daa126

Browse files
Merge pull request #362 from sushobhit-lt/DOT-6255
auto tunnel support added in exec:start
2 parents f6902b7 + 36ee857 commit 6daa126

File tree

8 files changed

+46
-13
lines changed

8 files changed

+46
-13
lines changed

src/commander/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import getGitInfo from '../tasks/getGitInfo.js';
88
import createBuildExec from '../tasks/createBuildExec.js';
99
import snapshotQueue from '../lib/snapshotQueue.js';
1010
import { startPolling, startPingPolling } from '../lib/utils.js';
11+
import startTunnel from '../tasks/startTunnel.js'
1112

1213
const command = new Command();
1314

@@ -27,12 +28,13 @@ command
2728
ctx.snapshotQueue = new snapshotQueue(ctx);
2829
ctx.totalSnapshots = 0
2930
ctx.isStartExec = true
30-
31+
3132
let tasks = new Listr<Context>(
3233
[
3334
authExec(ctx),
3435
startServer(ctx),
3536
getGitInfo(ctx),
37+
...(ctx.config.tunnel && ctx.config.tunnel?.type === 'auto' ? [startTunnel(ctx)] : []),
3638
createBuildExec(ctx),
3739

3840
],
@@ -51,14 +53,15 @@ command
5153
try {
5254
await tasks.run(ctx);
5355
if (ctx.build && ctx.build.id) {
54-
startPingPolling(ctx);
56+
startPingPolling(ctx, 'exec-start');
5557
}
5658
if (ctx.options.fetchResults && ctx.build && ctx.build.id) {
5759
startPolling(ctx, '', false, '')
5860
}
5961

6062
} catch (error) {
6163
console.error('Error during server execution:', error);
64+
process.exit(1);
6265
}
6366
});
6467

src/commander/stopServer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ command
3333
}
3434
} catch (error: any) {
3535
// Handle any errors during the HTTP request
36-
if (error.code === 'ECONNABORTED') {
36+
if (error && error.code === 'ECONNABORTED') {
3737
console.error(chalk.red('Error: SmartUI server did not respond in 15 seconds'));
38+
} if (error && error.code === 'ECONNREFUSED') {
39+
console.error(chalk.red('Error: Looks like smartui cli server is already stopped'));
3840
} else {
3941
console.error(chalk.red('Error while stopping server'));
4042
}

src/lib/schemaValidation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ const ConfigSchema = {
239239
type: "string",
240240
errorMessage: "Invalid config; logFile should be a string value"
241241
},
242+
environment: {
243+
type: "string",
244+
enum: ["stage", "prod"],
245+
errorMessage: "Invalid config; environment should be a string value either stage or prod"
246+
}
242247
},
243248
required: ["type"],
244249
additionalProperties: false

src/lib/server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { readFileSync, truncate } from 'fs'
55
import { Context } from '../types.js'
66
import { validateSnapshot } from './schemaValidation.js'
77
import { pingIntervalId } from './utils.js';
8-
import { startPolling } from './utils.js';
8+
import { stopTunnelHelper } from './utils.js';
99

1010
const uploadDomToS3ViaEnv = process.env.USE_LAMBDA_INTERNAL || false;
1111
export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMessage, ServerResponse>> => {
@@ -151,6 +151,11 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
151151
}
152152
}
153153

154+
//Handle Tunnel closure
155+
if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
156+
await stopTunnelHelper(ctx)
157+
}
158+
154159
await ctx.browser?.close();
155160
if (ctx.server){
156161
ctx.server.close();
@@ -168,7 +173,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
168173
replyCode = 500;
169174
replyBody = { error: { message: error.message } };
170175
}
171-
176+
172177
// Step 5: Return the response
173178
return reply.code(replyCode).send(replyBody);
174179
});

src/lib/utils.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo
324324

325325
export let pingIntervalId: NodeJS.Timeout | null = null;
326326

327-
export async function startPingPolling(ctx: Context): Promise<void> {
327+
export async function startPingPolling(ctx: Context, event: string): Promise<void> {
328328
try {
329329
ctx.log.debug('Sending initial ping to server...');
330330
await ctx.client.ping(ctx.build.id, ctx.log);
@@ -336,9 +336,9 @@ export async function startPingPolling(ctx: Context): Promise<void> {
336336
// Start the polling interval
337337
pingIntervalId = setInterval(async () => {
338338
try {
339-
ctx.log.debug('Sending ping to server...');
339+
ctx.log.debug('Sending ping to server...'+ event);
340340
await ctx.client.ping(ctx.build.id, ctx.log);
341-
ctx.log.debug('Ping sent successfully.');
341+
ctx.log.debug('Ping sent successfully.'+ event);
342342
} catch (error: any) {
343343
ctx.log.error(`Error during ping polling: ${error.message}`);
344344
}
@@ -390,6 +390,11 @@ export async function startTunnelBinary(ctx: Context) {
390390
ctx.config.tunnel.tunnelName = randomTunnelName
391391
}
392392

393+
if (tunnelConfig?.environment) {
394+
tunnelArguments.environment = tunnelConfig.environment
395+
}
396+
397+
393398
ctx.log.debug(`tunnel config ${JSON.stringify(tunnelArguments)}`)
394399

395400
if (ctx.config.tunnel?.type === 'auto') {
@@ -434,7 +439,7 @@ export async function startPollingForTunnel(ctx: Context, build_id: string, base
434439

435440
const status = await tunnelInstance.stop();
436441
ctx.log.debug('Tunnel is Stopped ? ' + status);
437-
442+
return;
438443
}
439444
} catch (error: any) {
440445
if (error.message.includes('ENOTFOUND')) {
@@ -450,10 +455,10 @@ export async function startPollingForTunnel(ctx: Context, build_id: string, base
450455

451456
export async function stopTunnelHelper(ctx: Context) {
452457
const tunnelRunningStatus = await tunnelInstance.isRunning();
453-
ctx.log.debug('Running status of tunnel before stopping ? ' + tunnelRunningStatus);
458+
ctx.log.debug('stop-tunnel:: Running status of tunnel before stopping ? ' + tunnelRunningStatus);
454459

455460
const status = await tunnelInstance.stop();
456-
ctx.log.debug('Tunnel is Stopped ? ' + status);
461+
ctx.log.debug('stop-tunnel:: Tunnel is Stopped ? ' + status);
457462
}
458463

459464
/**

src/tasks/createBuildExec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
3636
}
3737
task.output = chalk.gray(`build id: ${resp.data.buildId}`);
3838
task.title = 'SmartUI build created'
39+
if (ctx.env.USE_REMOTE_DISCOVERY){
40+
task.output += chalk.gray(`\n Using remote discovery for this build`);
41+
}
3942
} else {
4043
task.output = chalk.gray(`Empty PROJECT_TOKEN and PROJECT_NAME. Skipping Creation of Build!`)
4144
task.title = 'Skipped SmartUI build creation'
@@ -45,9 +48,10 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
4548
}
4649

4750
if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
48-
startPingPolling(ctx);
4951
if (ctx.build && ctx.build.id) {
5052
startPollingForTunnel(ctx, '', false, '');
53+
} else {
54+
startPingPolling(ctx, "tunnel-process");
5155
}
5256
}
5357

@@ -61,6 +65,14 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
6165
tunnelName: tunnelResp.data.tunnel_name
6266
}
6367
ctx.log.debug(`Tunnel Details: ${JSON.stringify(ctx.tunnelDetails)}`)
68+
//USE_REMOTE_DISCOVERY as default if Tunnel is true
69+
if (process.env.USE_REMOTE_DISCOVERY === undefined) {
70+
ctx.env.USE_REMOTE_DISCOVERY = true;
71+
process.env.USE_REMOTE_DISCOVERY = 'true';
72+
task.output += chalk.gray(`\n Using remote discovery by deafult for this build`);
73+
}
74+
ctx.log.debug(`USE_REMOTE_DISCOVERY is set to ${ctx.env.USE_REMOTE_DISCOVERY}`);
75+
6476
} else if (tunnelResp && tunnelResp.error) {
6577
if (tunnelResp.error.message) {
6678
if (tunnelResp.error.code && tunnelResp.error.code === 400) {

src/tasks/finalizeBuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
3030

3131
if (pingIntervalId !== null) {
3232
clearInterval(pingIntervalId);
33-
ctx.log.debug('Ping polling stopped immediately.');
33+
ctx.log.debug('Ping polling stopped immediately from Finalize Build');
3434
}
3535

3636
for (const [sessionId, capabilities] of ctx.sessionCapabilitiesMap.entries()) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export interface tunnelConfig {
232232
dir: string;
233233
v: boolean;
234234
logFile: string;
235+
environment:string;
235236
}
236237

237238
export interface FigmaWebConfig {

0 commit comments

Comments
 (0)