Skip to content

Commit

Permalink
Move utils + add prompt utils (#29059)
Browse files Browse the repository at this point in the history
Made `cli/lib/utils` into a directory to make room for `cli/lib/utils/prompts.ts` and other things (maybe split out all the fetch related utils too).

Replaced all the raw `inquirer.prompt` calls with a simplified prompt util. This centralized some of the stuff like checking for an interactive terminal + using a different prompt type in the monorepo.

GitOrigin-RevId: 1123b04263e187149f526c076281cc22c46ca7f7
  • Loading branch information
sshader authored and Convex, Inc. committed Aug 21, 2024
1 parent 5e59234 commit 6b9d565
Show file tree
Hide file tree
Showing 35 changed files with 244 additions and 237 deletions.
2 changes: 1 addition & 1 deletion npm-packages/convex/src/bundler/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Context } from "./context.js";
import path from "path";

import { findUp } from "find-up";
import { findParentConfigs } from "../cli/lib/utils.js";
import { findParentConfigs } from "../cli/lib/utils/utils.js";

/**
* Mimics Node.js node_modules resolution. Ideally we would be able to
Expand Down
55 changes: 17 additions & 38 deletions npm-packages/convex/src/cli/configure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from "chalk";
import inquirer from "inquirer";
import {
Context,
logFailure,
Expand Down Expand Up @@ -37,12 +36,13 @@ import {
ThrowingFetchError,
validateOrSelectProject,
validateOrSelectTeam,
} from "./lib/utils.js";
} from "./lib/utils/utils.js";
import { writeConvexUrlToEnvFile } from "./lib/envvars.js";
import path from "path";
import { projectDashboardUrl } from "./dashboard.js";
import { doCodegen, doInitCodegen } from "./lib/codegen.js";
import { handleLocalDeployment } from "./lib/localDeployment/localDeployment.js";
import { promptOptions, promptString } from "./lib/utils/prompts.js";

type DeploymentCredentials = {
url: string;
Expand Down Expand Up @@ -288,17 +288,11 @@ async function selectNewProject(
const { teamSlug: selectedTeam, chosen: didChooseBetweenTeams } =
await validateOrSelectTeam(ctx, config.team, "Team:");
let projectName: string = config.project || cwd;
if (process.stdin.isTTY && !config.project) {
projectName = (
await inquirer.prompt([
{
type: "input",
name: "project",
message: "Project name:",
default: cwd,
},
])
).project;
if (!config.project) {
projectName = await promptString(ctx, {
message: "Project name:",
default: cwd,
});
}

showSpinner(ctx, "Creating new Convex project...");
Expand Down Expand Up @@ -386,31 +380,16 @@ async function askToConfigure(
if (!(await hasProjects(ctx))) {
return "new";
}
return await promptToInitWithProjects(reconfigure);
}

async function promptToInitWithProjects(
reconfigure: boolean,
): Promise<"new" | "existing"> {
const { choice } = await inquirer.prompt([
{
// In the Convex mono-repo, `list` seems to cause the command to not
// respond to CTRL+C while `search-list` does not.
type: process.env.CONVEX_RUNNING_LIVE_IN_MONOREPO
? "search-list"
: "list",
name: "choice",
message: reconfigure
? "Configure a different project?"
: "What would you like to configure?",
default: "new",
choices: [
{ name: "create a new project", value: "new" },
{ name: "choose an existing project", value: "existing" },
],
},
]);
return choice;
return await promptOptions(ctx, {
message: reconfigure
? "Configure a different project?"
: "What would you like to configure?",
default: "new",
choices: [
{ name: "create a new project", value: "new" },
{ name: "choose an existing project", value: "existing" },
],
});
}

type DeploymentOptions =
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/convexExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
waitUntilCalled,
deploymentFetch,
logAndHandleFetchError,
} from "./lib/utils.js";
} from "./lib/utils/utils.js";
import {
logFailure,
oneoffContext,
Expand Down
28 changes: 10 additions & 18 deletions npm-packages/convex/src/cli/convexImport.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import chalk from "chalk";
import inquirer from "inquirer";
import {
ensureHasConvexDependency,
formatSize,
waitUntilCalled,
deploymentFetch,
logAndHandleFetchError,
} from "./lib/utils.js";
} from "./lib/utils/utils.js";
import {
logFailure,
oneoffContext,
Expand All @@ -29,6 +28,7 @@ import { actionDescription } from "./lib/command.js";
import { ConvexHttpClient } from "../browser/http_client.js";
import { makeFunctionReference } from "../server/index.js";
import { deploymentDashboardUrlPage } from "./dashboard.js";
import { promptYesNo } from "./lib/utils/prompts.js";

// Backend has minimum chunk size of 5MiB except for the last chunk,
// so we use 5MiB as highWaterMark which makes fs.ReadStream[asyncIterator]
Expand Down Expand Up @@ -279,14 +279,10 @@ async function askToConfirmImport(
}
logMessage(ctx, messageToConfirm);
if (requireManualConfirmation !== false && !yes) {
const { confirmed } = await inquirer.prompt([
{
type: "confirm",
name: "confirmed",
message: `Perform the import?`,
default: true,
},
]);
const confirmed = await promptYesNo(ctx, {
message: "Perform import?",
default: true,
});
if (!confirmed) {
return await ctx.crash({
exitCode: 1,
Expand Down Expand Up @@ -315,14 +311,10 @@ async function askToConfirmImportWithExistingImports(
if (yes) {
return;
}
const { confirmed } = await inquirer.prompt([
{
type: "confirm",
name: "confirmed",
message: `Start another import?`,
default: true,
},
]);
const confirmed = await promptYesNo(ctx, {
message: "Start another import?",
default: true,
});
if (!confirmed) {
return await ctx.crash({
exitCode: 1,
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
fetchDeploymentCredentialsProvisionProd,
} from "./lib/api.js";
import { runPaginatedQuery } from "./lib/run.js";
import { parsePositiveInteger } from "./lib/utils.js";
import { parsePositiveInteger } from "./lib/utils/utils.js";
import { Command } from "@commander-js/extra-typings";
import { actionDescription } from "./lib/command.js";

Expand Down
18 changes: 6 additions & 12 deletions npm-packages/convex/src/cli/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import chalk from "chalk";
import { Command, Option } from "@commander-js/extra-typings";
import inquirer from "inquirer";
import {
Context,
logFinishedStep,
Expand All @@ -25,7 +24,7 @@ import {
bigBrainAPI,
getConfiguredDeploymentName,
readAdminKeyFromEnvVar,
} from "./lib/utils.js";
} from "./lib/utils/utils.js";
import { spawnSync } from "child_process";
import { runFunctionAndLog } from "./lib/run.js";
import { usageStateWarning } from "./lib/usage.js";
Expand All @@ -35,6 +34,7 @@ import {
isPreviewDeployKey,
} from "./lib/deployment.js";
import { runPush } from "./lib/components.js";
import { promptYesNo } from "./lib/utils/prompts.js";

export const deploy = new Command("deploy")
.summary("Deploy to your prod deployment")
Expand Down Expand Up @@ -413,14 +413,8 @@ Your ${chalk.bold(deployment.requestedType)} deployment ${chalk.bold(
Make sure that your published client is configured with this URL (for instructions see https://docs.convex.dev/hosting)\n`,
);
return (
await inquirer.prompt([
{
type: "confirm",
name: "shouldPush",
message: `Do you want to push your code to your ${deployment.requestedType} deployment ${deployment.requestedName} now?`,
default: true,
},
])
).shouldPush;
return promptYesNo(ctx, {
message: `Do you want to push your code to your ${deployment.requestedType} deployment ${deployment.requestedName} now?`,
default: true,
});
}
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/deployments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from "@commander-js/extra-typings";
import { readProjectConfig } from "./lib/config.js";
import chalk from "chalk";
import { bigBrainAPI } from "./lib/utils.js";
import { bigBrainAPI } from "./lib/utils/utils.js";
import { logError, logMessage, oneoffContext } from "../bundler/context.js";

type Deployment = {
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getCurrentTimeString,
waitForever,
waitUntilCalled,
} from "./lib/utils.js";
} from "./lib/utils/utils.js";
import { Crash, WatchContext, Watcher } from "./lib/watch.js";
import { watchLogs } from "./lib/logs.js";
import { subscribe } from "./lib/run.js";
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chalk from "chalk";
import open from "open";
import { oneoffContext } from "../bundler/context.js";
import { getTargetDeploymentName } from "./lib/deployment.js";
import { bigBrainFetch, deprecationCheckWarning } from "./lib/utils.js";
import { bigBrainFetch, deprecationCheckWarning } from "./lib/utils/utils.js";

export const docs = new Command("docs")
.description("Open the docs in the browser")
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
deploymentFetch,
ensureHasConvexDependency,
logAndHandleFetchError,
} from "./lib/utils.js";
} from "./lib/utils/utils.js";

const envSet = new Command("set")
// Pretend value is required
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getConfiguredDeploymentName,
getConfiguredDeploymentOrCrash,
readAdminKeyFromEnvVar,
} from "./utils.js";
} from "./utils/utils.js";

export type DeploymentName = string;
export type DeploymentType = "dev" | "prod" | "local";
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/lib/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { finishPush, startPush, waitForSchema } from "./deploy2.js";
import { version } from "../version.js";
import { PushOptions, runNonComponentsPush } from "./push.js";
import { ensureHasConvexDependency, functionsDir } from "./utils.js";
import { ensureHasConvexDependency, functionsDir } from "./utils/utils.js";
import {
bundleDefinitions,
bundleImplementations,
Expand Down
4 changes: 2 additions & 2 deletions npm-packages/convex/src/cli/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import {
deprecationCheckWarning,
logAndHandleFetchError,
ThrowingFetchError,
} from "./utils.js";
} from "./utils/utils.js";
import { getTargetDeploymentName } from "./deployment.js";
import { createHash } from "crypto";
import { promisify } from "util";
import zlib from "zlib";
import { recursivelyDelete } from "./fsUtils.js";
import { NodeDependency } from "./deployApi/modules.js";
export { productionProvisionHost, provisionHost } from "./utils.js";
export { productionProvisionHost, provisionHost } from "./utils/utils.js";

const brotli = promisify(zlib.brotliCompress);

Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/lib/deploy2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { changeSpinner, Context, logFailure } from "../../bundler/context.js";
import { deploymentFetch, logAndHandleFetchError } from "./utils.js";
import { deploymentFetch, logAndHandleFetchError } from "./utils/utils.js";
import {
StartPushRequest,
startPushResponse,
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/lib/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { changedEnvVarFile, getEnvVarRegex } from "./envvars.js";
import {
CONVEX_DEPLOY_KEY_ENV_VAR_NAME,
readAdminKeyFromEnvVar,
} from "./utils.js";
} from "./utils/utils.js";
import { DeploymentType } from "./api.js";

const ENV_VAR_FILE_PATH = ".env.local";
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/lib/envvars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chalk from "chalk";
import * as dotenv from "dotenv";

import { Context, logWarning } from "../../bundler/context.js";
import { loadPackageJson } from "./utils.js";
import { loadPackageJson } from "./utils/utils.js";

const FRAMEWORKS = [
"create-react-app",
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/convex/src/cli/lib/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
logAndHandleFetchError,
deploymentFetch,
deprecationCheckWarning,
} from "./utils.js";
} from "./utils/utils.js";

type IndexMetadata = {
table: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
bigBrainAPI,
bigBrainFetch,
logAndHandleFetchError,
} from "../utils.js";
} from "../utils/utils.js";

export async function bigBrainStart(
ctx: Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import path from "path";
import { rootDirectory } from "../utils.js";
import { rootDirectory } from "../utils/utils.js";
import { Context } from "../../../bundler/context.js";

export function rootDeploymentStateDir() {
Expand Down
21 changes: 8 additions & 13 deletions npm-packages/convex/src/cli/lib/localDeployment/localDeployment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Context, logVerbose } from "../../../bundler/context.js";
import detect from "detect-port";
import inquirer from "inquirer";
import {
bigBrainPause,
bigBrainRecordActivity,
Expand All @@ -24,6 +23,7 @@ import {
CleanupDeploymentFunc,
OnDeploymentActivityFunc,
} from "../deployment.js";
import { promptSearch } from "../utils/prompts.js";

export type DeploymentDetails = {
deploymentName: string;
Expand Down Expand Up @@ -218,18 +218,13 @@ async function chooseFromExistingLocalDeployments(ctx: Context): Promise<{
config: LocalDeploymentConfig;
}> {
const localDeployments = await getLocalDeployments(ctx);
const { choice } = await inquirer.prompt([
{
type: "search-list",
name: "choice",
message: "Choose from an existing local deployment?",
choices: localDeployments.map((d) => ({
name: d.deploymentName,
value: d,
})),
},
]);
return choice;
return promptSearch(ctx, {
message: "Choose from an existing local deployment?",
choices: localDeployments.map((d) => ({
name: d.deploymentName,
value: d,
})),
});
}

async function choosePorts(
Expand Down
Loading

0 comments on commit 6b9d565

Please sign in to comment.