Skip to content

Commit

Permalink
npx convex self-host function-spec (#34120)
Browse files Browse the repository at this point in the history
refactor the function-spec CLI tool so it can be called from both `npx convex function-spec` and `npx convex self-host function-spec`

GitOrigin-RevId: ab2d298f49b189b5533814ef46a71e72802550ad
  • Loading branch information
ldanilek authored and Convex, Inc. committed Feb 6, 2025
1 parent 7e475ee commit f900492
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
27 changes: 5 additions & 22 deletions npm-packages/convex/src/cli/functionSpec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import chalk from "chalk";
import { logOutput, oneoffContext } from "../bundler/context.js";
import { oneoffContext } from "../bundler/context.js";
import {
deploymentSelectionFromOptions,
fetchDeploymentCredentialsWithinCurrentProject,
} from "./lib/api.js";
import { runSystemQuery } from "./lib/run.js";
import { Command, Option } from "@commander-js/extra-typings";
import { actionDescription } from "./lib/command.js";
import { functionSpecForDeployment } from "./lib/functionSpec.js";

export const functionSpec = new Command("function-spec")
.summary("List function metadata from your deployment")
Expand All @@ -30,25 +29,9 @@ export const functionSpec = new Command("function-spec")
deploymentSelection,
);

const functions = (await runSystemQuery(ctx, {
await functionSpecForDeployment(ctx, {
deploymentUrl,
adminKey,
functionName: "_system/cli/modules:apiSpec",
componentPath: undefined,
args: {},
})) as any[];

const output = JSON.stringify(
{ url: deploymentUrl, functions: functions },
null,
2,
);

if (options.file) {
const fileName = `function_spec_${Date.now().valueOf()}.json`;
ctx.fs.writeUtf8File(fileName, output);
logOutput(ctx, chalk.green(`Wrote function spec to ${fileName}`));
} else {
logOutput(ctx, output);
}
file: !!options.file,
});
});
35 changes: 35 additions & 0 deletions npm-packages/convex/src/cli/lib/functionSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import chalk from "chalk";
import { logOutput } from "../../bundler/context.js";
import { runSystemQuery } from "./run.js";
import { Context } from "../../bundler/context.js";

export async function functionSpecForDeployment(
ctx: Context,
options: {
deploymentUrl: string;
adminKey: string;
file: boolean;
},
) {
const functions = (await runSystemQuery(ctx, {
deploymentUrl: options.deploymentUrl,
adminKey: options.adminKey,
functionName: "_system/cli/modules:apiSpec",
componentPath: undefined,
args: {},
})) as any[];

const output = JSON.stringify(
{ url: options.deploymentUrl, functions: functions },
null,
2,
);

if (options.file) {
const fileName = `function_spec_${Date.now().valueOf()}.json`;
ctx.fs.writeUtf8File(fileName, output);
logOutput(ctx, chalk.green(`Wrote function spec to ${fileName}`));
} else {
logOutput(ctx, output);
}
}
22 changes: 21 additions & 1 deletion npm-packages/convex/src/cli/selfHost.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as dotenv from "dotenv";
import { Command } from "@commander-js/extra-typings";
import { Command, Option } from "@commander-js/extra-typings";
import { Context, logVerbose, oneoffContext } from "../bundler/context.js";
import { handleManuallySetUrlAndAdminKey } from "./configure.js";
import { devAgainstDeployment } from "./lib/dev.js";
Expand All @@ -15,6 +15,7 @@ import { CONVEX_DEPLOYMENT_VAR_NAME } from "./lib/deployment.js";
import { runInDeployment } from "./lib/run.js";
import { importIntoDeployment } from "./lib/convexImport.js";
import { exportFromDeployment } from "./lib/convexExport.js";
import { functionSpecForDeployment } from "./lib/functionSpec.js";
import { dataInDeployment } from "./lib/data.js";
import {
envGetInDeployment,
Expand Down Expand Up @@ -352,3 +353,22 @@ selfHost
...options,
});
});

selfHost
.command("function-spec")
.summary("List function metadata from your deployment")
.description("List argument and return values to your Convex functions.")
.allowExcessArguments(false)
.addOption(new Option("--file", "Output as JSON to a file."))
.addSelfHostOptions()
.showHelpAfterError()
.action(async (options) => {
const ctx = oneoffContext();
const credentials = await selfHostCredentials(ctx, true, options);

await functionSpecForDeployment(ctx, {
deploymentUrl: credentials.url,
adminKey: credentials.adminKey,
file: !!options.file,
});
});

0 comments on commit f900492

Please sign in to comment.