Skip to content

Commit 940af2f

Browse files
start tunnel as a task
1 parent 171122f commit 940af2f

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

src/commander/exec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import exec from '../tasks/exec.js'
1111
import processSnapshots from '../tasks/processSnapshot.js'
1212
import finalizeBuild from '../tasks/finalizeBuild.js'
1313
import snapshotQueue from '../lib/snapshotQueue.js'
14+
import startTunnel from '../tasks/startTunnel.js'
1415

1516
const command = new Command();
1617

@@ -42,6 +43,7 @@ command
4243
authExec(ctx),
4344
startServer(ctx),
4445
getGitInfo(ctx),
46+
...(ctx.config.tunnel && ctx.config.tunnel?.type === 'auto' ? [startTunnel(ctx)] : []),
4547
createBuildExec(ctx),
4648
exec(ctx),
4749
processSnapshots(ctx),

src/lib/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,12 @@ export async function startTunnelBinary(ctx: Context) {
384384
if (tunnelConfig?.tunnelName) {
385385
tunnelArguments.tunnelName = tunnelConfig.tunnelName
386386
} else {
387-
const randomNumber = Math.floor(100000 + Math.random() * 900000);
387+
const randomNumber = Math.floor(1000000 + Math.random() * 9000000);
388388
let randomTunnelName = `smartui-cli-Node-tunnel-${randomNumber}`
389389
tunnelArguments.tunnelName = randomTunnelName;
390390
ctx.config.tunnel.tunnelName = randomTunnelName
391391
}
392+
tunnelArguments.environment = 'stage'
392393

393394
ctx.log.debug(`tunnel config ${JSON.stringify(tunnelArguments)}`)
394395

@@ -452,10 +453,8 @@ export async function stopTunnelHelper(ctx: Context) {
452453
const tunnelRunningStatus = await tunnelInstance.isRunning();
453454
ctx.log.debug('Running status of tunnel before stopping ? ' + tunnelRunningStatus);
454455

455-
if (tunnelRunningStatus) {
456-
const status = await tunnelInstance.stop();
457-
ctx.log.debug('Tunnel is Stopped ? ' + status);
458-
}
456+
const status = await tunnelInstance.stop();
457+
ctx.log.debug('Tunnel is Stopped ? ' + status);
459458
}
460459

461460

src/tasks/createBuildExec.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1010
task: async (ctx, task): Promise<void> => {
1111
updateLogContext({task: 'createBuild'});
1212

13-
let errorCode = 0;
1413
try {
15-
if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
16-
try {
17-
await startTunnelBinary(ctx);
18-
ctx.isStartExec = true;
19-
} catch (error: any) {
20-
ctx.log.debug(`Error starting the tunnel: ${error.message}`);
21-
errorCode = 1;
22-
throw new Error(`Error while starting tunnel binary: ${error.message}`);
23-
}
24-
}
25-
2614
if (ctx.authenticatedInitially && !ctx.config.skipBuildCreation) {
2715
let resp = await ctx.client.createBuild(ctx.git, ctx.config, ctx.log, ctx.build.name, ctx.isStartExec);
2816
ctx.build = {
@@ -78,13 +66,8 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
7866
if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
7967
await stopTunnelHelper(ctx)
8068
}
81-
if (errorCode === 1) {
82-
task.output = chalk.gray(error.message);
83-
throw new Error('Skipped SmartUI build creation');
84-
} else {
85-
task.output = chalk.gray(error.message);
86-
throw new Error('SmartUI build creation failed');
87-
}
69+
task.output = chalk.gray(error.message);
70+
throw new Error('SmartUI build creation failed');
8871
}
8972
},
9073
rendererOptions: { persistentOutput: true }

src/tasks/startTunnel.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ListrTask, ListrRendererFactory } from 'listr2';
2+
import { Context } from '../types.js'
3+
import chalk from 'chalk';
4+
import { updateLogContext } from '../lib/logger.js';
5+
import { startTunnelBinary } from '../lib/utils.js';
6+
7+
export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRendererFactory> => {
8+
return {
9+
title: `Starting Tunnel`,
10+
task: async (ctx, task): Promise<void> => {
11+
updateLogContext({task: 'startTunnel'});
12+
13+
try {
14+
await startTunnelBinary(ctx);
15+
ctx.isStartExec = true;
16+
task.title = 'Tunnel Started';
17+
task.output = 'Tunnel started successfully';
18+
} catch (error: any) {
19+
ctx.log.debug(error);
20+
task.output = chalk.gray(error.message);
21+
throw new Error('Error while starting tunnel binary');
22+
}
23+
},
24+
rendererOptions: { persistentOutput: true }
25+
}
26+
}

0 commit comments

Comments
 (0)