UNPKG

trigger.dev

Version:

A Command-Line Interface for Trigger.dev projects

79 lines 2.94 kB
import { CliApiClient } from "../apiClient.js"; import { readAuthConfigProfile } from "./configFiles.js"; import { logger } from "./logger.js"; export async function isLoggedIn(profile = "default") { try { const config = readAuthConfigProfile(profile); if (!config?.accessToken || !config?.apiUrl) { return { ok: false, error: "You must login first" }; } const apiClient = new CliApiClient(config.apiUrl, config.accessToken); const userData = await apiClient.whoAmI(); if (!userData.success) { return { ok: false, error: userData.error, auth: { apiUrl: config.apiUrl, accessToken: config.accessToken, tokenType: "personal", }, }; } return { ok: true, profile, userId: userData.data.userId, email: userData.data.email, dashboardUrl: userData.data.dashboardUrl, auth: { apiUrl: config.apiUrl, accessToken: config.accessToken, tokenType: "personal", }, }; } catch (e) { return { ok: false, error: e instanceof Error ? e.message : "Unknown error", }; } } export async function getProjectClient(options) { logger.debug(`Initializing ${options.env} environment for project ${options.projectRef}`, options.apiUrl); const apiClient = new CliApiClient(options.apiUrl, options.accessToken); const projectEnv = await apiClient.getProjectEnv({ projectRef: options.projectRef, env: options.env, }); if (!projectEnv.success) { if (projectEnv.error === "Project not found") { logger.error(`Project not found: ${options.projectRef}. Ensure you are using the correct project ref and CLI profile (use --profile). Currently using the "${options.profile}" profile, which points to ${options.apiUrl}`); } else { logger.error(`Failed to initialize ${options.env} environment: ${projectEnv.error}. Using project ref ${options.projectRef}`); } return; } const client = new CliApiClient(projectEnv.data.apiUrl, projectEnv.data.apiKey, options.branch); return { id: projectEnv.data.projectId, name: projectEnv.data.name, client, }; } export async function upsertBranch(options) { const apiClient = new CliApiClient(options.apiUrl, options.accessToken); const branchEnv = await apiClient.upsertBranch(options.projectRef, { env: "preview", branch: options.branch, git: options.gitMeta, }); if (!branchEnv.success) { logger.error(`Failed to upsert branch: ${branchEnv.error}`); return; } return branchEnv.data; } //# sourceMappingURL=session.js.map