From cb10f66295f10fd1752d04e4650ea010bb0fe09b Mon Sep 17 00:00:00 2001 From: Lars Kumbier Date: Thu, 11 Apr 2019 12:10:45 +0200 Subject: [PATCH] Add port command --- index.d.ts | 1 + index.js | 16 ++++++++++++++++ test/docker-compose-2.yml | 4 +++- test/index.js | 12 ++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 86c792b..168c601 100644 --- a/index.d.ts +++ b/index.d.ts @@ -18,6 +18,7 @@ declare module "docker-compose" { buildOne(service:String, options: IDockerComposeOptions): Promise; ps(options: IDockerComposeOptions): Promise; push(options: IDockerComposePushOptions): Promise; + port(service:String, containerPort:String|Number, options: IDockerComposeOptions): Promise; } interface IDockerComposeOptions { diff --git a/index.js b/index.js index 2cc4122..26116d9 100644 --- a/index.js +++ b/index.js @@ -359,6 +359,21 @@ const logs = function (service, options) { return execCompose('logs', args, options); }; +/** + * @param {string} service + * @param {string|number} containerPort + * @param {object} options + * @param {string} options.cwd + * @param {boolean} [options.log] + * @param {?(string|string[])} [options.config] + * @param {?object} [options.env] + * @param {?(string[]|Array)} [options.composeOptions] + */ +const port = function(service, containerPort, options) { + const args = [service, containerPort] + return execCompose('port', args, options) +}; + module.exports = { upAll, upMany, @@ -378,4 +393,5 @@ module.exports = { buildOne, ps, push, + port, }; diff --git a/test/docker-compose-2.yml b/test/docker-compose-2.yml index 9b5883b..1ee220b 100644 --- a/test/docker-compose-2.yml +++ b/test/docker-compose-2.yml @@ -3,4 +3,6 @@ version: '2' services: db: image: mongo:3.4 - container_name: compose_test_mongodb_2 \ No newline at end of file + container_name: compose_test_mongodb_2 + ports: + - "5432" diff --git a/test/index.js b/test/index.js index 3594ad3..4c3e814 100644 --- a/test/index.js +++ b/test/index.js @@ -321,3 +321,15 @@ test('logs does follow service logs', async assert => { assert.true(await isContainerRunning('/compose_test_mongodb')); assert.end(); }); + +test('returns the port for a started service', async assert => { + const config = { + cwd: path.join(__dirname), + config: './docker-compose-2.yml' + } + await compose.upAll(config); + const port = await compose.port('db', 5432, config); + + assert.true(port.out.match(/.*:[0-9]{1,5}/)); + assert.end(); +})