Feat: Detect if a Next.js project is running on the specified port before opening a tunnel (#214)

* Feat: Detect nextjs project

* Add check before opening tunnel

* Update dev.ts

* Add changeset
This commit is contained in:
Ashutosh Bhadauriya
2023-07-31 21:13:44 +05:30
committed by GitHub
parent 463cbed7df
commit efd590976c
2 changed files with 23 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/cli": patch
---
Detect if a nextjs project is running on the specified port before opening tunnel
+18 -2
View File
@@ -9,6 +9,7 @@ import { logger } from "../utils/logger.js";
import { resolvePath } from "../utils/parseNameAndPath.js"; import { resolvePath } from "../utils/parseNameAndPath.js";
import { TriggerApi } from "../utils/triggerApi.js"; import { TriggerApi } from "../utils/triggerApi.js";
import dotenv from "dotenv"; import dotenv from "dotenv";
import fetch from "node-fetch";
export const DevCommandOptionsSchema = z.object({ export const DevCommandOptionsSchema = z.object({
port: z.coerce.number(), port: z.coerce.number(),
@@ -48,7 +49,7 @@ export async function devCommand(path: string, anyOptions: any) {
logger.success(`✔️ [trigger.dev] Detected TriggerClient id: ${endpointId}`); logger.success(`✔️ [trigger.dev] Detected TriggerClient id: ${endpointId}`);
// Read from .env.local or .env to get the TRIGGER_API_KEY and TRIGGER_API_URL // Read from .env.local or .env to get the TRIGGER_API_KEY and TRIGGER_API_URL
const { apiUrl, envFile } = await getTriggerApiDetails( const { apiUrl, envFile, apiKey } = await getTriggerApiDetails(
resolvedPath, resolvedPath,
options.envFile options.envFile
); );
@@ -59,9 +60,24 @@ export async function devCommand(path: string, anyOptions: any) {
` [trigger.dev] Looking for Next.js site on port ${options.port}` ` [trigger.dev] Looking for Next.js site on port ${options.port}`
); );
const localEndpointHandlerUrl = `http://localhost:${options.port}${options.handlerPath}`;
try {
await fetch(localEndpointHandlerUrl, {
method: "HEAD",
headers: {
"x-trigger-api-key": apiKey,
"x-trigger-action": "PING",
"x-trigger-endpoint-id": endpointId,
},
});
} catch (err) {
logger.error(`❌ [trigger.dev] No server found on port ${options.port}.`);
process.exit(1);
}
// Setup tunnel // Setup tunnel
const endpointUrl = await resolveEndpointUrl(apiUrl, options.port); const endpointUrl = await resolveEndpointUrl(apiUrl, options.port);
const endpointHandlerUrl = `${endpointUrl}${options.handlerPath}`; const endpointHandlerUrl = `${endpointUrl}${options.handlerPath}`;
const connectingSpinner = ora( const connectingSpinner = ora(