flecks/packages/docker/build/generate.js
2024-02-12 06:31:43 -06:00

75 lines
2.2 KiB
JavaScript

exports.generateDockerFile = async (flecks) => {
const dockerfile = [
'FROM node:20',
'',
'RUN mkdir -p /var/www',
'WORKDIR /var/www',
'COPY package.json /var/www',
'COPY build /var/www/build',
'COPY dist /var/www/dist',
'',
'ENV DEBUG=*',
'ENV NODE_ENV=production',
'',
'CMD ["node", "dist/server"]',
'',
'VOLUME /var/www/node_modules',
'',
].join('\n');
return flecks.invokeComposedAsync('@flecks/docker.Dockerfile', dockerfile);
};
exports.generateComposeConfig = async (flecks) => {
const id = flecks.get('@flecks/core.id');
const appServiceName = `${id}_app`;
const services = {
[appServiceName]: {
build: {
context: '../..',
dockerfile: 'dist/server/Dockerfile',
},
environment: {
FLECKS_ENV__flecks_docker__enabled: 'false',
},
volumes: [
'../../node_modules:/var/www/node_modules',
],
},
};
const containers = await flecks.invokeAsync('@flecks/docker.containers');
(
await Promise.all(
Object.entries(containers)
.map(async ([fleck, config]) => {
Object.entries(await config)
.forEach(([key, config]) => {
services[key] = {image: config.image, environment: {}, ...config.extra};
});
return [
`FLECKS_ENV__${flecks.constructor.environmentalize(fleck)}`,
config,
];
}),
)
)
// Set environment.
.forEach(([prefix, config]) => {
Object.values(config)
.forEach((config) => {
Object.entries(config.environment || {})
.forEach(([configService, environment]) => {
Object.entries(environment || {})
.forEach(([key, value]) => {
const [realKey, realService] = 'app' === configService
? [`${prefix}__${key}`, appServiceName]
: [key, configService];
services[realService].environment[realKey] = value;
});
});
});
});
const config = {version: '3', services};
await flecks.invokeSequentialAsync('@flecks/docker.docker-compose.yml', config);
return config;
};