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
7 changes: 5 additions & 2 deletions src/commander/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -27,12 +28,13 @@ command
ctx.snapshotQueue = new snapshotQueue(ctx);
ctx.totalSnapshots = 0
ctx.isStartExec = true

let tasks = new Listr<Context>(
[
authExec(ctx),
startServer(ctx),
getGitInfo(ctx),
...(ctx.config.tunnel && ctx.config.tunnel?.type === 'auto' ? [startTunnel(ctx)] : []),
createBuildExec(ctx),

],
Expand All @@ -51,14 +53,15 @@ 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, '')
}

} catch (error) {
console.error('Error during server execution:', error);
process.exit(1);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/commander/stopServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib/schemaValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FastifyInstance<Server, IncomingMessage, ServerResponse>> => {
Expand Down Expand Up @@ -151,6 +151,11 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
}
}

//Handle Tunnel closure
if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
await stopTunnelHelper(ctx)
}

await ctx.browser?.close();
if (ctx.server){
ctx.server.close();
Expand All @@ -168,7 +173,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
replyCode = 500;
replyBody = { error: { message: error.message } };
}

// Step 5: Return the response
return reply.code(replyCode).send(replyBody);
});
Expand Down
17 changes: 11 additions & 6 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo

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

export async function startPingPolling(ctx: Context): Promise<void> {
export async function startPingPolling(ctx: Context, event: string): Promise<void> {
try {
ctx.log.debug('Sending initial ping to server...');
await ctx.client.ping(ctx.build.id, ctx.log);
Expand All @@ -336,9 +336,9 @@ export async function startPingPolling(ctx: Context): Promise<void> {
// 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}`);
}
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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')) {
Expand All @@ -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);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/tasks/createBuildExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
}
task.output = chalk.gray(`build id: ${resp.data.buildId}`);
task.title = 'SmartUI build created'
if (ctx.env.USE_REMOTE_DISCOVERY){
task.output += chalk.gray(`\n Using remote discovery for this build`);
}
} else {
task.output = chalk.gray(`Empty PROJECT_TOKEN and PROJECT_NAME. Skipping Creation of Build!`)
task.title = 'Skipped SmartUI build creation'
Expand All @@ -45,9 +48,10 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
}

if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
startPingPolling(ctx);
if (ctx.build && ctx.build.id) {
startPollingForTunnel(ctx, '', false, '');
} else {
startPingPolling(ctx, "tunnel-process");
}
}

Expand All @@ -61,6 +65,14 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
tunnelName: tunnelResp.data.tunnel_name
}
ctx.log.debug(`Tunnel Details: ${JSON.stringify(ctx.tunnelDetails)}`)
//USE_REMOTE_DISCOVERY as default if Tunnel is true
if (process.env.USE_REMOTE_DISCOVERY === undefined) {
ctx.env.USE_REMOTE_DISCOVERY = true;
process.env.USE_REMOTE_DISCOVERY = 'true';
task.output += chalk.gray(`\n Using remote discovery by deafult for this build`);
}
ctx.log.debug(`USE_REMOTE_DISCOVERY is set to ${ctx.env.USE_REMOTE_DISCOVERY}`);

} else if (tunnelResp && tunnelResp.error) {
if (tunnelResp.error.message) {
if (tunnelResp.error.code && tunnelResp.error.code === 400) {
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/finalizeBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen

if (pingIntervalId !== null) {
clearInterval(pingIntervalId);
ctx.log.debug('Ping polling stopped immediately.');
ctx.log.debug('Ping polling stopped immediately from Finalize Build');
}

for (const [sessionId, capabilities] of ctx.sessionCapabilitiesMap.entries()) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export interface tunnelConfig {
dir: string;
v: boolean;
logFile: string;
environment:string;
}

export interface FigmaWebConfig {
Expand Down