From a7da0380a8464bbd2efc2c16b70b67d4fdf77c24 Mon Sep 17 00:00:00 2001 From: Alexander Zeitler Date: Sun, 11 Apr 2021 15:17:14 +0200 Subject: [PATCH] feat: make result for `version` command type safe --- src/index.ts | 19 ++++++++++++++++--- test/index.test.ts | 4 ++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index b3e62edd..efc0a516 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,10 @@ export type DockerComposePortResult = { port: number } +export type DockerComposeVersionResult = { + version: string +} + export interface IDockerComposeLogOptions extends IDockerComposeOptions { follow?: boolean } @@ -377,8 +381,17 @@ export const port = async function ( } } -export const version = function ( +export const version = async function ( options?: IDockerComposeOptions -): Promise { - return execCompose('version', ['--short'], options) +): Promise> { + try { + const result = await execCompose('version', ['--short'], options) + const version = result.out.replace('\n', '') + return { + ...result, + data: { version } + } + } catch (error) { + return Promise.reject(error) + } } diff --git a/test/index.test.ts b/test/index.test.ts index 3a96b792..7d83e287 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -641,7 +641,7 @@ test('removes container', async (): Promise => { }) test('returns version information', async (): Promise => { - const version = await compose.version() + const version = await (await compose.version()).data.version - expect(version.out).toBeTruthy() + expect(version).toMatch(/^(\d+\.)?(\d+\.)?(\*|\d+)$/) })