Skip to content

Commit d395b95

Browse files
Chigalaericallam
andauthored
fix: Handle ngrok config upgrade error in createTunnel function (#295)
* fix: Handle ngrok config upgrade error in createTunnel function * Improved the output when upgrading the ngrok configuration * Create three-flies-sneeze.md --------- Co-authored-by: Eric Allam <[email protected]>
1 parent f3bda59 commit d395b95

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

.changeset/three-flies-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/cli": patch
3+
---
4+
5+
fix: Handle ngrok config upgrade error in createTunnel function

packages/cli/src/commands/dev.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
import childProcess from "child_process";
12
import chokidar from "chokidar";
23
import dotenv from "dotenv";
34
import fs from "fs/promises";
45
import ngrok from "ngrok";
56
import fetch from "node-fetch";
6-
import ora from "ora";
7+
import ora, { Ora } from "ora";
78
import pathModule from "path";
9+
import util from "util";
810
import { z } from "zod";
11+
import { CLOUD_API_URL } from "../consts.js";
912
import { telemetryClient } from "../telemetry/telemetry.js";
1013
import { pathExists, readFile } from "../utils/fileSystem.js";
1114
import { logger } from "../utils/logger.js";
1215
import { resolvePath } from "../utils/parseNameAndPath.js";
1316
import { TriggerApi } from "../utils/triggerApi.js";
14-
import { CLOUD_API_URL } from "../consts.js";
17+
18+
const asyncExecFile = util.promisify(childProcess.execFile);
1519

1620
export const DevCommandOptionsSchema = z.object({
1721
port: z.coerce.number(),
@@ -282,23 +286,48 @@ async function resolveEndpointUrl(apiUrl: string, port: number) {
282286

283287
// Setup tunnel
284288
const tunnelSpinner = ora(`🚇 Creating tunnel`).start();
285-
const tunnelUrl = await createTunnel(port);
289+
290+
const tunnelUrl = await createTunnel(port, tunnelSpinner);
291+
286292
if (tunnelUrl) {
287293
tunnelSpinner.succeed(`🚇 Created tunnel: ${tunnelUrl}`);
288294
}
289295

290296
return tunnelUrl;
291297
}
292298

293-
async function createTunnel(port: number) {
299+
async function createTunnel(port: number, spinner: Ora) {
294300
try {
295301
return await ngrok.connect(port);
296-
} catch (e) {
297-
logger.error(`Ngrok failed to create a tunnel for port ${port}.\n${e}`);
302+
} catch (error: any) {
303+
if (
304+
typeof error.message === "string" &&
305+
error.message.includes("`version` property is required")
306+
) {
307+
await upgradeNgrokConfig(spinner);
308+
309+
try {
310+
return await ngrok.connect(port);
311+
} catch (retryError) {
312+
spinner.fail(
313+
`Ngrok failed to create a tunnel for port ${port} after configuration upgrade.\n${retryError}`
314+
);
315+
return;
316+
}
317+
}
298318
return;
299319
}
300320
}
301321

322+
async function upgradeNgrokConfig(spinner: Ora) {
323+
try {
324+
await asyncExecFile("ngrok", ["config", "upgrade"]);
325+
spinner.info("Ngrok configuration upgraded successfully.");
326+
} catch (error) {
327+
spinner.fail(`Failed to upgrade ngrok configuration.\n${error}`);
328+
}
329+
}
330+
302331
async function refreshEndpoint(apiClient: TriggerApi, endpointId: string, endpointUrl: string) {
303332
try {
304333
const response = await apiClient.registerEndpoint({

0 commit comments

Comments
 (0)