CLI dev command is creating a tunnel and attempting to connect to the API
This commit is contained in:
@@ -3,23 +3,32 @@ import pathModule from "path";
|
||||
import { detectNextJsProject } from "../utils/detectNextJsProject.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { resolvePath } from "../utils/parseNameAndPath.js";
|
||||
import ngrok from "ngrok";
|
||||
import { z } from "zod";
|
||||
import { TriggerApi } from "../utils/triggerApi.js";
|
||||
|
||||
export type DevCommandOptions = {
|
||||
port: string;
|
||||
envFile: string;
|
||||
tunnel: "ngrok" | "localtunnel";
|
||||
};
|
||||
export const DevCommandOptionsSchema = z.object({
|
||||
port: z.coerce.number(),
|
||||
envFile: z.string(),
|
||||
tunnel: z.union([z.literal("ngrok"), z.literal("localtunnel")]),
|
||||
});
|
||||
|
||||
export async function devCommand(path: string, anyOptions: any) {
|
||||
const result = DevCommandOptionsSchema.safeParse(anyOptions);
|
||||
if (!result.success) {
|
||||
logger.error(result.error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
const options = result.data;
|
||||
|
||||
export async function devCommand(path: string, options: DevCommandOptions) {
|
||||
console.log("devCommand", path, options);
|
||||
const resolvedPath = resolvePath(path);
|
||||
// Detect if are are in a Next.js project
|
||||
const isNextJsProject = await detectNextJsProject(resolvedPath);
|
||||
|
||||
if (!isNextJsProject) {
|
||||
logger.error("You must run this command in a Next.js project.");
|
||||
process.exit(1);
|
||||
}
|
||||
logger.success(`✅ Detected valid Next.js project at ${resolvedPath}`);
|
||||
|
||||
// Read from package.json to get the endpointId
|
||||
const endpointId = await getEndpointIdFromPackageJson(resolvedPath);
|
||||
@@ -29,18 +38,29 @@ export async function devCommand(path: string, options: DevCommandOptions) {
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
logger.success(`✅ Detected TriggerClient id: ${endpointId}`);
|
||||
|
||||
// Read from .env.local to get the TRIGGER_API_KEY and TRIGGER_API_URL
|
||||
const { apiKey, apiUrl } = await getTriggerApiDetails(
|
||||
resolvedPath,
|
||||
options.envFile
|
||||
);
|
||||
console.log("apiKey", apiKey);
|
||||
console.log("apiUrl", apiUrl);
|
||||
logger.success(`✅ Found API Key in ${options.envFile} file`);
|
||||
const apiClient = new TriggerApi(apiKey, apiUrl);
|
||||
|
||||
// Setup tunnel
|
||||
const tunnelUrl = await createTunnel(options.port);
|
||||
logger.success(`🚇 Created tunnel: ${tunnelUrl}`);
|
||||
|
||||
logger.info(`Connecting to Trigger.dev...`);
|
||||
//wait 200ms
|
||||
// await wait(200);
|
||||
|
||||
//do initial refresh of the endpoint
|
||||
//todo get path for API
|
||||
await refreshEndpoint(apiClient, endpointId, tunnelUrl);
|
||||
logger.success(`🔄 Updated your Jobs`);
|
||||
|
||||
// Call triggerApi.registerEndpoint
|
||||
// Watch for changes to .ts files and call triggerApi.indexEndpoint
|
||||
}
|
||||
|
||||
@@ -49,7 +69,10 @@ async function getEndpointIdFromPackageJson(path: string) {
|
||||
const pkgBuffer = await fs.readFile(pkgJsonPath);
|
||||
const pkgJson = JSON.parse(pkgBuffer.toString());
|
||||
|
||||
return pkgJson["trigger.dev"]?.endpointId;
|
||||
const value = pkgJson["trigger.dev"]?.endpointId;
|
||||
if (!value || typeof value !== "string") return;
|
||||
|
||||
return value as string;
|
||||
}
|
||||
|
||||
async function getTriggerApiDetails(path: string, envFile: string) {
|
||||
@@ -86,3 +109,37 @@ async function getTriggerApiDetails(path: string, envFile: string) {
|
||||
|
||||
return { apiKey, apiUrl };
|
||||
}
|
||||
|
||||
async function createTunnel(port: number) {
|
||||
try {
|
||||
return await ngrok.connect(port);
|
||||
} catch (e) {
|
||||
logger.error(`Ngrok failed to create a tunnel for port ${port}.\n${e}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshEndpoint(
|
||||
apiClient: TriggerApi,
|
||||
endpointId: string,
|
||||
tunnelUrl: string
|
||||
) {
|
||||
const response = await apiClient.registerEndpoint({
|
||||
id: endpointId,
|
||||
url: `${tunnelUrl}/api/trigger`,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Endpoint couldn't refresh: ${response.error}`);
|
||||
// process.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
function wait(duration: number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, duration);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user