From 3467e79244297d2b4c62730d4f03662a7a2965ba Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Mon, 29 Apr 2019 15:07:33 +0200 Subject: [PATCH 1/7] Expose docker-compose exit code in results --- index.d.ts | 1 + index.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 29804fc..f9e377f 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..996bd01 100644 --- a/index.js +++ b/index.js @@ -67,6 +67,7 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) => }); const result = { + exitCode: 0, err: '', out: '' }; @@ -79,7 +80,8 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) => result.err += chunk.toString(); }); - childProc.on('close', () => { + childProc.on('close', (exitCode, _) => { + result.exitCode = exitCode; resolve(result); }); From 134752cb80a245d1afd08008cfb541c1e199df03 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Mon, 29 Apr 2019 15:18:28 +0200 Subject: [PATCH 2/7] Add test for returning the correct exit code --- test/index.js | 6 ++++++ 1 file changed, 6 insertions(+) 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' ]}); From 1a0c9af4a6867619ec28b68e5bf79c525d8635bf Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Mon, 29 Apr 2019 15:35:00 +0200 Subject: [PATCH 3/7] Add exit code information to the README --- readme.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 } ``` From d13a85bd76c8457b46f33bd63e8a97f2264bf73b Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Tue, 30 Apr 2019 18:01:14 +0200 Subject: [PATCH 4/7] Allow exitCode to be null and initialize it to null as well --- index.d.ts | 2 +- index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index f9e377f..ede1e55 100644 --- a/index.d.ts +++ b/index.d.ts @@ -63,7 +63,7 @@ interface IDockerComposePushOptions extends IDockerComposeOptions { } interface IDockerComposeResult { - exitCode: number; + exitCode: ?number; out: string; err: string; } diff --git a/index.js b/index.js index 996bd01..c8f1826 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,7 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) => }); const result = { - exitCode: 0, + exitCode: null, err: '', out: '' }; From 3a8b74665d7733bcd4d1091990df0909016bfb2e Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Tue, 30 Apr 2019 18:01:53 +0200 Subject: [PATCH 5/7] Grab exit code from 'exit' event of the child process --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c8f1826..365195a 100644 --- a/index.js +++ b/index.js @@ -80,8 +80,11 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) => result.err += chunk.toString(); }); - childProc.on('close', (exitCode, _) => { - result.exitCode = exitCode; + childProc.on('exit', exitCode => { + result.exitCode = exitCode + }) + + childProc.on('close', () => { resolve(result); }); From 1a85ccacf4037196c4a5960766a5150a7a0cbc2d Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Tue, 30 Apr 2019 18:45:42 +0200 Subject: [PATCH 6/7] Resolve execCompose future on exit instead of close --- index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.js b/index.js index 365195a..11a29c2 100644 --- a/index.js +++ b/index.js @@ -81,10 +81,7 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) => }); childProc.on('exit', exitCode => { - result.exitCode = exitCode - }) - - childProc.on('close', () => { + result.exitCode = exitCode; resolve(result); }); From 594d828a90cc8a3d2ebd076ff6d4528b2273e223 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Tue, 30 Apr 2019 19:25:41 +0200 Subject: [PATCH 7/7] Add CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 CHANGELOG.md 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