This repository was archived by the owner on Aug 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.ts
More file actions
71 lines (62 loc) · 1.81 KB
/
compose.ts
File metadata and controls
71 lines (62 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import yaml from "js-yaml";
import { Compose, ComposeService, Service } from "./types.ts";
import { CADDYFILE_PATH, DOCKERFILE_NAME } from "./constants.ts";
function serviceToCompose(service: Service): ComposeService {
return {
build: {
context: service.path,
dockerfile: DOCKERFILE_NAME,
},
container_name: service.name,
restart: "unless-stopped",
environment: {
PORT: service.port.toString(),
},
networks: ["dockyard-net"],
};
}
function addServicesToCompose(
services: Service[],
serviceDefs: Record<string, ComposeService>,
): void {
for (const s of services) {
serviceDefs[s.name] = serviceToCompose(s);
}
}
function addDefaultCaddy(
serviceDefs: Record<string, ComposeService>,
volumeDefs: Record<string, unknown>,
): void {
serviceDefs["caddy"] = {
image: "caddy:latest",
container_name: "caddy",
restart: "unless-stopped",
ports: ["80:80", "443:443"],
volumes: [
`${CADDYFILE_PATH}:/etc/caddy/Caddyfile`,
"caddy_data:/data",
"caddy_config:/config",
],
networks: ["dockyard-net"],
};
volumeDefs["caddy_data"] = {};
volumeDefs["caddy_config"] = {};
}
export function buildCompose(services: Service[]): Compose {
const serviceDefs: Record<string, ComposeService> = {};
const volumeDefs: Record<string, unknown> = {};
addServicesToCompose(services, serviceDefs);
addDefaultCaddy(serviceDefs, volumeDefs);
return {
services: serviceDefs,
volumes: volumeDefs,
networks: { "dockyard-net": { driver: "bridge" } },
};
}
export function printYaml(obj: unknown): void {
console.log(yaml.dump(obj, { noRefs: true }));
}
export function writeYamlToFile(filename: string, obj: unknown): void {
const yamlContent = yaml.dump(obj, { noRefs: true });
Deno.writeTextFileSync(filename, yamlContent);
}