UNPKG

@reliverse/rse-sdk

Version:

@reliverse/rse-sdk allows you to create new plugins for @reliverse/rse CLI, interact with reliverse.org, and even extend your own CLI functionality (you may also try @reliverse/dler-sdk for this case).

70 lines (69 loc) 2.45 kB
import { re } from "@reliverse/relico"; import { relinka } from "@reliverse/relinka"; import { confirmPrompt } from "@reliverse/rempts"; import { projectsCreateProjectEnv } from "@vercel/sdk/funcs/projectsCreateProjectEnv"; import { getVercelEnvVar, withRateLimit } from "./vercel-api.js"; import { getEnvVars } from "./vercel-utils.js"; export async function addEnvVarsToVercelProject(vercelInstance, projectName, projectPath, selectedOptions) { relinka("info", "Setting up environment variables..."); const envVars = await getEnvVars(projectPath); if (envVars.length === 0) { return; } if (selectedOptions.useSharedEnvVars) { const shouldUseShared = await confirmPrompt({ title: `Would you like to use shared environment variables from Vercel.com? ${re.red("[\u{1F6A8} Experimental]")}`, content: "Only missing variables will be uploaded from your local .env file", defaultValue: false }); if (shouldUseShared) { const existingEnvVars = await getVercelEnvVar( vercelInstance, projectName, envVars[0]?.key ?? "" ); const newEnvVars = envVars.filter( (env) => !existingEnvVars?.key.includes(env.key) ); if (newEnvVars.length > 0) { await uploadEnvVars(vercelInstance, projectName, newEnvVars); relinka( "success", `Added ${newEnvVars.length} new environment variables` ); if (existingEnvVars) { relinka( "info", `Kept ${existingEnvVars.key} existing shared variables` ); } } else { relinka( "info", "All required environment variables are already set in Vercel" ); } } else { await uploadEnvVars(vercelInstance, projectName, envVars); relinka("verbose", "Environment variables added successfully"); } } else { await uploadEnvVars(vercelInstance, projectName, envVars); relinka("verbose", "Environment variables added successfully"); } } async function uploadEnvVars(vercelInstance, projectName, envVars) { await withRateLimit(async () => { const res = await projectsCreateProjectEnv(vercelInstance, { idOrName: projectName, upsert: "true", requestBody: envVars.map((env) => ({ ...env, target: env.target ?? ["production", "preview", "development"] })) }); if (!res.ok) { throw res.error; } }); }