convex
Version:
Client for the Convex Cloud
306 lines (300 loc) • 12.6 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(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var deploy_exports = {};
__export(deploy_exports, {
deploy: () => deploy
});
module.exports = __toCommonJS(deploy_exports);
var import_chalk = __toESM(require("chalk"), 1);
var import_extra_typings = require("@commander-js/extra-typings");
var import_context = require("../bundler/context.js");
var import_api = require("./lib/api.js");
var import_envvars = require("./lib/envvars.js");
var import_utils = require("./lib/utils/utils.js");
var import_child_process = require("child_process");
var import_run = require("./lib/run.js");
var import_usage = require("./lib/usage.js");
var import_deployment = require("./lib/deployment.js");
var import_components = require("./lib/components.js");
var import_prompts = require("./lib/utils/prompts.js");
const deploy = new import_extra_typings.Command("deploy").summary("Deploy to your prod deployment").description(
"Deploy to your deployment. By default, this deploys to your prod deployment.\n\nDeploys to a preview deployment if the `CONVEX_DEPLOY_KEY` environment variable is set to a Preview Deploy Key."
).option("-v, --verbose", "Show full listing of changes").option(
"--dry-run",
"Print out the generated configuration without deploying to your Convex deployment"
).option("-y, --yes", "Skip confirmation prompt when running locally").addOption(
new import_extra_typings.Option(
"--typecheck <mode>",
`Whether to check TypeScript files with \`tsc --noEmit\` before deploying.`
).choices(["enable", "try", "disable"]).default("try")
).addOption(
new import_extra_typings.Option(
"--codegen <mode>",
"Whether to regenerate code in `convex/_generated/` before pushing."
).choices(["enable", "disable"]).default("enable")
).addOption(
new import_extra_typings.Option(
"--cmd <command>",
"Command to run as part of deploying your app (e.g. `vite build`). This command can depend on the environment variables specified in `--cmd-url-env-var-name` being set."
)
).addOption(
new import_extra_typings.Option(
"--cmd-url-env-var-name <name>",
"Environment variable name to set Convex deployment URL (e.g. `VITE_CONVEX_URL`) when using `--cmd`"
)
).addOption(
new import_extra_typings.Option(
"--preview-run <functionName>",
"Function to run if deploying to a preview deployment. This is ignored if deploying to a production deployment."
)
).addOption(
new import_extra_typings.Option(
"--preview-create <name>",
"The name to associate with this deployment if deploying to a newly created preview deployment. Defaults to the current Git branch name in Vercel, Netlify and Github CI. This is ignored if deploying to a production deployment."
).conflicts("preview-name")
).addOption(
new import_extra_typings.Option(
"--check-build-environment <mode>",
"Whether to check for a non-production build environment before deploying to a production Convex deployment."
).choices(["enable", "disable"]).default("enable").hideHelp()
).addOption(new import_extra_typings.Option("--debug-bundle-path <path>").hideHelp()).addOption(new import_extra_typings.Option("--debug").hideHelp()).addOption(new import_extra_typings.Option("--admin-key <adminKey>").hideHelp()).addOption(new import_extra_typings.Option("--url <url>").hideHelp()).addOption(new import_extra_typings.Option("--write-push-request <writePushRequest>").hideHelp()).addOption(
new import_extra_typings.Option(
"--preview-name <name>",
"[deprecated] Use `--preview-create` instead. The name to associate with this deployment if deploying to a preview deployment."
).hideHelp().conflicts("preview-create")
).showHelpAfterError().action(async (cmdOptions) => {
const ctx = import_context.oneoffContext;
(0, import_api.storeAdminKeyEnvVar)(cmdOptions.adminKey);
const configuredDeployKey = (0, import_utils.readAdminKeyFromEnvVar)() ?? null;
if (cmdOptions.checkBuildEnvironment === "enable" && (0, import_envvars.isNonProdBuildEnvironment)() && configuredDeployKey !== null && (0, import_deployment.deploymentTypeFromAdminKey)(configuredDeployKey) === "prod") {
await ctx.crash({
exitCode: 1,
errorType: "invalid filesystem data",
printedMessage: `Detected a non-production build environment and "${import_utils.CONVEX_DEPLOY_KEY_ENV_VAR_NAME}" for a production Convex deployment.
This is probably unintentional.
`
});
}
await (0, import_usage.usageStateWarning)(ctx);
if (configuredDeployKey !== null && (0, import_deployment.isPreviewDeployKey)(configuredDeployKey)) {
if (cmdOptions.previewName !== void 0) {
await ctx.crash({
exitCode: 1,
errorType: "fatal",
printedMessage: "The `--preview-name` flag has been deprecated in favor of `--preview-create`. Please re-run the command using `--preview-create` instead."
});
}
await deployToNewPreviewDeployment(ctx, {
...cmdOptions,
configuredDeployKey
});
} else {
await deployToExistingDeployment(ctx, cmdOptions);
}
});
async function deployToNewPreviewDeployment(ctx, options) {
const previewName = options.previewCreate ?? (0, import_envvars.gitBranchFromEnvironment)();
if (previewName === null) {
await ctx.crash({
exitCode: 1,
errorType: "fatal",
printedMessage: "`npx convex deploy` to a preview deployment could not determine the preview name. Provide one using `--preview-create`"
});
}
if (options.dryRun) {
(0, import_context.logFinishedStep)(
ctx,
`Would have claimed preview deployment for "${previewName}"`
);
await runCommand(ctx, {
cmdUrlEnvVarName: options.cmdUrlEnvVarName,
cmd: options.cmd,
dryRun: !!options.dryRun,
url: "https://<PREVIEW DEPLOYMENT>.convex.cloud"
});
(0, import_context.logFinishedStep)(
ctx,
`Would have deployed Convex functions to preview deployment for "${previewName}"`
);
if (options.previewRun !== void 0) {
(0, import_context.logMessage)(ctx, `Would have run function "${options.previewRun}"`);
}
return;
}
const data = await (0, import_utils.bigBrainAPI)({
ctx,
method: "POST",
url: "claim_preview_deployment",
data: {
projectSelection: await (0, import_api.projectSelection)(
ctx,
await (0, import_utils.getConfiguredDeploymentName)(ctx),
options.configuredDeployKey
),
identifier: previewName
}
});
const previewAdminKey = data.adminKey;
const previewUrl = data.instanceUrl;
await runCommand(ctx, { ...options, url: previewUrl });
const pushOptions = {
adminKey: previewAdminKey,
verbose: !!options.verbose,
dryRun: false,
typecheck: options.typecheck,
debug: !!options.debug,
debugBundlePath: options.debugBundlePath,
codegen: options.codegen === "enable",
url: previewUrl,
cleanupHandle: null
};
(0, import_context.showSpinner)(ctx, `Deploying to ${previewUrl}...`);
await (0, import_components.runPush)(import_context.oneoffContext, pushOptions);
(0, import_context.logFinishedStep)(ctx, `Deployed Convex functions to ${previewUrl}`);
if (options.previewRun !== void 0) {
await (0, import_run.runFunctionAndLog)(
ctx,
previewUrl,
previewAdminKey,
options.previewRun,
{},
void 0,
{
onSuccess: () => {
(0, import_context.logFinishedStep)(
ctx,
`Finished running function "${options.previewRun}"`
);
}
}
);
}
}
async function deployToExistingDeployment(ctx, options) {
const deploymentSelection = (0, import_api.deploymentSelectionFromOptions)({
...options,
prod: true
});
const { name: configuredDeploymentName, type: configuredDeploymentType } = (0, import_deployment.getConfiguredDeploymentFromEnvVar)();
const { adminKey, url, deploymentName, deploymentType } = await (0, import_api.fetchDeploymentCredentialsWithinCurrentProject)(
ctx,
deploymentSelection
);
if (deploymentSelection.kind !== "deployKey" && deploymentName !== void 0 && deploymentType !== void 0 && configuredDeploymentName !== null) {
const shouldPushToProd = deploymentName === configuredDeploymentName || (options.yes ?? await askToConfirmPush(
ctx,
{
configuredName: configuredDeploymentName,
configuredType: configuredDeploymentType,
requestedName: deploymentName,
requestedType: deploymentType
},
url
));
if (!shouldPushToProd) {
await ctx.crash({
exitCode: 1,
printedMessage: null,
errorType: "fatal"
});
}
}
await runCommand(ctx, { ...options, url });
const pushOptions = {
adminKey,
verbose: !!options.verbose,
dryRun: !!options.dryRun,
typecheck: options.typecheck,
debug: !!options.debug,
debugBundlePath: options.debugBundlePath,
codegen: options.codegen === "enable",
url,
writePushRequest: options.writePushRequest,
cleanupHandle: null
};
(0, import_context.showSpinner)(
ctx,
`Deploying to ${url}...${options.dryRun ? " [dry run]" : ""}`
);
await (0, import_components.runPush)(import_context.oneoffContext, pushOptions);
(0, import_context.logFinishedStep)(
ctx,
`${options.dryRun ? "Would have deployed" : "Deployed"} Convex functions to ${url}`
);
}
async function runCommand(ctx, options) {
if (options.cmd === void 0) {
return;
}
const urlVar = options.cmdUrlEnvVarName ?? (await (0, import_envvars.suggestedEnvVarName)(ctx)).envVar;
(0, import_context.showSpinner)(
ctx,
`Running '${options.cmd}' with environment variable "${urlVar}" set...${options.dryRun ? " [dry run]" : ""}`
);
if (!options.dryRun) {
const env = { ...process.env };
env[urlVar] = options.url;
const result = (0, import_child_process.spawnSync)(options.cmd, {
env,
stdio: "inherit",
shell: true
});
if (result.status !== 0) {
await ctx.crash({
exitCode: 1,
errorType: "invalid filesystem data",
printedMessage: `'${options.cmd}' failed`
});
}
}
(0, import_context.logFinishedStep)(
ctx,
`${options.dryRun ? "Would have run" : "Ran"} "${options.cmd}" with environment variable "${urlVar}" set`
);
}
async function askToConfirmPush(ctx, deployment, prodUrl) {
(0, import_context.logMessage)(
ctx,
`You're currently developing against your ${import_chalk.default.bold(
deployment.configuredType ?? "dev"
)} deployment
${deployment.configuredName} (set in CONVEX_DEPLOYMENT)
Your ${import_chalk.default.bold(deployment.requestedType)} deployment ${import_chalk.default.bold(
deployment.requestedName
)} serves traffic at:
${(await (0, import_envvars.suggestedEnvVarName)(ctx)).envVar}=${import_chalk.default.bold(prodUrl)}
Make sure that your published client is configured with this URL (for instructions see https://docs.convex.dev/hosting)
`
);
return (0, import_prompts.promptYesNo)(ctx, {
message: `Do you want to push your code to your ${deployment.requestedType} deployment ${deployment.requestedName} now?`,
default: true
});
}
//# sourceMappingURL=deploy.js.map