11#!/usr/bin/env node
22
33const path = require ( 'path' ) ;
4- const { spawn } = require ( 'child_process' ) ;
4+ const { execSync , spawnSync } = require ( 'child_process' ) ;
55const program = require ( 'commander' ) ;
66const chalk = require ( 'chalk' ) ;
77const deepAssign = require ( 'deep-assign' ) ;
@@ -68,7 +68,7 @@ if (!packageConfig && !doprConfig) {
6868if ( typeof packageConfig . file === 'string' ) {
6969 packageConfig . file = [ packageConfig . file ] ;
7070}
71- if ( typeof doprConfig . file === 'string' ) {
71+ if ( doprConfig && typeof doprConfig . file === 'string' ) {
7272 doprConfig . file = [ doprConfig . file ] ;
7373}
7474
@@ -114,6 +114,7 @@ if (cliAction.exec && !cliAction.service) {
114114
115115const dockerComposeFiles = [ ] ;
116116
117+ // Validate/sanitize all docker-compose files!
117118cliAction . file . forEach ( ( file , pos ) => {
118119 file = path . resolve ( file ) ;
119120
@@ -130,41 +131,72 @@ cliAction.file.forEach((file, pos) => {
130131 dockerComposeFiles . push ( '--file' , file ) ;
131132} ) ;
132133
133- // Parse command
134- const cliCommand = cliAction . command
135- . replace ( '%action%' , action || '' )
136- . replace ( '%args%' , args . join ( ' ' ) )
137- . split ( ' ' ) ;
138-
139- // Args
140- const user = cliAction . user ? [ '--user' , cliAction . user ] : [ ] ;
141-
142- const cliArgs = dockerComposeFiles
143- . concat ( cliAction . exec ? [ 'exec' , ...user , cliAction . service ] : [ ] )
144- . concat ( cliAction . detached ? [ '-d' ] : [ ] )
145- . concat ( cliAction . privileged ? [ '--privileged' ] : [ ] )
146- . concat ( cliAction . index ? [ '--index' , cliAction . index ] : [ ] )
147- . concat ( cliCommand ) ;
148-
149- if ( program . verbose ) {
150- console . log ( chalk . gray ( 'ENV: ' + env ) ) ;
151- console . log ( chalk . gray ( 'CWD: ' + basePath ) ) ;
152- console . log ( chalk . gray ( 'CMD: docker-compose ' + cliArgs . join ( ' ' ) ) ) ;
134+ // Supporting array command so treat anything else as array as well.
135+ if ( typeof cliAction . command === 'string' ) {
136+ cliAction . command = [ cliAction . command ] ;
153137}
154138
155- // Fire!
156- const childProcess = spawn ( 'docker-compose' , cliArgs , {
139+ const cliOptions = {
157140 cwd : basePath ,
158- env,
159141 stdio : 'inherit' ,
160142 shell : true
161- } ) ;
143+ } ;
162144
163- childProcess . on ( 'close' , code => {
145+ const exitHandler = code => {
164146 if ( program . verbose ) {
165147 console . log ( ( code > 0 ? chalk . red : chalk . gray ) ( `command exited with code ${ code } ` ) ) ;
166148 }
167149
168150 // Pass through exit code
169- process . exit ( code ) ;
151+ if ( code !== 0 ) {
152+ process . exit ( code ) ;
153+ }
154+ } ;
155+
156+ // Run commands synchronously one after another!
157+ cliAction . command . forEach ( command => {
158+ if ( program . verbose ) {
159+ console . log ( chalk . gray ( 'ENV: ' + env ) ) ;
160+ console . log ( chalk . gray ( 'CWD: ' + basePath ) ) ;
161+ }
162+
163+ // Is it a reference to another action?
164+ if ( command [ 0 ] === '@' ) {
165+ const refArgs = dockerProjectArgs . slice ( 2 ) . concat ( command . substr ( 1 ) ) ;
166+
167+ if ( program . verbose ) {
168+ console . log ( chalk . gray ( 'CMD: dopr ' + refArgs . join ( ' ' ) ) ) ;
169+ }
170+
171+ // Fire!
172+ return exitHandler ( spawnSync ( 'dopr ' , refArgs , cliOptions ) . status ) ;
173+ }
174+
175+ // Command is expected to run in host context!
176+ if ( cliAction . service === '@host' ) {
177+ return execSync ( command , cliOptions ) ;
178+ }
179+
180+ // Parse command
181+ const cliCommand = command
182+ . replace ( '%action%' , action || '' )
183+ . replace ( '%args%' , args . join ( ' ' ) )
184+ . split ( ' ' ) ;
185+
186+ // Args
187+ const user = cliAction . user ? [ '--user' , cliAction . user ] : [ ] ;
188+
189+ const cliArgs = dockerComposeFiles
190+ . concat ( cliAction . exec ? [ 'exec' , ...user , cliAction . service ] : [ ] )
191+ . concat ( cliAction . detached ? [ '-d' ] : [ ] )
192+ . concat ( cliAction . privileged ? [ '--privileged' ] : [ ] )
193+ . concat ( cliAction . index ? [ '--index' , cliAction . index ] : [ ] )
194+ . concat ( cliCommand ) ;
195+
196+ if ( program . verbose ) {
197+ console . log ( chalk . gray ( 'CMD: docker-compose ' + cliArgs . join ( ' ' ) ) ) ;
198+ }
199+
200+ // Fire!
201+ exitHandler ( spawnSync ( 'docker-compose' , cliArgs , cliOptions ) . status ) ;
170202} ) ;
0 commit comments