import type { Deploy, GitRef } from './config' 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): GitRef { const type = form['deploy-ref-type'] const value = form['deploy-ref-value'] return type === 'default' ? { type } : { type, value } } export function parseDeploy(form: Record): 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(line => line.trim()), volumes: volumes.split(/\n/g).map(line => line.trim()), }, } } 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 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: { path, env: Object.fromEntries(env.split(/\n/g).map(line => cutString(line.trim(), '='))), ports: ports.split(/\n/g).map(line => line.trim()), volumes: volumes.split(/\n/g).map(line => line.trim()), }, } } 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') }