diff --git a/src/index.ts b/src/index.ts index e2ba734e..13650af8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,11 @@ export interface IDockerComposeOptions { env?: NodeJS.ProcessEnv } +export type DockerComposePortResult = { + address: string + port: number +} + export interface IDockerComposeLogOptions extends IDockerComposeOptions { follow?: boolean } @@ -27,6 +32,13 @@ export interface IDockerComposeResult { err: string } +export type TypedDockerComposeResult = { + exitCode: number | null + out: string + err: string + result: T +} + /** * Converts supplied yml files to cli arguments * https://docs.docker.com/compose/reference/overview/#use--f-to-specify-name-and-path-of-one-or-more-compose-files @@ -343,14 +355,28 @@ export const logs = function ( return execCompose('logs', args, options) } -export const port = function ( +export const port = async function ( service: string, containerPort: string | number, options?: IDockerComposeOptions -): Promise { +): Promise> { const args = [service, containerPort] - return execCompose('port', args, options) + try { + const result = await execCompose('port', args, options) + const [address, port] = result.out.split(':') + return { + exitCode: result.exitCode, + out: result.out, + err: result.err, + result: { + address, + port: Number(port) + } + } + } catch (error) { + return Promise.reject(error) + } } export const version = function ( diff --git a/test/docker-compose-2.yml b/test/docker-compose-2.yml index 4b2ebf66..9c4ba266 100644 --- a/test/docker-compose-2.yml +++ b/test/docker-compose-2.yml @@ -8,4 +8,4 @@ services: environment: NGINX_PORT: 8888 ports: - - "8888" + - 8888:8888 diff --git a/test/index.test.ts b/test/index.test.ts index e54331f3..014f2cda 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -370,9 +370,10 @@ test('build accepts config as string', async (): Promise => { } await compose.upAll(config) - const port = await compose.port('web', 8888, config) + const result = await compose.port('web', 8888, config) - expect(port.out).toMatch(/.*:[0-9]{1,5}/) + expect(result.result.address).toBe('0.0.0.0') + expect(result.result.port).toBe(8888) await compose.down(config) })