UNPKG

@wocker/core

Version:
201 lines (200 loc) 5.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PROJECT_TYPE_COMPOSE = exports.PROJECT_TYPE_PRESET = exports.PROJECT_TYPE_IMAGE = exports.PROJECT_TYPE_DOCKERFILE = exports.Project = void 0; const volumeParse_1 = require("../utils/volumeParse"); class Project { constructor(data) { Object.assign(this, data); } get containerName() { return `${this.name}.workspace`; } get domains() { const host = this.getEnv("VIRTUAL_HOST"); if (!host) { return []; } return host.split(","); } hasDomain(domain) { return this.domains.includes(domain); } addDomain(addDomain) { let domains = [ ...this.domains.filter((domain) => { return domain !== addDomain; }), addDomain ]; this.setEnv("VIRTUAL_HOST", domains.join(",")); } removeDomain(removeDomain) { if (!this.hasDomain(removeDomain)) { return; } let domains = this.domains.filter((domain) => { return domain !== removeDomain; }); this.setEnv("VIRTUAL_HOST", domains.join(",")); } clearDomains() { this.unsetEnv("VIRTUAL_HOST"); } linkPort(hostPort, containerPort) { if (!this.ports) { this.ports = []; } this.ports = [ ...this.ports.filter((link) => { return link !== `${hostPort}:${containerPort}`; }), `${hostPort}:${containerPort}` ]; } unlinkPort(hostPort, containerPort) { if (!this.ports) { return; } this.ports = this.ports.filter((link) => { return link !== `${hostPort}:${containerPort}`; }); if (this.ports.length === 0) { delete this.ports; } } hasEnv(name) { if (!this.env) { return false; } return this.env.hasOwnProperty(name); } getEnv(name, defaultValue) { const { [name]: value = defaultValue } = this.env || {}; return value; } setEnv(name, value) { if (!this.env) { this.env = {}; } this.env[name] = typeof value === "boolean" ? (value ? "true" : "false") : value; } unsetEnv(name) { if (!this.env) { return; } if (name in this.env) { delete this.env[name]; } if (Object.keys(this.env).length === 0) { delete this.env; } } hasMeta(name) { return !!this.metadata && this.metadata.hasOwnProperty(name); } getMeta(name, defaultValue) { const { [name]: value = defaultValue } = this.metadata || {}; return value; } setMeta(name, value) { if (!this.metadata) { this.metadata = {}; } this.metadata[name] = typeof value === "boolean" ? (value ? "true" : "false") : value; } unsetMeta(name) { if (!this.metadata) { return; } if (name in this.metadata) { delete this.metadata[name]; } if (Object.keys(this.metadata).length === 0) { delete this.metadata; } } getVolumeBySource(source) { return (this.volumes || []).find((volume) => { return (0, volumeParse_1.volumeParse)(volume).source === source; }); } getVolumeByDestination(destination) { return (this.volumes || []).find((volume) => { return (0, volumeParse_1.volumeParse)(volume).destination === destination; }); } volumeMount(...volumes) { if (volumes.length === 0) { return; } const [volume, ...restVolumes] = volumes; const { destination } = (0, volumeParse_1.volumeParse)(volume); this.volumes = [ ...(this.volumes || []).filter((v) => { return v !== this.getVolumeByDestination(destination); }), volume ]; this.volumeMount(...restVolumes); } volumeUnmount(...volumes) { if (!this.volumes || volumes.length === 0) { return; } const [volume, ...restVolumes] = volumes; const v = (0, volumeParse_1.volumeParse)(volume); this.volumes = this.volumes.filter((mounted) => { const m = (0, volumeParse_1.volumeParse)(mounted); return v.source !== m.source && v.destination !== m.destination; }); if (this.volumes.length === 0) { delete this.volumes; return; } this.volumeUnmount(...restVolumes); } addExtraHost(host, domain) { if (!this.extraHosts) { this.extraHosts = {}; } this.extraHosts[host] = domain; } removeExtraHost(host) { if (!this.extraHosts || !this.extraHosts[host]) { return; } delete this.extraHosts[host]; if (Object.keys(this.extraHosts).length === 0) { delete this.extraHosts; } } toObject() { return { id: this.id, name: this.name, type: this.type, path: this.path, preset: this.preset, presetMode: this.presetMode, imageName: this.imageName, dockerfile: this.dockerfile, composefile: this.composefile, scripts: this.scripts, ports: this.ports, volumes: this.volumes, env: this.env, buildArgs: this.buildArgs, extraHosts: this.extraHosts, metadata: this.metadata }; } } exports.Project = Project; exports.PROJECT_TYPE_DOCKERFILE = "dockerfile"; exports.PROJECT_TYPE_IMAGE = "image"; exports.PROJECT_TYPE_PRESET = "preset"; exports.PROJECT_TYPE_COMPOSE = "compose";