diff --git a/docs/README.md b/docs/README.md index 4701090..f9af499 100644 --- a/docs/README.md +++ b/docs/README.md @@ -54,6 +54,7 @@ All commands return a `Promise({object})` with stdout and stderr strings and an exitCode: 0, // !== 0 in case of an error } ``` +Although the return type is a `Promise`, it is still possible to get the process progres before the `Promise` resolves, by passing a callback function to the optional `callback` parameter. ### Example @@ -66,6 +67,19 @@ compose.upAll({ cwd: path.join(__dirname), log: true }) err => { console.log('something went wrong:', err.message)} ); ``` +To get process progres +```typescript +compose.upAll({ + cwd: path.join(__dirname), + callback: (chunk: Buffer) => { + console.log('job in progres: ', chunk.ToString()) + } + }) + .then( + () => { console.log('job done')}, + err => { console.log('something went wrong:', err.message)} + ); +``` To execute command inside a running container ```javascript @@ -82,6 +96,7 @@ compose.exec('node', 'npm install', { cwd: path.join(__dirname) }) * `configAsString {string}`: configuration can be provided as is, instead of relying on a file. In case `configAsString` is provided `config` will be ignored. * `[log] {boolean}`: optional setting to enable console logging (output of `docker-compose` `stdout`/`stderr` output) * `[composeOptions] string[]|Array void`: optional callback function, that provides infromation about the process while it is still runing. * `[commandOptions] string[]|Array void } export type DockerComposePortResult = { @@ -181,10 +182,12 @@ export const execCompose = ( childProc.stdout.on('data', (chunk): void => { result.out += chunk.toString() + options.callback?.(chunk, 'stdout') }) childProc.stderr.on('data', (chunk): void => { result.err += chunk.toString() + options.callback?.(chunk, 'stderr') }) childProc.on('exit', (exitCode): void => { diff --git a/test/index.test.ts b/test/index.test.ts index eba01e8..b243734 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -723,3 +723,14 @@ test('parse ps output', () => { ] }) }) + +test('ensure progress callback is called', async (): Promise => { + const config = { + cwd: path.join(__dirname), + config: './docker-compose.yml', + callback: jest.fn() + } + await compose.upAll(config) + expect(config.callback).toBeCalled() + await compose.down(config) +})