11import path from "node:path" ;
2- import { isCancel , log , select , spinner , text } from "@clack/prompts" ;
2+ import { isCancel , log , select , spinner } from "@clack/prompts" ;
33import { consola } from "consola" ;
44import { execa } from "execa" ;
55import fs from "fs-extra" ;
66import pc from "picocolors" ;
77import type { PackageManager , ProjectConfig } from "../../types" ;
8- import { exitCancelled } from "../../utils/errors" ;
98import { getPackageExecutionCommand } from "../../utils/package-runner" ;
109import { addEnvVariablesToFile , type EnvVariable } from "../core/env-setup" ;
1110
@@ -83,56 +82,6 @@ async function setupWithCreateDb(
8382 }
8483}
8584
86- async function initPrismaDatabase (
87- serverDir : string ,
88- packageManager : PackageManager ,
89- ) {
90- try {
91- const prismaDir = path . join ( serverDir , "prisma" ) ;
92- await fs . ensureDir ( prismaDir ) ;
93-
94- log . info ( "Starting Prisma PostgreSQL setup." ) ;
95-
96- const prismaInitCommand = getPackageExecutionCommand (
97- packageManager ,
98- "prisma init --db" ,
99- ) ;
100-
101- await execa ( prismaInitCommand , {
102- cwd : serverDir ,
103- stdio : "inherit" ,
104- shell : true ,
105- } ) ;
106-
107- log . info (
108- pc . yellow (
109- "Please copy the Prisma Postgres URL.\nIt looks like: postgresql://user:password@host:5432/db?sslmode=require" ,
110- ) ,
111- ) ;
112-
113- const databaseUrl = await text ( {
114- message : "Paste your Prisma Postgres database URL:" ,
115- validate ( value ) {
116- if ( ! value ) return "Please enter a database URL" ;
117- if ( ! value . startsWith ( "postgresql://" ) ) {
118- return "URL should start with postgresql://" ;
119- }
120- } ,
121- } ) ;
122-
123- if ( isCancel ( databaseUrl ) ) return null ;
124-
125- return {
126- databaseUrl : databaseUrl as string ,
127- } ;
128- } catch ( error ) {
129- if ( error instanceof Error ) {
130- consola . error ( error . message ) ;
131- }
132- return null ;
133- }
134- }
135-
13685async function writeEnvFile (
13786 projectDir : string ,
13887 backend : ProjectConfig [ "backend" ] ,
@@ -165,25 +114,6 @@ async function writeEnvFile(
165114 }
166115}
167116
168- async function addDotenvImportToPrismaConfig (
169- projectDir : string ,
170- backend : ProjectConfig [ "backend" ] ,
171- ) {
172- try {
173- const prismaConfigPath = path . join (
174- projectDir ,
175- "packages/db/prisma.config.ts" ,
176- ) ;
177- let content = await fs . readFile ( prismaConfigPath , "utf8" ) ;
178- const envPath =
179- backend === "self" ? "../../apps/web/.env" : "../../apps/server/.env" ;
180- content = `import dotenv from "dotenv";\ndotenv.config({ path: "${ envPath } " });\n${ content } ` ;
181- await fs . writeFile ( prismaConfigPath , content ) ;
182- } catch ( _error ) {
183- consola . error ( "Failed to update prisma.config.ts" ) ;
184- }
185- }
186-
187117function displayManualSetupInstructions ( target : "apps/web" | "apps/server" ) {
188118 log . info ( `Manual Prisma PostgreSQL Setup Instructions:
189119
@@ -199,7 +129,7 @@ export async function setupPrismaPostgres(
199129 config : ProjectConfig ,
200130 cliInput ?: { manualDb ?: boolean } ,
201131) {
202- const { packageManager, projectDir, orm , backend } = config ;
132+ const { packageManager, projectDir, backend } = config ;
203133 const manualDb = cliInput ?. manualDb ?? false ;
204134 const dbDir = path . join ( projectDir , "packages/db" ) ;
205135
@@ -214,72 +144,38 @@ export async function setupPrismaPostgres(
214144 return ;
215145 }
216146
217- const mode = await select ( {
147+ const setupMode = await select ( {
218148 message : "Prisma Postgres setup: choose mode" ,
219149 options : [
220150 {
221- label : "Automatic" ,
151+ label : "Automatic (create-db) " ,
222152 value : "auto" ,
223- hint : "Automated setup with provider CLI, sets .env " ,
153+ hint : "Provision a database via Prisma's create-db CLI " ,
224154 } ,
225155 {
226156 label : "Manual" ,
227157 value : "manual" ,
228- hint : "Manual setup, add env vars yourself " ,
158+ hint : "Add your own DATABASE_URL later " ,
229159 } ,
230160 ] ,
231161 initialValue : "auto" ,
232162 } ) ;
233163
234- if ( isCancel ( mode ) ) return exitCancelled ( "Operation cancelled" ) ;
164+ if ( isCancel ( setupMode ) ) return ;
235165
236- if ( mode === "manual" ) {
166+ if ( setupMode === "manual" ) {
237167 await writeEnvFile ( projectDir , backend ) ;
238168 displayManualSetupInstructions (
239169 backend === "self" ? "apps/web" : "apps/server" ,
240170 ) ;
241171 return ;
242172 }
243173
244- const setupOptions = [
245- {
246- label : "Quick setup with create-db" ,
247- value : "create-db" ,
248- hint : "Fastest, automated database creation (no auth)" ,
249- } ,
250- ] ;
251-
252- if ( orm === "prisma" ) {
253- setupOptions . push ( {
254- label : "Custom setup with Prisma Init" ,
255- value : "custom" ,
256- hint : "More control (requires auth)" ,
257- } ) ;
258- }
259-
260- const setupMethod = await select ( {
261- message : "Choose your Prisma Postgres setup method:" ,
262- options : setupOptions ,
263- initialValue : "create-db" ,
264- } ) ;
265-
266- if ( isCancel ( setupMethod ) ) return exitCancelled ( "Operation cancelled" ) ;
267-
268- let prismaConfig : PrismaConfig | null = null ;
269-
270- if ( setupMethod === "create-db" ) {
271- prismaConfig = await setupWithCreateDb ( dbDir , packageManager ) ;
272- } else {
273- prismaConfig = await initPrismaDatabase ( dbDir , packageManager ) ;
274- }
174+ const prismaConfig = await setupWithCreateDb ( dbDir , packageManager ) ;
275175
276176 if ( prismaConfig ) {
277177 await writeEnvFile ( projectDir , backend , prismaConfig ) ;
278178
279- if ( orm === "prisma" ) {
280- await addDotenvImportToPrismaConfig ( projectDir , backend ) ;
281- }
282-
283179 log . success (
284180 pc . green ( "Prisma Postgres database configured successfully!" ) ,
285181 ) ;
0 commit comments