convex
Version:
Client for the Convex Cloud
190 lines (189 loc) • 6.15 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var envvars_exports = {};
__export(envvars_exports, {
offerToWriteToEnv: () => offerToWriteToEnv,
suggestedEnvVarName: () => suggestedEnvVarName
});
module.exports = __toCommonJS(envvars_exports);
var import_chalk = __toESM(require("chalk"));
var dotenv = __toESM(require("dotenv"));
var import_inquirer = __toESM(require("inquirer"));
var import_utils = require("./utils");
async function suggestedEnvVarName(ctx) {
if (!ctx.fs.exists("package.json")) {
return {
envVar: "CONVEX_URL"
};
}
const packages = await (0, import_utils.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"
]);
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(
import_chalk.default.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(
import_chalk.default.green(`${envVar3} in ${envFile} is already ${value}`)
);
return;
}
if (Object.values(config).filter((v) => v === oldValue).length !== 1) {
import_chalk.default.yellow(`Can't safely modify ${envFile}, please edit manually.`);
return;
}
if (saveUrl === "yes" || (await import_inquirer.default.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(
import_chalk.default.green(`Updated ${envFile} with ${envVar3}="${value}"`)
);
return;
}
console.error(
import_chalk.default.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 import_inquirer.default.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(
import_chalk.default.green(`Updated ${envFile} with ${envVar2}="${value}"`)
);
}
return;
}
const { appearToBeUsing, envVar } = await suggestedEnvVarName(ctx);
if (appearToBeUsing) {
console.error(import_chalk.default.green(`Detected ${appearToBeUsing} project.`));
}
if (saveUrl === "yes" || (await import_inquirer.default.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(import_chalk.default.green(`Wrote ${envFile} file to save ${envVar}.`));
return;
}
}
//# sourceMappingURL=envvars.js.map