convex
Version:
Client for the Convex Cloud
161 lines (160 loc) • 4.65 kB
JavaScript
;
import chalk from "chalk";
import * as dotenv from "dotenv";
import inquirer from "inquirer";
import { loadPackageJson } from "./utils";
export async function suggestedEnvVarName(ctx) {
if (!ctx.fs.exists("package.json")) {
return {
envVar: "CONVEX_URL"
};
}
const packages = await loadPackageJson(ctx);
const isCreateReactApp = !!packages.filter(
({ name }) => name === "react-scripts"
).length;
if (isCreateReactApp) {
return {
appearToBeUsing: "create-react-app",
envVar: "REACT_APP_CONVEX_URL"
};
}
const isNextJs = !!packages.filter(({ name }) => name === "next").length;
if (isNextJs) {
return {
appearToBeUsing: "Next.js",
envVar: "NEXT_PUBLIC_CONVEX_URL"
};
}
const isVite = !!packages.filter(({ name }) => name === "vite");
if (isVite) {
return {
appearToBeUsing: "Vite",
envVar: "VITE_CONVEX_URL"
};
}
return {
envVar: "CONVEX_URL"
};
}
function suggestedProdEnvFile(ctx) {
if (ctx.fs.exists(".env.production")) {
return {
existing: true,
envFile: ".env.production"
};
}
if (ctx.fs.exists(".env")) {
return {
existing: true,
envFile: ".env"
};
}
return {
existing: false,
envFile: ".env"
};
}
function suggestedDevEnvFile(ctx) {
return {
existing: ctx.fs.exists(".env.local"),
envFile: ".env.local"
};
}
const EXPECTED_NAMES = /* @__PURE__ */ new Set([
"CONVEX_URL",
"NEXT_PUBLIC_CONVEX_URL",
"VITE_CONVEX_URL",
"REACT_APP_CONVEX_URL"
]);
export async function offerToWriteToEnv(ctx, type, value, saveUrl = "ask") {
if (saveUrl === "no")
return;
const { envFile, existing } = type === "dev" ? suggestedDevEnvFile(ctx) : suggestedProdEnvFile(ctx);
if (existing) {
const config = dotenv.parse(ctx.fs.readUtf8File(envFile));
console.error(`Found ${envFile} file.`);
const matching = Object.keys(config).filter((key) => EXPECTED_NAMES.has(key));
if (matching.length > 1) {
console.error(
chalk.yellow(
`Found multiple CONVEX_URL environment variables in ${envFile} so cannot update automatically.`
)
);
return;
}
if (matching.length === 1) {
const [envVar3, oldValue] = [matching[0], config[matching[0]]];
if (oldValue === value) {
console.error(
chalk.green(`${envVar3} in ${envFile} is already ${value}`)
);
return;
}
if (Object.values(config).filter((v) => v === oldValue).length !== 1) {
chalk.yellow(`Can't safely modify ${envFile}, please edit manually.`);
return;
}
if (saveUrl === "yes" || (await inquirer.prompt([
{
type: "confirm",
name: "updateEnvFile",
message: `Update ${envVar3} in ${envFile} to ${value} to save ${type} URL?`,
default: true
}
])).updateEnvFile) {
const modified = ctx.fs.readUtf8File(envFile).replace(oldValue, value);
ctx.fs.writeUtf8File(envFile, modified);
console.error(
chalk.green(`Updated ${envFile} with ${envVar3}="${value}"`)
);
return;
}
console.error(
chalk.yellow(`Please update ${envFile} with ${envVar3}="${value}"`)
);
return;
}
const { appearToBeUsing: appearToBeUsing2, envVar: envVar2 } = await suggestedEnvVarName(ctx);
if (appearToBeUsing2) {
console.error(`Detected ${appearToBeUsing2} project.`);
}
if (saveUrl === "yes" || (await inquirer.prompt([
{
type: "confirm",
name: "updateEnvFile",
message: `Add ${envVar2} to ${envFile} to save ${type} URL?`,
default: true
}
])).updateEnvFile) {
const orig = ctx.fs.readUtf8File(envFile);
const modified = `${orig}
${envVar2}="${value}"
`;
ctx.fs.writeUtf8File(envFile, modified);
console.error(
chalk.green(`Updated ${envFile} with ${envVar2}="${value}"`)
);
}
return;
}
const { appearToBeUsing, envVar } = await suggestedEnvVarName(ctx);
if (appearToBeUsing) {
console.error(chalk.green(`Detected ${appearToBeUsing} project.`));
}
if (saveUrl === "yes" || (await inquirer.prompt([
{
type: "confirm",
name: "updateEnvFile",
message: `Create a ${envFile} file and add ${envVar} to save ${type} URL?`,
default: true
}
])).updateEnvFile) {
const contents = `${envVar}="${value}"
`;
ctx.fs.writeUtf8File(envFile, contents);
console.error(chalk.green(`Wrote ${envFile} file to save ${envVar}.`));
return;
}
}
//# sourceMappingURL=envvars.js.map