Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 97 additions & 4 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cancel, intro, isCancel, log, select, spinner, text } from "@clack/prompts";
import { cancel, intro, isCancel, log, outro, select, spinner, text } from "@clack/prompts";
import fs from "fs-extra";
import os from "node:os";
import path from "node:path";

import { scaffoldCreateTemplate } from "../templates/render-create-template";
Expand All @@ -18,6 +19,12 @@ import {
collectCreateAddonSetupContext,
executeCreateAddonSetupContext,
} from "../tasks/setup-addons";
import {
collectComputeDeployContext,
executeComputeDeployContext,
type ComputeDeployContext,
type ComputeDeployResult,
} from "../tasks/deploy-to-compute";
import {
trackCreateCompleted,
trackCreateFailed,
Expand All @@ -43,6 +50,7 @@ export type CreatePromptContext = {
projectPackageName: string;
prismaSetupContext: PrismaSetupContext;
addonSetupContext?: CreateAddonSetupContext;
computeDeployContext?: ComputeDeployContext;
};

type ExecuteCreateContextResult =
Expand Down Expand Up @@ -84,6 +92,27 @@ function validateProjectName(value: string | undefined): string | undefined {
return undefined;
}

function formatDeployEnvFile(databaseUrl: string): string {
if (/[\r\n]/.test(databaseUrl)) {
throw new Error("DATABASE_URL must be a single-line value.");
}

return `DATABASE_URL=${databaseUrl}\n`;
}

async function createDeployEnvFile(databaseUrl: string): Promise<{
path: string;
cleanup(): Promise<void>;
}> {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "create-prisma-compute-env-"));
const envFilePath = path.join(tempDir, ".env");
await fs.writeFile(envFilePath, formatDeployEnvFile(databaseUrl), "utf8");
return {
path: envFilePath,
cleanup: () => fs.remove(tempDir),
};
}

async function promptForProjectName(): Promise<string | undefined> {
const projectName = await text({
message: "Project name",
Expand Down Expand Up @@ -305,14 +334,27 @@ async function collectCreateContext(
return;
}

const projectPackageName = toPackageName(path.basename(targetDirectory));

const computeDeployContext = await collectComputeDeployContext(input, {
template,
packageManager: prismaSetupContext.packageManager,
useDefaults,
defaultServiceName: projectPackageName,
});
if (computeDeployContext === undefined) {
return;
}

return {
targetDirectory,
targetPathState,
force,
template,
projectPackageName: toPackageName(path.basename(targetDirectory)),
projectPackageName,
prismaSetupContext,
addonSetupContext: addonSetupContext ?? undefined,
computeDeployContext: computeDeployContext ?? undefined,
};
}

Expand All @@ -329,6 +371,7 @@ async function executeCreateContext(
schemaPreset: context.prismaSetupContext.schemaPreset,
provider: context.prismaSetupContext.databaseProvider,
packageManager: context.prismaSetupContext.packageManager,
compute: Boolean(context.computeDeployContext),
});
scaffoldSpinner.stop("Project files scaffolded.");
} catch (error) {
Expand Down Expand Up @@ -385,14 +428,15 @@ async function executeCreateContext(
}
}

let prismaResult: Awaited<ReturnType<typeof executePrismaSetupContext>>;
try {
const didSetupPrisma = await executePrismaSetupContext(context.prismaSetupContext, {
prismaResult = await executePrismaSetupContext(context.prismaSetupContext, {
prependNextSteps: nextSteps,
projectDir: context.targetDirectory,
includeDevNextStep: true,
});

if (!didSetupPrisma) {
if (!prismaResult.ok) {
return {
ok: false,
stage: "prisma_setup",
Expand All @@ -406,5 +450,54 @@ async function executeCreateContext(
};
}

let deployResult: ComputeDeployResult | undefined;
if (context.computeDeployContext) {
let deployEnvFile: Awaited<ReturnType<typeof createDeployEnvFile>> | undefined;
try {
if (prismaResult.databaseUrl) {
deployEnvFile = await createDeployEnvFile(prismaResult.databaseUrl);
}

const result = await executeComputeDeployContext({
context: context.computeDeployContext,
projectDir: context.targetDirectory,
envFilePath: deployEnvFile?.path,
});
if (!result.ok && !result.cancelled) {
return {
ok: false,
stage: "compute_deploy",
error: result.error,
};
}
if (result.ok) {
deployResult = result.result;
}
} catch (error) {
return {
ok: false,
stage: "compute_deploy",
error,
};
} finally {
if (deployEnvFile) {
await deployEnvFile.cleanup().catch(() => undefined);
}
}
}
Comment thread
AmanVarshney01 marked this conversation as resolved.

const summaryLines: string[] = [];
summaryLines.push(`Setup complete.${prismaResult.warningSection}`);
if (deployResult) {
summaryLines.push(
"",
"Deployed to Prisma Compute:",
`- Service URL: ${deployResult.serviceUrl}`,
`- Version URL: ${deployResult.versionUrl}`,
);
}
summaryLines.push("", "Next steps:", prismaResult.nextSteps.join("\n"));
outro(summaryLines.join("\n"));

return { ok: true };
}
Loading
Loading