@@ -101,7 +101,7 @@ export class Stack {
101101 * Get the status of the stack from `docker compose ps --format json`
102102 */
103103 async ps ( ) : Promise < object > {
104- let res = await childProcessAsync . spawn ( "docker" , [ "compose" , " ps", "--format" , "json" ] , {
104+ let res = await childProcessAsync . spawn ( "docker" , this . getComposeOptions ( " ps", "--format" , "json" ) , {
105105 cwd : this . path ,
106106 encoding : "utf-8" ,
107107 } ) ;
@@ -117,7 +117,7 @@ export class Stack {
117117
118118 get isGitRepo ( ) : boolean {
119119 try {
120- execSync ( "git rev-parse --is-inside-work-tree" , { cwd : this . path } ) ;
120+ execSync ( "git rev-parse --is-inside-work-tree" , { cwd : this . path } ) ;
121121 return true ;
122122 } catch ( error ) {
123123 return false ;
@@ -127,7 +127,7 @@ export class Stack {
127127 get gitUrl ( ) : string {
128128 if ( this . isGitRepo ) {
129129 try {
130- let stdout = execSync ( "git config --get remote.origin.url" , { cwd : this . path } ) ;
130+ let stdout = execSync ( "git config --get remote.origin.url" , { cwd : this . path } ) ;
131131 return stdout . toString ( ) . trim ( ) ;
132132 } catch ( error ) {
133133 return "" ;
@@ -139,7 +139,7 @@ export class Stack {
139139 get branch ( ) : string {
140140 if ( this . isGitRepo ) {
141141 try {
142- let stdout = execSync ( "git branch --show-current" , { cwd : this . path } ) ;
142+ let stdout = execSync ( "git branch --show-current" , { cwd : this . path } ) ;
143143 return stdout . toString ( ) . trim ( ) ;
144144 } catch ( error ) {
145145 return "" ;
@@ -258,7 +258,7 @@ export class Stack {
258258
259259 async deploy ( socket : DockgeSocket ) : Promise < number > {
260260 const terminalName = getComposeTerminalName ( socket . endpoint , this . name ) ;
261- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " up", "-d" , "--remove-orphans" ] , this . path ) ;
261+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " up", "-d" , "--remove-orphans" ) , this . path ) ;
262262 if ( exitCode !== 0 ) {
263263 throw new Error ( "Failed to deploy, please check the terminal output for more information." ) ;
264264 }
@@ -267,7 +267,7 @@ export class Stack {
267267
268268 async delete ( socket : DockgeSocket ) : Promise < number > {
269269 const terminalName = getComposeTerminalName ( socket . endpoint , this . name ) ;
270- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " down", "--remove-orphans" ] , this . path ) ;
270+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " down", "--remove-orphans" ) , this . path ) ;
271271 if ( exitCode !== 0 ) {
272272 throw new Error ( "Failed to delete, please check the terminal output for more information." ) ;
273273 }
@@ -481,9 +481,22 @@ export class Stack {
481481 return stack ;
482482 }
483483
484+ getComposeOptions ( command : string , ...extraOptions : string [ ] ) {
485+ //--env-file ./../global.env --env-file .env
486+ let options = [ "compose" , command , ...extraOptions ] ;
487+ if ( fs . existsSync ( path . join ( this . server . stacksDir , "global.env" ) ) ) {
488+ if ( fs . existsSync ( path . join ( this . path , ".env" ) ) ) {
489+ options . splice ( 1 , 0 , "--env-file" , "./.env" ) ;
490+ }
491+ options . splice ( 1 , 0 , "--env-file" , "../global.env" ) ;
492+ }
493+ console . log ( options ) ;
494+ return options ;
495+ }
496+
484497 async start ( socket : DockgeSocket ) {
485498 const terminalName = getComposeTerminalName ( socket . endpoint , this . name ) ;
486- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " up", "-d" , "--remove-orphans" ] , this . path ) ;
499+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " up", "-d" , "--remove-orphans" ) , this . path ) ;
487500 if ( exitCode !== 0 ) {
488501 throw new Error ( "Failed to start, please check the terminal output for more information." ) ;
489502 }
@@ -492,7 +505,7 @@ export class Stack {
492505
493506 async stop ( socket : DockgeSocket ) : Promise < number > {
494507 const terminalName = getComposeTerminalName ( socket . endpoint , this . name ) ;
495- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " stop" ] , this . path ) ;
508+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " stop") , this . path ) ;
496509 if ( exitCode !== 0 ) {
497510 throw new Error ( "Failed to stop, please check the terminal output for more information." ) ;
498511 }
@@ -501,7 +514,7 @@ export class Stack {
501514
502515 async restart ( socket : DockgeSocket ) : Promise < number > {
503516 const terminalName = getComposeTerminalName ( socket . endpoint , this . name ) ;
504- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " restart" ] , this . path ) ;
517+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " restart") , this . path ) ;
505518 if ( exitCode !== 0 ) {
506519 throw new Error ( "Failed to restart, please check the terminal output for more information." ) ;
507520 }
@@ -510,7 +523,7 @@ export class Stack {
510523
511524 async down ( socket : DockgeSocket ) : Promise < number > {
512525 const terminalName = getComposeTerminalName ( socket . endpoint , this . name ) ;
513- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " down" ] , this . path ) ;
526+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " down") , this . path ) ;
514527 if ( exitCode !== 0 ) {
515528 throw new Error ( "Failed to down, please check the terminal output for more information." ) ;
516529 }
@@ -522,13 +535,13 @@ export class Stack {
522535
523536 if ( this . isGitRepo ) {
524537 // TODO: error handling e.g. local changes
525- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "git " , [ "pull" ] , this . path ) ;
538+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker " , this . getComposeOptions ( "pull" ) , this . path ) ;
526539 if ( exitCode !== 0 ) {
527540 throw new Error ( "Failed to update, please check the terminal output for more information." ) ;
528541 }
529542 }
530543
531- let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " pull" ] , this . path ) ;
544+ let exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " pull") , this . path ) ;
532545 if ( exitCode !== 0 ) {
533546 throw new Error ( "Failed to pull, please check the terminal output for more information." ) ;
534547 }
@@ -540,7 +553,7 @@ export class Stack {
540553 return exitCode ;
541554 }
542555
543- exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , [ "compose" , " up", "-d" , "--remove-orphans" ] , this . path ) ;
556+ exitCode = await Terminal . exec ( this . server , socket , terminalName , "docker" , this . getComposeOptions ( " up", "-d" , "--remove-orphans" ) , this . path ) ;
544557 if ( exitCode !== 0 ) {
545558 throw new Error ( "Failed to restart, please check the terminal output for more information." ) ;
546559 }
@@ -597,7 +610,7 @@ export class Stack {
597610
598611 async joinCombinedTerminal ( socket : DockgeSocket ) {
599612 const terminalName = getCombinedTerminalName ( socket . endpoint , this . name ) ;
600- const terminal = Terminal . getOrCreateTerminal ( this . server , terminalName , "docker" , [ "compose" , " logs", "-f" , "--tail" , "100" ] , this . path ) ;
613+ const terminal = Terminal . getOrCreateTerminal ( this . server , terminalName , "docker" , this . getComposeOptions ( " logs", "-f" , "--tail" , "100" ) , this . path ) ;
601614 terminal . enableKeepAlive = true ;
602615 terminal . rows = COMBINED_TERMINAL_ROWS ;
603616 terminal . cols = COMBINED_TERMINAL_COLS ;
@@ -618,7 +631,7 @@ export class Stack {
618631 let terminal = Terminal . getTerminal ( terminalName ) ;
619632
620633 if ( ! terminal ) {
621- terminal = new InteractiveTerminal ( this . server , terminalName , "docker" , [ "compose" , " exec", serviceName , shell ] , this . path ) ;
634+ terminal = new InteractiveTerminal ( this . server , terminalName , "docker" , this . getComposeOptions ( " exec", serviceName , shell ) , this . path ) ;
622635 terminal . rows = TERMINAL_ROWS ;
623636 log . debug ( "joinContainerTerminal" , "Terminal created" ) ;
624637 }
@@ -631,7 +644,7 @@ export class Stack {
631644 let statusList = new Map < string , Array < object > > ( ) ;
632645
633646 try {
634- let res = await childProcessAsync . spawn ( "docker" , [ "compose" , " ps", "--format" , "json" ] , {
647+ let res = await childProcessAsync . spawn ( "docker" , this . getComposeOptions ( " ps", "--format" , "json" ) , {
635648 cwd : this . path ,
636649 encoding : "utf-8" ,
637650 } ) ;
@@ -642,12 +655,15 @@ export class Stack {
642655
643656 let lines = res . stdout ?. toString ( ) . split ( "\n" ) ;
644657
645- const addLine = ( obj : { Service : string , State : string , Name : string , Health : string } ) => {
658+ const addLine = ( obj : { Service : string , State : string , Name : string , Health : string , Ports : string } ) => {
646659 if ( ! statusList . has ( obj . Service ) ) {
647660 statusList . set ( obj . Service , [ ] ) ;
648661 }
649662 statusList . get ( obj . Service ) ?. push ( {
650663 status : obj . Health || obj . State ,
664+ ports : obj . Ports . split ( / , \s * / ) . filter ( ( s ) => {
665+ return s . indexOf ( "->" ) >= 0 ;
666+ } ) ,
651667 name : obj . Name
652668 } ) ;
653669 } ;
0 commit comments