convex
Version:
Client for the Convex Cloud
164 lines (163 loc) • 5.31 kB
JavaScript
import * as dotenv from "dotenv";
import { logFailure } from "../../bundler/context.js";
import { changedEnvVarFile, getEnvVarRegex } from "./envvars.js";
import {
CONVEX_DEPLOY_KEY_ENV_VAR_NAME,
readAdminKeyFromEnvVar
} from "./utils.js";
const ENV_VAR_FILE_PATH = ".env.local";
export const CONVEX_DEPLOYMENT_VAR_NAME = "CONVEX_DEPLOYMENT";
export function getTargetDeploymentName() {
return getDeploymentNameFromAdminKey() ?? getConfiguredDeploymentFromEnvVar().name;
}
export function getConfiguredDeploymentFromEnvVar() {
dotenv.config({ path: ENV_VAR_FILE_PATH });
dotenv.config();
const raw = process.env[CONVEX_DEPLOYMENT_VAR_NAME] ?? null;
if (raw === null || raw === "") {
return { type: null, name: null };
}
const name = stripDeploymentTypePrefix(raw);
const type = getDeploymentTypeFromConfiguredDeployment(raw);
return { type, name };
}
export function stripDeploymentTypePrefix(deployment) {
return deployment.split(":").at(-1);
}
function getDeploymentTypeFromConfiguredDeployment(raw) {
const typeRaw = raw.split(":")[0];
const type = typeRaw === "prod" || typeRaw === "dev" || typeRaw === "preview" ? typeRaw : null;
return type;
}
export async function writeDeploymentEnvVar(ctx, deploymentType, deployment) {
const existingFile = ctx.fs.exists(ENV_VAR_FILE_PATH) ? ctx.fs.readUtf8File(ENV_VAR_FILE_PATH) : null;
const changedFile = changesToEnvVarFile(
existingFile,
deploymentType,
deployment
);
process.env[CONVEX_DEPLOYMENT_VAR_NAME] = deploymentType + ":" + deployment.deploymentName;
if (changedFile !== null) {
ctx.fs.writeUtf8File(ENV_VAR_FILE_PATH, changedFile);
return { wroteToGitIgnore: await gitIgnoreEnvVarFile(ctx) };
}
return { wroteToGitIgnore: false };
}
export async function eraseDeploymentEnvVar(ctx) {
const existingFile = ctx.fs.exists(ENV_VAR_FILE_PATH) ? ctx.fs.readUtf8File(ENV_VAR_FILE_PATH) : null;
if (existingFile === null) {
return false;
}
const config = dotenv.parse(existingFile);
const existing = config[CONVEX_DEPLOYMENT_VAR_NAME];
if (existing === void 0) {
return false;
}
const changedFile = existingFile.replace(
getEnvVarRegex(CONVEX_DEPLOYMENT_VAR_NAME),
""
);
ctx.fs.writeUtf8File(ENV_VAR_FILE_PATH, changedFile);
return true;
}
async function gitIgnoreEnvVarFile(ctx) {
const gitIgnorePath = ".gitignore";
const gitIgnoreContents = ctx.fs.exists(gitIgnorePath) ? ctx.fs.readUtf8File(gitIgnorePath) : "";
const changedGitIgnore = changesToGitIgnore(gitIgnoreContents);
if (changedGitIgnore !== null) {
ctx.fs.writeUtf8File(gitIgnorePath, changedGitIgnore);
return true;
}
return false;
}
export function changesToEnvVarFile(existingFile, deploymentType, {
team,
project,
deploymentName
}) {
const deploymentValue = deploymentType + ":" + deploymentName;
const commentOnPreviousLine = "# Deployment used by `npx convex dev`";
const commentAfterValue = `team: ${team}, project: ${project}`;
return changedEnvVarFile(
existingFile,
CONVEX_DEPLOYMENT_VAR_NAME,
deploymentValue,
commentAfterValue,
commentOnPreviousLine
);
}
export function changesToGitIgnore(existingFile) {
if (existingFile === null) {
return `${ENV_VAR_FILE_PATH}
`;
}
const gitIgnoreLines = existingFile.split("\n");
const envVarFileIgnored = gitIgnoreLines.some(
(line) => line === ".env.local" || line === ".env.*" || line === ".env*" || line === "*.local" || line === ".env*.local"
);
if (!envVarFileIgnored) {
return `${existingFile}
${ENV_VAR_FILE_PATH}
`;
} else {
return null;
}
}
export function getDeploymentNameFromAdminKey() {
const adminKey = readAdminKeyFromEnvVar();
if (adminKey === void 0) {
return null;
}
return deploymentNameFromAdminKey(adminKey);
}
export async function deploymentNameFromAdminKeyOrCrash(ctx, adminKey) {
const deploymentName = deploymentNameFromAdminKey(adminKey);
if (deploymentName === null) {
logFailure(
ctx,
`Please set ${CONVEX_DEPLOY_KEY_ENV_VAR_NAME} to a new key which you can find on your Convex dashboard.`
);
return await ctx.crash(1);
}
return deploymentName;
}
function deploymentNameFromAdminKey(adminKey) {
const parts = adminKey.split("|");
if (parts.length === 1) {
return null;
}
if (isPreviewDeployKey(adminKey)) {
return null;
}
return stripDeploymentTypePrefix(parts[0]);
}
export function isPreviewDeployKey(adminKey) {
const parts = adminKey.split("|");
if (parts.length === 1) {
return false;
}
const [prefix] = parts;
const prefixParts = prefix.split(":");
return prefixParts[0] === "preview" && prefixParts.length === 3;
}
export function deploymentTypeFromAdminKey(adminKey) {
const parts = adminKey.split(":");
if (parts.length === 1) {
return "prod";
}
return parts.at(0);
}
export async function getTeamAndProjectFromPreviewAdminKey(ctx, adminKey) {
const parts = adminKey.split("|")[0].split(":");
if (parts.length !== 3) {
logFailure(
ctx,
"Malformed preview CONVEX_DEPLOY_KEY, get a new key from Project Settings."
);
return await ctx.crash(1);
}
const [_preview, teamSlug, projectSlug] = parts;
return { teamSlug, projectSlug };
}
//# sourceMappingURL=deployment.js.map
;