"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateBackendDockerfile = generateBackendDockerfile;
function generateBackendDockerfile({ devOps: { database }, schema: { projectType, slug }, }) {
    const useDb = !!database;
    const isWorkspace = projectType === 'workspace';
    const directory = isWorkspace ? `projects/${slug}/` : '';
    return generateDockerfile({ useDb, slug, directory, isWorkspace });
}
const generateDockerfile = ({ useDb, slug, directory, isWorkspace, }) => {
    const migrateScriptPath = useDb ? `full/${directory}docker/backend/migrate.sh ` : '';
    const copyCommand = isWorkspace ? `COPY full/${directory}docker/backend/start.api.sh ${migrateScriptPath}./\n` : '';
    const chmodCommand = `RUN chmod +x ./start.api.sh${useDb ? ' ./migrate.sh' : ''}`;
    return /*Dockerfile*/ `FROM node:24-alpine AS alpine
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update && apk add --no-cache libc6-compat

# Setup pnpm on the alpine base
FROM alpine AS base
RUN npm i -g --ignore-scripts pnpm${isWorkspace ? '' : ' turbo@^2'}

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
WORKDIR /app

# First install the dependencies (as they change less often)
COPY json/ .
RUN pnpm i --frozen-lockfile --ignore-scripts

WORKDIR /app

# Set Turborepo environment variables
ENV TURBO_API=https://turborepo-cache.postxl.com
ENV TURBO_TEAM=postxl
ARG TURBO_TOKEN
ENV TURBO_TOKEN=\${TURBO_TOKEN}

# Build the project
COPY full/ .
RUN pnpm ${isWorkspace ? `turbo:ci build --filter=@postxl/${slug} --filter=@postxl/${slug}-api` : `turbo run build --filter=@postxl/${slug}-api`}

WORKDIR /app/${directory}backend
${useDb ? '# We separately add the migration script so that is can be called directly during the Migration stage.' : ''}
${copyCommand}${chmodCommand}

FROM base AS runner
WORKDIR /app

# Create non-root user and group
RUN addgroup --system --gid 1001 nestjs && adduser --system --uid 1001 nestjs

# Copy files as root (default)
COPY --from=installer /app/ .
# Set ownership for all files to nestjs
# Restrict permissions only for backend app code, keep dependencies writable
RUN chown -R nestjs:nestjs /app \\
	&& find /app/${directory}backend -type d -exec chmod 755 {} + \\
	&& find /app/${directory}backend -type f -exec chmod 444 {} +

# Switch to non-root user
USER nestjs

WORKDIR /app/${directory.slice(0, -1)}
CMD ["/bin/sh", "./backend/start.api.sh"]
`;
};
//# sourceMappingURL=backend-dockerfile.generator.js.map