feat: make result for port command type safe

This commit is contained in:
Alexander Zeitler 2021-04-11 01:10:18 +02:00
parent b6eec0047d
commit 70a98f473b
3 changed files with 33 additions and 6 deletions

View file

@ -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<T> = {
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<IDockerComposeResult> {
): Promise<TypedDockerComposeResult<DockerComposePortResult>> {
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 (

View file

@ -8,4 +8,4 @@ services:
environment:
NGINX_PORT: 8888
ports:
- "8888"
- 8888:8888

View file

@ -370,9 +370,10 @@ test('build accepts config as string', async (): Promise<void> => {
}
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)
})