File size: 1,476 Bytes
b80fc11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import _ from "lodash";
import type { ComposeSpecification, DefinitionsService } from "../types";

export const addAppNameToRootNetwork = (
	composeData: ComposeSpecification,
	appName: string,
): ComposeSpecification => {
	const updatedComposeData = { ...composeData };

	// Initialize networks if it doesn't exist
	if (!updatedComposeData.networks) {
		updatedComposeData.networks = {};
	}

	// Add the new network with the app name
	updatedComposeData.networks[appName] = {
		name: appName,
		external: true,
	};

	return updatedComposeData;
};

export const addAppNameToServiceNetworks = (
	services: { [key: string]: DefinitionsService },
	appName: string,
): { [key: string]: DefinitionsService } => {
	return _.mapValues(services, (service) => {
		if (!service.networks) {
			service.networks = [appName];
			return service;
		}

		if (Array.isArray(service.networks)) {
			if (!service.networks.includes(appName)) {
				service.networks.push(appName);
			}
		} else {
			service.networks[appName] = {};
		}

		return service;
	});
};

export const addAppNameToAllServiceNames = (
	composeData: ComposeSpecification,
	appName: string,
): ComposeSpecification => {
	let updatedComposeData = { ...composeData };

	updatedComposeData = addAppNameToRootNetwork(updatedComposeData, appName);

	if (updatedComposeData.services) {
		updatedComposeData.services = addAppNameToServiceNetworks(
			updatedComposeData.services,
			appName,
		);
	}

	return updatedComposeData;
};