File size: 1,795 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
64
65
66
67
68
69
70
71
72
73
74
import _ from "lodash";
import type {
	ComposeSpecification,
	DefinitionsConfig,
	DefinitionsService,
} from "../types";

export const addSuffixToConfigsRoot = (
	configs: { [key: string]: DefinitionsConfig },
	suffix: string,
): { [key: string]: DefinitionsConfig } => {
	const newConfigs: { [key: string]: DefinitionsConfig } = {};

	_.forEach(configs, (config, configName) => {
		const newConfigName = `${configName}-${suffix}`;
		newConfigs[newConfigName] = _.cloneDeep(config);
	});

	return newConfigs;
};

export const addSuffixToConfigsInServices = (
	services: { [key: string]: DefinitionsService },
	suffix: string,
): { [key: string]: DefinitionsService } => {
	const newServices: { [key: string]: DefinitionsService } = {};

	_.forEach(services, (serviceConfig, serviceName) => {
		const newServiceConfig = _.cloneDeep(serviceConfig);

		// Reemplazar nombres de configs en configs
		if (_.has(newServiceConfig, "configs")) {
			newServiceConfig.configs = _.map(newServiceConfig.configs, (config) => {
				if (_.isString(config)) {
					return `${config}-${suffix}`;
				}
				if (_.isObject(config) && config.source) {
					return {
						...config,
						source: `${config.source}-${suffix}`,
					};
				}
				return config;
			});
		}

		newServices[serviceName] = newServiceConfig;
	});

	return newServices;
};

export const addSuffixToAllConfigs = (
	composeData: ComposeSpecification,
	suffix: string,
): ComposeSpecification => {
	const updatedComposeData = { ...composeData };
	if (composeData?.configs) {
		updatedComposeData.configs = addSuffixToConfigsRoot(
			composeData.configs,
			suffix,
		);
	}

	if (composeData?.services) {
		updatedComposeData.services = addSuffixToConfigsInServices(
			composeData.services,
			suffix,
		);
	}

	return updatedComposeData;
};