diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b676ab1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Next + +## :tada: Enhancements + +* [#58](https://github.com/PDMLab/docker-compose/issues/58): Expose docker-compose exit code in results diff --git a/index.d.ts b/index.d.ts index 29804fc..ede1e55 100644 --- a/index.d.ts +++ b/index.d.ts @@ -63,6 +63,7 @@ interface IDockerComposePushOptions extends IDockerComposeOptions { } interface IDockerComposeResult { + exitCode: ?number; out: string; err: string; } diff --git a/index.js b/index.js index 2e0b54c..11a29c2 100644 --- a/index.js +++ b/index.js @@ -67,6 +67,7 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) => }); const result = { + exitCode: null, err: '', out: '' }; @@ -79,7 +80,8 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) => result.err += chunk.toString(); }); - childProc.on('close', () => { + childProc.on('exit', exitCode => { + result.exitCode = exitCode; resolve(result); }); diff --git a/readme.md b/readme.md index 83f60ef..d5d5fe5 100644 --- a/readme.md +++ b/readme.md @@ -39,11 +39,12 @@ npm install --save-dev docker-compose * `configServices(options)` - Returns list of services defined in configuration files * `configVolumes(options)` - Returns list of volumes defined in configuration files -All commands return a `Promise({object})` with an stdout and stderr strings +All commands return a `Promise({object})` with stdout and stderr strings and an exit code: ```javascript { - out: 'stdout contents' - err: 'stderr contents' + out: 'stdout contents', + err: 'stderr contents', + exitCode: 0, // !== 0 in case of an error } ``` diff --git a/test/index.js b/test/index.js index c344ade..346a24e 100644 --- a/test/index.js +++ b/test/index.js @@ -53,6 +53,12 @@ test('ensure container gets started', async assert => { assert.end(); }); +test('ensure exit code is returned correctly', async assert => { + assert.equal(0, (await compose.down({ cwd: path.join(__dirname), log: true })).exitCode); + assert.equal(0, (await compose.upAll({ cwd: path.join(__dirname), log: true })).exitCode); + assert.equal(1, (await compose.logs('non_existent_service', { cwd: path.join(__dirname) })).exitCode); +}); + test('ensure container gets started with --build option', async assert => { await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' }); await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', commandOptions: [ '--build' ]});