UNPKG

@nestjs-mod/docker-compose

Version:

Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience. (Generator docker-compose.yml for https://docs.docker.com/compose)

109 lines 7.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DockerComposeNginx = void 0; const common_1 = require("@nestjs-mod/common"); const case_anything_1 = require("case-anything"); const fs_1 = require("fs"); const path_1 = require("path"); const docker_compose_module_1 = require("../../docker-compose.module"); const docker_compose_utils_1 = require("../../docker-compose.utils"); const docker_compose_nginx_constants_1 = require("./docker-compose-nginx.constants"); const docker_compose_nginx_settings_1 = require("./docker-compose-nginx.settings"); exports.DockerComposeNginx = (0, common_1.createNestModule)({ moduleName: docker_compose_nginx_constants_1.DOCKER_COMPOSE_NGINX_MODULE_NAME, moduleDescription: 'Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. (Generator for nginx in docker-compose.yml for https://www.npmjs.com/package/@nestjs-mod/docker-compose)', globalEnvironmentsOptions: { skipValidation: true }, globalConfigurationOptions: { skipValidation: true }, staticConfigurationModel: docker_compose_nginx_settings_1.DockerComposeNginxConfiguration, preWrapApplication: async ({ project, modules, current }) => { if (!modules[common_1.NestModuleCategory.infrastructure]) { modules[common_1.NestModuleCategory.infrastructure] = []; } const dockerComposeNginxModule = (0, common_1.createNestModule)({ project, moduleName: docker_compose_nginx_constants_1.DOCKER_COMPOSE_NGINX_MODULE_NAME, moduleDescription: 'Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. (Generator for nginx in docker-compose.yml for https://www.npmjs.com/package/@nestjs-mod/docker-compose)', moduleCategory: common_1.NestModuleCategory.infrastructure, globalEnvironmentsOptions: { name: project?.name, skipValidation: (0, common_1.isInfrastructureMode)() }, globalConfigurationOptions: { name: project?.name, skipValidation: (0, common_1.isInfrastructureMode)() }, staticConfigurationModel: docker_compose_nginx_settings_1.DockerComposeNginxConfiguration, imports: ({ contextName, project, staticConfiguration }) => { const networks = (project?.name ? staticConfiguration?.networks?.map((n) => ({ ...n, name: (0, case_anything_1.kebabCase)([project?.name, n.name, 'network'].filter(Boolean).join('-')), })) ?? [{ name: (0, case_anything_1.kebabCase)(`${project?.name}-network`), driver: 'bridge' }] : staticConfiguration?.networks) ?? []; if (networks?.length === 0) { networks.push({ name: 'default-network', driver: 'bridge' }); } const networkNames = networks?.map((n) => n.name); const serviceName = (0, docker_compose_utils_1.getDockerComposeServiceName)(project?.name, docker_compose_utils_1.DockerComposeServiceType.Nginx); const kebabServiceName = (0, case_anything_1.kebabCase)(serviceName); let configContent = staticConfiguration.configContent || ''; const ports = Object.entries(staticConfiguration?.ports || {}).map(([, port]) => port); for (let i = 0; i <= ports.length; i++) { configContent = configContent.replace(new RegExp(`%port${i + 1}%`, 'ig'), String(ports[i])); } for (const serviceName of Object.keys(staticConfiguration.dependsOnServiceNames || {})) { configContent = configContent.replace(new RegExp(`%${serviceName}%`, 'ig'), (0, docker_compose_utils_1.getDockerComposeServiceName)(project?.name, serviceName)); } const keys = Object.keys(process.env); for (const key of keys) { configContent = configContent.replace(new RegExp(`%${key}%`, 'ig'), process.env[key] || ''); } if (staticConfiguration.configFolder) { if (!(0, fs_1.existsSync)(staticConfiguration.configFolder)) { (0, fs_1.mkdirSync)(staticConfiguration.configFolder, { recursive: true }); } (0, fs_1.writeFileSync)((0, path_1.join)(staticConfiguration.configFolder, `${kebabServiceName}-nginx.conf`), configContent); } return [ common_1.ProjectUtils.forFeature({ featureModuleName: docker_compose_nginx_constants_1.DOCKER_COMPOSE_NGINX_MODULE_NAME, contextName, }), docker_compose_module_1.DockerCompose.forFeature({ contextName, featureModuleName: (0, case_anything_1.constantCase)(serviceName), featureConfiguration: { services: { [serviceName]: { image: staticConfiguration?.image, container_name: serviceName, volumes: [ staticConfiguration.configFolder ? `${staticConfiguration.configFolder}:/etc/nginx/conf.d` : '', staticConfiguration.logsFolder ? `${staticConfiguration.logsFolder}:/var/log/nginx/` : '', ].filter(Boolean), ports: Object.entries(staticConfiguration.ports || {}).map(([key, value]) => `${key}:${value}`), networks: networkNames, tty: true, restart: 'always', depends_on: Object.entries(staticConfiguration.dependsOnServiceNames || {}) .map(([serviceName, condition]) => { const keys = Object.keys(process.env); if (serviceName) { for (const key of keys) { serviceName = String(serviceName).replace(new RegExp(`%${key}%`, 'ig'), process.env[key] || ''); } } // todo: fix type // eslint-disable-next-line @typescript-eslint/no-explicit-any return [serviceName, condition]; }) .map(([serviceName, condition]) => ({ [(0, docker_compose_utils_1.getDockerComposeServiceName)(project?.name, serviceName)]: { condition }, })) .reduce((all, cur) => ({ ...all, ...cur }), {}), }, }, networks: networks.reduce((all, cur) => ({ ...all, [cur.name]: { driver: cur.driver } }), {}), }, }), ]; }, }).DockerComposeNginx; modules[common_1.NestModuleCategory.infrastructure].push(dockerComposeNginxModule.forRootAsync(current.asyncModuleOptions)); }, }).DockerComposeNginx; //# sourceMappingURL=docker-compose-nginx.module.js.map