You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.5 KiB
TypeScript

import type { Deploy, GitRef } from './config'
function splitBinding(pair: string): [string, string] {
const parts = pair.trim().split(':')
if (parts.length !== 2) {
throw new Error(`invalid binding format`)
}
const [external, internal] = parts
return [external, internal]
}
function cutString(s: string, sep: string): [string, string] {
return [s.slice(0, s.indexOf(sep)), s.slice(s.indexOf(sep) + sep.length)]
}
function notEmpty(s: string, message: string) {
if (s.trim().length === 0) {
throw new Error(message)
}
return s
}
export function parseRef(form: Record<string, any>): GitRef {
const type = form['deploy-ref-type']
const value = form['deploy-ref-value']
return type === 'default' ? { type } : { type, value }
}
export function parseDeploy(form: Record<string, any>): Deploy {
const name = form['deploy-name']
const type = form['deploy-type']
if (type === 'docker') {
// const url = notEmpty(form['deploy-url'], 'must provide an url')
const containerName = form['deploy-options-name'] as string
const image = notEmpty(form['deploy-options-image'], 'must provide an image name')
const ports = form['deploy-options-ports'] as string
const env = form['deploy-options-env'] as string
const volumes = form['deploy-options-volumes'] as string
return {
name,
type: 'docker',
options: {
image,
name: containerName,
env: Object.fromEntries(env.split(/\n/g).map(line => cutString(line.trim(), '='))),
ports: ports.split(/\n/g).map(splitBinding),
volumes: volumes.split(/\n/g).map(splitBinding),
},
}
}
if (type === 'shell') {
const path = form['deploy-options-path'] as string
const env = form['deploy-options-env'] as string
const url = form['deploy-url'] as string
const ref = parseRef(form)
return {
name,
url,
ref,
type: 'shell',
options: {
path,
env: Object.fromEntries(env.split(/\n/g).map(line => cutString(line.trim(), '='))),
},
}
}
if (type === 'dockerfile') {
const url = form['deploy-url'] as string
const ref = parseRef(form)
const containerName = form['deploy-options-name'] as string
const path = form['deploy-options-path'] as string
const ports = form['deploy-options-ports'] as string
const env = form['deploy-options-env'] as string
const volumes = form['deploy-options-volumes'] as string
return {
name,
url,
ref,
type: 'dockerfile',
options: {
name: containerName,
path,
env: Object.fromEntries(env.split(/\n/g).map(line => cutString(line.trim(), '='))),
ports: ports.split(/\n/g).map(splitBinding),
volumes: volumes.split(/\n/g).map(splitBinding),
},
}
}
if (type === 'docker-compose') {
const url = form['deploy-url'] as string
const ref = parseRef(form)
const path = form['deploy-options-path'] as string
return {
name,
url,
ref,
type: 'docker-compose',
options: {
path,
},
}
}
throw new Error('invalid deploy type')
}