feat: make result for config command type safe

This commit is contained in:
Alexander Zeitler 2021-04-11 15:39:24 +02:00
parent a7da0380a8
commit a2f5a4ec0e
4 changed files with 30 additions and 7 deletions

View file

@ -111,5 +111,8 @@
"extends": [
"@commitlint/config-conventional"
]
},
"dependencies": {
"yaml": "^1.10.2"
}
}

View file

@ -1,4 +1,5 @@
import childProcess from 'child_process'
import yaml from 'yaml'
export interface IDockerComposeOptions {
cwd?: string
config?: string | string[]
@ -18,6 +19,14 @@ export type DockerComposeVersionResult = {
version: string
}
export type DockerComposeConfigResult = {
config: {
version: Record<string, string>
services: Record<string, string | Record<string, string>>
volumes: Record<string, string>
}
}
export interface IDockerComposeLogOptions extends IDockerComposeOptions {
follow?: boolean
}
@ -292,10 +301,19 @@ export const pullOne = function (
return execCompose('pull', [service], options)
}
export const config = function (
export const config = async function (
options?: IDockerComposeOptions
): Promise<IDockerComposeResult> {
return execCompose('config', [], options)
): Promise<TypedDockerComposeResult<DockerComposeConfigResult>> {
try {
const result = await execCompose('config', [], options)
const config = yaml.parse(result.out)
return {
...result,
data: { config }
}
} catch (error) {
return Promise.reject(error)
}
}
export const configServices = function (

View file

@ -514,9 +514,11 @@ test('config show data for docker-compose files', async (): Promise<void> => {
config: 'docker-compose-42.yml'
})
expect(std.err).toBeFalsy()
expect(std.out.includes('some-service')).toBeTruthy()
expect(std.out.includes('test/volume:/mountedvolume:rw')).toBeTruthy()
expect(std.data.config.version).toBe('3')
expect(std.data.config.services['some-service']['image']).toBe(
'nginx:1.19.9-alpine'
)
expect(std.data.config.volumes['db-data']).toEqual({})
})
test('config show data for docker-compose files (services)', async (): Promise<void> => {

View file

@ -8920,7 +8920,7 @@ yallist@^4.0.0:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
yaml@^1.10.0:
yaml@^1.10.0, yaml@^1.10.2:
version "1.10.2"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==