Skip to content

Commit 2ed5fe1

Browse files
ldanilekConvex, Inc.
authored and
Convex, Inc.
committed
support self-host env variables and flags in regular npx convex dev|deploy|run|etc (#34177)
1. check for CONVEX_SELF_HOST_URL and CONVEX_SELF_HOST_ADMIN_KEY env variables in all of the CLI commands, and consider them to be url and admin key if provided 2. avoid checking usage state, which is a big brain api call, if we're doing "urlWithAdminKey" 3. throw error if you've configured self-hosting but you also have cloud-hosting specific variables or flags set -- specifically CONVEX_DEPLOYMENT and `--prod`/`--preview-name`/etc. this PR doesn't delete the `self-host` subcommand yet, nor does it rename SELF_HOST to SELF_HOSTED which can be in separate PRs. GitOrigin-RevId: 0804513a2cf504a62b374179bacfca9d6c70a05a
1 parent d9d98b2 commit 2ed5fe1

File tree

16 files changed

+215
-32
lines changed

16 files changed

+215
-32
lines changed

npm-packages/convex/src/cli/configure.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
DeploymentName,
1313
fetchDeploymentCredentialsProvisioningDevOrProdMaybeThrows,
1414
createProject,
15+
DeploymentSelection,
1516
} from "./lib/api.js";
1617
import {
1718
configFilepath,
@@ -72,6 +73,7 @@ export async function deploymentCredentialsOrConfigure(
7273
ctx: Context,
7374
chosenConfiguration: ChosenConfiguration,
7475
cmdOptions: {
76+
deploymentSelection: DeploymentSelection;
7577
prod: boolean;
7678
localOptions: {
7779
ports?: {
@@ -88,21 +90,33 @@ export async function deploymentCredentialsOrConfigure(
8890
cloud?: boolean | undefined;
8991
url?: string | undefined;
9092
adminKey?: string | undefined;
93+
envFile?: string | undefined;
9194
},
9295
partitionId?: number | undefined,
9396
): Promise<
9497
DeploymentCredentials & {
9598
deploymentName?: DeploymentName;
9699
}
97100
> {
98-
if (cmdOptions.url !== undefined && cmdOptions.adminKey !== undefined) {
101+
if (cmdOptions.deploymentSelection.kind === "urlWithAdminKey") {
99102
const credentials = await handleManuallySetUrlAndAdminKey(ctx, {
100-
url: cmdOptions.url,
101-
adminKey: cmdOptions.adminKey,
103+
url: cmdOptions.deploymentSelection.url,
104+
adminKey: cmdOptions.deploymentSelection.adminKey,
102105
});
103106
return { ...credentials };
104107
}
105108

109+
if (
110+
cmdOptions.deploymentSelection.kind !== "ownDev" &&
111+
cmdOptions.deploymentSelection.kind !== "ownProd"
112+
) {
113+
return await ctx.crash({
114+
exitCode: 1,
115+
errorType: "fatal",
116+
printedMessage: `Invalid deployment selection: ${cmdOptions.deploymentSelection.kind}.`,
117+
});
118+
}
119+
106120
const config = readGlobalConfig(ctx);
107121
const globallyForceCloud = !!config?.optOutOfLocalDevDeploymentsUntilBetaOver;
108122
if (globallyForceCloud && cmdOptions.local) {
@@ -129,11 +143,12 @@ export async function deploymentCredentialsOrConfigure(
129143
// TODO complain about any non-default cmdOptions.localOptions here
130144
// because we're ignoring them if this isn't a local development.
131145

132-
const deploymentOptions: DeploymentOptions = cmdOptions.prod
133-
? { kind: "prod" }
134-
: devDeployment === "local"
135-
? { kind: "local", ...cmdOptions.localOptions }
136-
: { kind: "dev" };
146+
const deploymentOptions: DeploymentOptions =
147+
cmdOptions.deploymentSelection.kind === "ownProd"
148+
? { kind: "prod" }
149+
: devDeployment === "local"
150+
? { kind: "local", ...cmdOptions.localOptions }
151+
: { kind: "dev" };
137152
const {
138153
deploymentName,
139154
deploymentUrl: url,

npm-packages/convex/src/cli/convexExport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export const convexExport = new Command("export")
2323
.action(async (options) => {
2424
const ctx = oneoffContext();
2525

26-
const deploymentSelection = deploymentSelectionFromOptions(options);
26+
const deploymentSelection = await deploymentSelectionFromOptions(
27+
ctx,
28+
options,
29+
);
2730

2831
const {
2932
adminKey,

npm-packages/convex/src/cli/convexImport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export const convexImport = new Command("import")
2727

2828
await ensureHasConvexDependency(ctx, "import");
2929

30-
const deploymentSelection = deploymentSelectionFromOptions(options);
30+
const deploymentSelection = await deploymentSelectionFromOptions(
31+
ctx,
32+
options,
33+
);
3134

3235
const deploymentNotice = options.prod
3336
? ` in your ${chalk.bold("prod")} deployment`

npm-packages/convex/src/cli/dashboard.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export const dashboard = new Command("dashboard")
2424
.action(async (options) => {
2525
const ctx = oneoffContext();
2626

27-
const deploymentSelection = deploymentSelectionFromOptions(options);
27+
const deploymentSelection = await deploymentSelectionFromOptions(
28+
ctx,
29+
options,
30+
);
2831
const { deploymentName } = await fetchDeploymentCredentialsProvisionProd(
2932
ctx,
3033
deploymentSelection,

npm-packages/convex/src/cli/data.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export const data = new Command("data")
2222
.showHelpAfterError()
2323
.action(async (tableName, options) => {
2424
const ctx = oneoffContext();
25-
const deploymentSelection = deploymentSelectionFromOptions(options);
25+
const deploymentSelection = await deploymentSelectionFromOptions(
26+
ctx,
27+
options,
28+
);
2629

2730
const {
2831
adminKey,

npm-packages/convex/src/cli/deploy.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import {
2121
import { PushOptions } from "./lib/push.js";
2222
import {
2323
CONVEX_DEPLOY_KEY_ENV_VAR_NAME,
24+
CONVEX_SELF_HOSTED_URL_VAR_NAME,
2425
bigBrainAPI,
2526
getConfiguredDeployment,
2627
readAdminKeyFromEnvVar,
2728
} from "./lib/utils/utils.js";
2829
import { runFunctionAndLog } from "./lib/run.js";
2930
import { usageStateWarning } from "./lib/usage.js";
3031
import {
32+
CONVEX_DEPLOYMENT_VAR_NAME,
3133
deploymentTypeFromAdminKey,
3234
getConfiguredDeploymentFromEnvVar,
3335
isPreviewDeployKey,
@@ -76,6 +78,14 @@ export const deploy = new Command("deploy")
7678
.hideHelp()
7779
.conflicts("preview-create"),
7880
)
81+
.addOption(
82+
new Option(
83+
"--env-file <envFile>",
84+
`Path to a custom file of environment variables, for choosing the \
85+
deployment, e.g. ${CONVEX_DEPLOYMENT_VAR_NAME} or ${CONVEX_SELF_HOSTED_URL_VAR_NAME}. \
86+
Same format as .env.local or .env files, and overrides them.`,
87+
),
88+
)
7989
.addOption(new Option("--partition-id <id>").hideHelp())
8090
.showHelpAfterError()
8191
.action(async (cmdOptions) => {
@@ -98,12 +108,11 @@ export const deploy = new Command("deploy")
98108
});
99109
}
100110

101-
await usageStateWarning(ctx);
102-
103111
if (
104112
configuredDeployKey !== null &&
105113
isPreviewDeployKey(configuredDeployKey)
106114
) {
115+
await usageStateWarning(ctx);
107116
if (cmdOptions.previewName !== undefined) {
108117
await ctx.crash({
109118
exitCode: 1,
@@ -247,12 +256,16 @@ async function deployToExistingDeployment(
247256
writePushRequest?: string | undefined;
248257
liveComponentSources?: boolean | undefined;
249258
partitionId?: string | undefined;
259+
envFile?: string | undefined;
250260
},
251261
) {
252-
const deploymentSelection = deploymentSelectionFromOptions({
262+
const deploymentSelection = await deploymentSelectionFromOptions(ctx, {
253263
...options,
254-
prod: true,
264+
implicitProd: true,
255265
});
266+
if (deploymentSelection.kind !== "urlWithAdminKey") {
267+
await usageStateWarning(ctx);
268+
}
256269
const { name: configuredDeploymentName, type: configuredDeploymentType } =
257270
getConfiguredDeploymentFromEnvVar();
258271
const { adminKey, url, deploymentName, deploymentType } =

npm-packages/convex/src/cli/dev.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { checkAuthorization, performLogin } from "./lib/login.js";
55
import { usageStateWarning } from "./lib/usage.js";
66
import { normalizeDevOptions } from "./lib/command.js";
77
import { devAgainstDeployment } from "./lib/dev.js";
8+
import { deploymentSelectionFromOptions } from "./lib/api.js";
9+
import { CONVEX_DEPLOYMENT_VAR_NAME } from "./lib/deployment.js";
10+
import { CONVEX_SELF_HOSTED_URL_VAR_NAME } from "./lib/utils/utils.js";
811

912
export const dev = new Command("dev")
1013
.summary("Develop against a dev deployment, watching for changes")
@@ -54,6 +57,14 @@ export const dev = new Command("dev")
5457
.default(false)
5558
.hideHelp(),
5659
)
60+
.addOption(
61+
new Option(
62+
"--env-file <envFile>",
63+
`Path to a custom file of environment variables, for choosing the \
64+
deployment, e.g. ${CONVEX_DEPLOYMENT_VAR_NAME} or ${CONVEX_SELF_HOSTED_URL_VAR_NAME}. \
65+
Same format as .env.local or .env files, and overrides them.`,
66+
),
67+
)
5768
.addOption(new Option("--skip-push").default(false).hideHelp())
5869
.addOption(new Option("--admin-key <adminKey>").hideHelp())
5970
.addOption(new Option("--url <url>").hideHelp())
@@ -95,6 +106,20 @@ export const dev = new Command("dev")
95106

96107
const devOptions = await normalizeDevOptions(ctx, cmdOptions);
97108

109+
const deploymentSelection = await deploymentSelectionFromOptions(
110+
ctx,
111+
cmdOptions,
112+
[
113+
["prod", "--prod"],
114+
["local", "--local"],
115+
["cloud", "--cloud"],
116+
["team", "--team"],
117+
["project", "--project"],
118+
["devDeployment", "--dev-deployment"],
119+
["configure", "--configure"],
120+
],
121+
);
122+
98123
if (cmdOptions.configure === undefined) {
99124
if (cmdOptions.team || cmdOptions.project || cmdOptions.devDeployment)
100125
return await ctx.crash({
@@ -143,7 +168,7 @@ export const dev = new Command("dev")
143168
localOptions["forceUpgrade"] = cmdOptions.localForceUpgrade;
144169
}
145170

146-
if (!cmdOptions.url || !cmdOptions.adminKey) {
171+
if (deploymentSelection.kind !== "urlWithAdminKey") {
147172
if (!(await checkAuthorization(ctx, false))) {
148173
await performLogin(ctx, cmdOptions);
149174
}
@@ -160,11 +185,14 @@ export const dev = new Command("dev")
160185
{
161186
...cmdOptions,
162187
localOptions,
188+
deploymentSelection,
163189
},
164190
partitionId,
165191
);
166192

167-
await usageStateWarning(ctx);
193+
if (deploymentSelection.kind !== "urlWithAdminKey") {
194+
await usageStateWarning(ctx);
195+
}
168196

169197
if (cmdOptions.skipPush) {
170198
return;

npm-packages/convex/src/cli/disableLocalDev.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const disableLocalDeployments = new Command("disable-local-deployments")
3636
}
3737

3838
await deploymentCredentialsOrConfigure(ctx, null, {
39+
deploymentSelection: { kind: "ownDev" },
3940
prod: false,
4041
localOptions: {
4142
forceUpgrade: false,

npm-packages/convex/src/cli/env.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ async function selectEnvDeployment(
3939
ctx: Context,
4040
options: DeploymentSelectionOptions,
4141
) {
42-
const deploymentSelection = deploymentSelectionFromOptions(options);
42+
const deploymentSelection = await deploymentSelectionFromOptions(
43+
ctx,
44+
options,
45+
);
4346
const { adminKey, url, deploymentName, deploymentType } =
4447
await fetchDeploymentCredentialsWithinCurrentProject(
4548
ctx,

npm-packages/convex/src/cli/functionSpec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export const functionSpec = new Command("function-spec")
2121
.showHelpAfterError()
2222
.action(async (options) => {
2323
const ctx = oneoffContext();
24-
const deploymentSelection = deploymentSelectionFromOptions(options);
24+
const deploymentSelection = await deploymentSelectionFromOptions(
25+
ctx,
26+
options,
27+
);
2528

2629
const { adminKey, url: deploymentUrl } =
2730
await fetchDeploymentCredentialsWithinCurrentProject(

0 commit comments

Comments
 (0)