@@ -5,6 +5,24 @@ import type { ExecOptions } from '@actions/exec'
55
66import { CEDAR_FRAMEWORK_PATH } from '../actionsLib.mjs'
77
8+ const CEDAR_APP_TEMPLATE_PATH = path . join (
9+ CEDAR_FRAMEWORK_PATH ,
10+ 'packages' ,
11+ 'create-cedar-app' ,
12+ 'templates' ,
13+ 'ts' ,
14+ )
15+
16+ const PRISMA_SCHEMA_DATASOURCE_URL_OLD = / ^ \s * u r l \s * = \s * e n v \( [ ^ ) ] * \) \s * \n / m
17+
18+ const CEDAR_APP_TEMPLATE_API_PACKAGE_JSON_PATH = path . join (
19+ CEDAR_APP_TEMPLATE_PATH ,
20+ 'api' ,
21+ 'package.json' ,
22+ )
23+
24+ const PRISMA_SCHEMA_GENERATOR_OLD = / g e n e r a t o r c l i e n t \{ [ ^ } ] * \} / s
25+
826/**
927 * Exec a command.
1028 * Output will be streamed to the live console.
@@ -95,7 +113,100 @@ async function setUpRscProject(
95113 } )
96114 console . log ( )
97115
116+ // TODO: Remove this block once Prisma v7 support has been merged into a
117+ // canary release of create-cedar-app, at which point the created project
118+ // will already be Prisma v7 compatible.
119+ console . log ( 'Updating project for Prisma v7 compatibility' )
120+ updateProjectForPrisma7 ( rscProjectPath )
121+ console . log ( )
122+
123+ console . log ( 'Installing new Prisma v7 dependencies' )
124+ await execInProject ( 'yarn install' )
125+ console . log ( )
126+
98127 console . log ( `Building project in ${ rscProjectPath } ` )
99128 await execInProject ( `node ${ cedarBinPath } build -v` )
100129 console . log ( )
101130}
131+
132+ /**
133+ * @param {string } rscProjectPath
134+ * @returns {void }
135+ */
136+ function updateProjectForPrisma7 ( rscProjectPath ) {
137+ // Copy prisma.config.cjs from template (Prisma v7 format)
138+ const prismaConfigPath = path . join ( rscProjectPath , 'api' , 'prisma.config.cjs' )
139+ fs . copyFileSync (
140+ path . join ( CEDAR_APP_TEMPLATE_PATH , 'api' , 'prisma.config.cjs' ) ,
141+ prismaConfigPath ,
142+ )
143+ console . log ( ' Updated api/prisma.config.cjs' )
144+
145+ // Update schema.prisma: remove datasource url line and replace generator block
146+ // (Can't copy the template wholesale because the project has extra models from
147+ // the RSC setup steps.)
148+ const schemaPath = path . join ( rscProjectPath , 'api' , 'db' , 'schema.prisma' )
149+ const templateSchema = fs . readFileSync (
150+ path . join ( CEDAR_APP_TEMPLATE_PATH , 'api' , 'db' , 'schema.prisma' ) ,
151+ 'utf8' ,
152+ )
153+ const generatorMatch = templateSchema . match ( PRISMA_SCHEMA_GENERATOR_OLD )
154+ const templateGenerator = generatorMatch ? generatorMatch [ 0 ] : null
155+ if ( ! templateGenerator ) {
156+ throw new Error ( 'Could not find generator block in template schema.prisma' )
157+ }
158+ const schemaContent = fs . readFileSync ( schemaPath , 'utf8' )
159+ const updatedSchema = schemaContent
160+ . replace ( PRISMA_SCHEMA_DATASOURCE_URL_OLD , '' )
161+ . replace ( PRISMA_SCHEMA_GENERATOR_OLD , templateGenerator )
162+ fs . writeFileSync ( schemaPath , updatedSchema , 'utf8' )
163+ console . log ( ' Updated api/db/schema.prisma' )
164+
165+ // Copy api/src/lib/db.ts from template (Prisma v7 client initialisation)
166+ const dbTsPath = path . join ( rscProjectPath , 'api' , 'src' , 'lib' , 'db.ts' )
167+ fs . copyFileSync (
168+ path . join ( CEDAR_APP_TEMPLATE_PATH , 'api' , 'src' , 'lib' , 'db.ts' ) ,
169+ dbTsPath ,
170+ )
171+ console . log ( ' Updated api/src/lib/db.ts' )
172+
173+ // Copy api/tsconfig.json from template (Prisma v7 / Node16 module resolution)
174+ const apiTsconfigPath = path . join ( rscProjectPath , 'api' , 'tsconfig.json' )
175+ fs . copyFileSync (
176+ path . join ( CEDAR_APP_TEMPLATE_PATH , 'api' , 'tsconfig.json' ) ,
177+ apiTsconfigPath ,
178+ )
179+ console . log ( ' Updated api/tsconfig.json' )
180+
181+ // Merge @prisma /adapter-better-sqlite3 and better-sqlite3 from template into
182+ // api/package.json (the canary-generated project won't have these deps)
183+ const apiPackageJsonPath = path . join ( rscProjectPath , 'api' , 'package.json' )
184+ const apiPackageJson = JSON . parse ( fs . readFileSync ( apiPackageJsonPath , 'utf8' ) )
185+ const templateApiPackageJson = JSON . parse (
186+ fs . readFileSync ( CEDAR_APP_TEMPLATE_API_PACKAGE_JSON_PATH , 'utf8' ) ,
187+ )
188+ const depsToAdd = [ '@prisma/adapter-better-sqlite3' , 'better-sqlite3' ]
189+ for ( const dep of depsToAdd ) {
190+ apiPackageJson . dependencies [ dep ] = templateApiPackageJson . dependencies [ dep ]
191+ }
192+ fs . writeFileSync (
193+ apiPackageJsonPath ,
194+ JSON . stringify ( apiPackageJson , null , 2 ) + '\n' ,
195+ 'utf8' ,
196+ )
197+ console . log ( ' Updated api/package.json' )
198+
199+ // Copy scripts/tsconfig.json from template (Prisma v7 / Node16 module resolution)
200+ const scriptsTsconfigPath = path . join (
201+ rscProjectPath ,
202+ 'scripts' ,
203+ 'tsconfig.json' ,
204+ )
205+ if ( fs . existsSync ( scriptsTsconfigPath ) ) {
206+ fs . copyFileSync (
207+ path . join ( CEDAR_APP_TEMPLATE_PATH , 'scripts' , 'tsconfig.json' ) ,
208+ scriptsTsconfigPath ,
209+ )
210+ console . log ( ' Updated scripts/tsconfig.json' )
211+ }
212+ }
0 commit comments