1- import { AstromiddlewareContent , clerkUI , SignInPage , SignUpPage } from "./utility.js" ;
1+ import { AstromiddlewareContent , clerkUI , clerkUserController , clerkUserModel , clerkWebhookRoute , SignInPage , SignUpPage } from "./utility.js" ;
22import chalk from "chalk" ;
33import fs from "fs" ;
44import { execa } from "execa" ;
55import path from "path" ;
6- import inquirer from 'inquirer' ;
76
8- export async function setupClerkAstro ( framework , projectName , language , styling , useTailwind ) {
7+ export async function setupClerkAstro ( state ) {
8+ const { framework, projectName, language, styling, uiLibrary, database } = state
9+ const useTailwind = ( styling === "tailwind" ) ;
910 try {
1011 const targetDir = path . resolve ( projectName ) ;
1112 const currentDir = process . cwd ( ) ;
@@ -39,12 +40,12 @@ export async function setupClerkAstro(framework, projectName, language, styling,
3940 if ( ! envcontent . includes ( "PUBLIC_CLERK_PUBLISHABLE_KEY" ) ) {
4041 envcontent += `\n\n${ clerkEnvVars } ` ;
4142 fs . writeFileSync ( envfilePath , envcontent . trim ( ) + "\n" , "utf-8" ) ;
42- console . log ( `✅ Clerk environment variables added to ${ file } ` ) ;
43+ console . log ( `Clerk environment variables added to ${ file } ` ) ;
4344 } else {
44- console . log ( `ℹ️ Clerk environment variables already exist in ${ file } ` ) ;
45+ console . log ( `Clerk environment variables already exist in ${ file } ` ) ;
4546 }
4647 } catch ( err ) {
47- console . error ( `❌ Error updating ${ file } :` , err . message ) ;
48+ console . error ( `Error updating ${ file } :` , err . message ) ;
4849 }
4950 }
5051
@@ -112,9 +113,9 @@ export async function setupClerkAstro(framework, projectName, language, styling,
112113 }
113114
114115 fs . writeFileSync ( configfilePath , content , "utf-8" ) ;
115- console . log ( "✅ astro.config.mjs updated successfully!" ) ;
116+ console . log ( "astro.config.mjs updated successfully!" ) ;
116117 } catch ( err ) {
117- console . error ( "❌ Error updating astro.config.mjs:" , err . message ) ;
118+ console . error ( "Error updating astro.config.mjs:" , err . message ) ;
118119 }
119120
120121 const homefilePath = path . join ( srcDir , 'pages' , "index.astro" ) ; ; // Path to your file
@@ -143,13 +144,13 @@ export async function setupClerkAstro(framework, projectName, language, styling,
143144 // Write the updated content back to the file
144145 fs . writeFileSync ( homefilePath , updatedContent , "utf-8" ) ;
145146
146- console . log ( "✅ Clerk UI added successfully to test.astro!" ) ;
147+ console . log ( "Clerk UI added successfully to test.astro!" ) ;
147148
148149 // Write middleware file
149150 const middlewareFile = path . join ( srcDir , `middleware.${ ( language === "ts" ) ? `ts` : `js` } ` ) ;
150151 fs . writeFileSync ( middlewareFile , AstromiddlewareContent . trim ( ) ) ;
151152
152- console . log ( chalk . green ( "✅ Middleware file created at:" ) , middlewareFile ) ;
153+ console . log ( chalk . green ( "Middleware file created at:" ) , middlewareFile ) ;
153154
154155 const signinpageContent = SignInPage ( useTailwind ) ;
155156 const signuppageContent = SignUpPage ( useTailwind ) ;
@@ -160,13 +161,61 @@ export async function setupClerkAstro(framework, projectName, language, styling,
160161 const signUpPagePath = path . join ( srcDir , "pages" , "signup.astro" ) ;
161162 fs . writeFileSync ( signUpPagePath , signuppageContent ) ;
162163
164+ if ( database === "mongodb" ) {
165+ console . log ( chalk . yellow ( "Setting up Syncing clerk data with database...." ) ) ;
166+ const clerkSecretVars = "CLERK_WEBHOOK_SIGNING_SECRET=" ;
167+ for ( const file of envFiles ) {
168+ const envfilePath = path . resolve ( file ) ;
169+ try {
170+ let envcontent = "" ;
171+
172+ // If file exists, read it; otherwise create a new one
173+ if ( fs . existsSync ( envfilePath ) ) {
174+ envcontent = fs . readFileSync ( envfilePath , "utf-8" ) ;
175+ }
176+
177+ // Prevent duplicate entries (idempotent)
178+ if ( ! envcontent . includes ( "CLERK_WEBHOOK_SIGNING_SECRET" ) ) {
179+ envcontent += `\n\n${ clerkSecretVars } ` ;
180+ fs . writeFileSync ( envfilePath , envcontent . trim ( ) + "\n" , "utf-8" ) ;
181+ console . log ( ` Clerk environment variables added to ${ file } ` ) ;
182+ } else {
183+ console . log ( `Clerk environment variables already exist in ${ file } ` ) ;
184+ }
185+ } catch ( err ) {
186+ console . error ( ` Error updating ${ file } :` , err . message ) ;
187+ }
188+
189+
190+ const webhookDir = path . join ( srcDir , "pages" , "api" , "webhook" ) ;
191+ fs . mkdirSync ( webhookDir , { recursive : true } ) ; // ✅ creates folder if missing
192+
193+ const webhookPath = path . join ( webhookDir , `clerk.${ language === "ts" ? "ts" : "js" } ` ) ;
194+ const webhookContent = clerkWebhookRoute ( framework , language ) ;
195+ fs . writeFileSync ( webhookPath , webhookContent ) ;
196+
197+ const userModelDir = path . join ( srcDir , "lib" , "database" , "models" ) ;
198+ fs . mkdirSync ( userModelDir , { recursive : true } ) ; // ✅ ensure folder exists
199+ const userModelPath = path . join ( userModelDir , `user.model.${ language === "ts" ? "ts" : "js" } ` ) ;
200+ const clerkModelContent = clerkUserModel ( language ) ;
201+ fs . writeFileSync ( userModelPath , clerkModelContent ) ;
202+
203+ const actionDir = path . join ( srcDir , "lib" , "actions" ) ;
204+ fs . mkdirSync ( actionDir , { recursive : true } ) ; // ✅ ensure folder exists
205+ const actionControllerPath = path . join ( actionDir , `user.action.${ language === "ts" ? "ts" : "js" } ` ) ;
206+ const actionControllerContent = clerkUserController ( framework , language ) ;
207+ fs . writeFileSync ( actionControllerPath , actionControllerContent ) ;
208+
209+ console . log ( chalk . green ( "Successfully setup syncing data setup for clerk data with database!" ) ) ;
210+ }
211+ }
163212
164- console . log ( chalk . green . bold ( '\n🎉 Astro Clerk setup completed!' ) ) ;
213+ console . log ( chalk . green . bold ( '\nAstro Clerk setup completed!' ) ) ;
165214 console . log ( chalk . yellow ( 'Update your PUBLIC_CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY in .env.local' ) ) ;
166215 console . log ( chalk . yellow ( 'Get your api by accessing official page of clerk.' ) ) ;
167216
168217 } catch ( err ) {
169- console . error ( chalk . red ( "✨ Error setting up Clerk for authentication" ) , err . message ) ;
218+ console . error ( chalk . red ( "Error setting up Clerk for authentication" ) , err . message ) ;
170219 console . log ( chalk . yellow ( "You may need to finish clerk setup manually." ) ) ;
171220 }
172221
0 commit comments