feat: more stuff

This commit is contained in:
Hamcha 2024-04-07 20:32:00 +02:00
parent 0a1b1517df
commit f66e46850f
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
4 changed files with 69 additions and 7 deletions

12
src/components/network.ts Normal file
View file

@ -0,0 +1,12 @@
import { DefinitionsNetwork } from "../compose-schema";
export class Network {
constructor(
public readonly name: string,
private options: DefinitionsNetwork
) {}
toCompose(): DefinitionsNetwork {
return this.options;
}
}

View file

@ -1,5 +1,12 @@
import { DefinitionsService } from "../compose-schema.ts";
export class Service {
constructor(public name: string, public options: DefinitionsService) {}
constructor(
public readonly name: string,
private options: DefinitionsService
) {}
toCompose(): DefinitionsService {
return this.options;
}
}

View file

@ -1,9 +1,26 @@
import { ComposeSpecification, DefinitionsService } from "../compose-schema.ts";
import {
ComposeSpecification,
DefinitionsNetwork,
DefinitionsService,
DefinitionsVolume,
} from "../compose-schema.ts";
import { registerDeployment } from "../deployment.ts";
import { Network } from "./network.ts";
import { Service } from "./service.ts";
import { Volume } from "./volume.ts";
function mapRecordToCompose<T, F extends { toCompose(): T }>(
obj: Record<string, F>
): Record<string, T> {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k, v.toCompose()])
);
}
export class Stack {
private services: Record<string, Service> = {};
private networks: Record<string, Network> = {};
private volumes: Record<string, Volume> = {};
constructor(public name: string) {
registerDeployment(this);
@ -18,14 +35,28 @@ export class Stack {
return this.services[name];
}
public toComposeFile(): ComposeSpecification {
const services: Record<string, DefinitionsService> = {};
for (const [name, service] of Object.entries(this.services)) {
services[name] = service.options;
public addNetwork(name: string, network: DefinitionsNetwork): Network {
if (this.networks[name]) {
throw new Error(`Network ${name} already defined.`);
}
this.networks[name] = new Network(name, network);
return this.networks[name];
}
public addVolume(name: string, volume: DefinitionsVolume): Volume {
if (this.volumes[name]) {
throw new Error(`Volume ${name} already defined.`);
}
this.volumes[name] = new Volume(name, volume);
return this.volumes[name];
}
public toComposeFile(): ComposeSpecification {
return {
version: "3.8",
services,
services: mapRecordToCompose(this.services),
networks: mapRecordToCompose(this.networks),
volumes: mapRecordToCompose(this.volumes),
};
}
}

12
src/components/volume.ts Normal file
View file

@ -0,0 +1,12 @@
import { DefinitionsVolume } from "../compose-schema";
export class Volume {
constructor(
public readonly name: string,
private options: DefinitionsVolume
) {}
toCompose(): DefinitionsVolume {
return this.options;
}
}