@@ -43,7 +43,8 @@ Examples:
4343 ${ formatCommand ( 'nile local start' , '--no-prompt' ) } Start without prompting for psql connection
4444 ${ formatCommand ( 'nile local stop' ) } Stop local development environment
4545 ${ formatCommand ( 'nile local info' ) } Show connection information
46-
46+ ${ formatCommand ( 'nile local connect --psql' ) } Connect to local database using psql
47+
4748${ getGlobalOptionsHelp ( ) } `) ;
4849
4950 local
@@ -314,5 +315,86 @@ ${getGlobalOptionsHelp()}`);
314315 }
315316 } ) ;
316317
318+ local
319+ . command ( 'connect' )
320+ . description ( 'Connect to local database' )
321+ . requiredOption ( '--psql' , 'Connect using PostgreSQL CLI' )
322+ . action ( async ( cmdOptions ) => {
323+ try {
324+ // Check if container is running
325+ const { stdout } = await execAsync ( 'docker ps --filter name=nile-local --format {{.Names}}' ) ;
326+ if ( ! stdout . includes ( 'nile-local' ) ) {
327+ console . error ( theme . error ( '\nNo Nile local environment is currently running.' ) ) ;
328+ console . log ( theme . dim ( 'Start it with: nile local start' ) ) ;
329+ process . exit ( 1 ) ;
330+ }
331+
332+ const connectSpinner = ora ( {
333+ text : 'Connecting to local database...' ,
334+ color : 'cyan'
335+ } ) . start ( ) ;
336+
337+ try {
338+ // Add a delay to ensure the database is ready for connections
339+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
340+
341+ // Connect using psql with individual parameters
342+ const psql = spawn ( 'psql' , [
343+ '-h' , 'localhost' ,
344+ '-p' , '5432' ,
345+ '-U' , '00000000-0000-0000-0000-000000000000' ,
346+ '-d' , 'test' ,
347+ '-w' // Never prompt for password
348+ ] , {
349+ stdio : 'inherit' ,
350+ env : {
351+ ...process . env ,
352+ PGPASSWORD : 'password' // Set password via environment variable
353+ }
354+ } ) ;
355+
356+ // Stop the spinner immediately as psql will take over the terminal
357+ connectSpinner . stop ( ) ;
358+
359+ // Handle psql exit
360+ psql . on ( 'exit' , ( code ) => {
361+ if ( code !== 0 ) {
362+ console . error ( theme . error ( '\nFailed to connect using psql. Please check if psql is installed.' ) ) ;
363+ if ( getOptions ( ) . debug ) {
364+ console . error ( theme . dim ( 'Try connecting directly with:' ) ) ;
365+ console . error ( theme . dim ( 'PGPASSWORD=password psql -h localhost -p 5432 -U 00000000-0000-0000-0000-000000000000 -d test' ) ) ;
366+ }
367+ process . exit ( 1 ) ;
368+ }
369+ } ) ;
370+
371+ // Handle psql error
372+ psql . on ( 'error' , ( error ) => {
373+ connectSpinner . fail ( 'Failed to launch psql' ) ;
374+ console . error ( theme . error ( '\nError launching psql:' ) , error . message ) ;
375+ if ( error . message . includes ( 'ENOENT' ) ) {
376+ console . error ( theme . error ( 'Please make sure psql is installed and available in your PATH' ) ) ;
377+ }
378+ process . exit ( 1 ) ;
379+ } ) ;
380+
381+ } catch ( error : any ) {
382+ connectSpinner . fail ( 'Failed to connect to database' ) ;
383+ if ( getOptions ( ) . debug ) {
384+ console . error ( theme . error ( 'Error details:' ) , error . message ) ;
385+ }
386+ process . exit ( 1 ) ;
387+ }
388+ } catch ( error : any ) {
389+ const options = getOptions ( ) ;
390+ if ( options . debug ) {
391+ console . error ( theme . error ( '\nFailed to connect to database:' ) , error ) ;
392+ } else {
393+ console . error ( theme . error ( '\nFailed to connect to database:' ) , error . message || 'Unknown error' ) ;
394+ }
395+ process . exit ( 1 ) ;
396+ }
397+ } ) ;
398+
317399 return local ;
318400}
0 commit comments