mirror of
https://github.com/danbulant/docker-compose
synced 2026-05-27 14:01:46 +00:00
refactor: replace if by ternary, improve variable naming
This commit is contained in:
parent
880d2522b5
commit
3be99e8360
1 changed files with 23 additions and 20 deletions
19
src/index.ts
19
src/index.ts
|
|
@ -80,29 +80,32 @@ export const mapPorts = (
|
||||||
mapped?: { address: string; port: number }
|
mapped?: { address: string; port: number }
|
||||||
exposed: { port: number; protocol: string }
|
exposed: { port: number; protocol: string }
|
||||||
}> => {
|
}> => {
|
||||||
if (!ports) return []
|
const result = !ports
|
||||||
|
? []
|
||||||
|
: (() => {
|
||||||
const allPorts = ports.split(',')
|
const allPorts = ports.split(',')
|
||||||
return allPorts.map((portString) => {
|
return allPorts.map((untypedPort) => {
|
||||||
const exposedFragments = portString.trim().split('->')
|
const exposedFragments = untypedPort.trim().split('->')
|
||||||
const [port, protocol] =
|
const [port, protocol] =
|
||||||
exposedFragments.length === 1
|
exposedFragments.length === 1
|
||||||
? exposedFragments[0].split('/')
|
? exposedFragments[0].split('/')
|
||||||
: exposedFragments[1].split('/')
|
: exposedFragments[1].split('/')
|
||||||
const [address, mappedPort] =
|
const [address, mappedPort] =
|
||||||
exposedFragments.length === 2 ? exposedFragments[0].split(':') : []
|
exposedFragments.length === 2 ? exposedFragments[0].split(':') : []
|
||||||
const result = {
|
return {
|
||||||
exposed: { port: Number(port), protocol },
|
exposed: { port: Number(port), protocol },
|
||||||
...(address &&
|
...(address &&
|
||||||
mappedPort && { mapped: { port: Number(mappedPort), address } })
|
mappedPort && { mapped: { port: Number(mappedPort), address } })
|
||||||
}
|
}
|
||||||
return result
|
|
||||||
})
|
})
|
||||||
|
})()
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export const mapPsOutput = (output: string): DockerComposePsResult => {
|
export const mapPsOutput = (output: string): DockerComposePsResult => {
|
||||||
const lines = output.split(`\n`).filter(nonEmptyString)
|
const allLines = output.split(`\n`).filter(nonEmptyString)
|
||||||
const lines2 = lines.filter((line, index) => index > 1)
|
const linesWithServices = allLines.filter((_, index) => index > 1)
|
||||||
const services = lines2.map((line) => {
|
const services = linesWithServices.map((line) => {
|
||||||
const [nameString, commandString, stateString, allPortsString] = line.split(
|
const [nameString, commandString, stateString, allPortsString] = line.split(
|
||||||
/\s{3,}/
|
/\s{3,}/
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue