Skip to content

Commit

Permalink
feat: add package manager support to dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Nov 18, 2024
1 parent cebb570 commit d943aaf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/templates/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
type Preferences,
type PreferencesType,
pmExecuteMap,
pmInstallFrozenLockfile,
pmInstallFrozenLockfileProduction,
pmLockFilesMap,
} from "utils.js";

Expand Down Expand Up @@ -47,7 +49,7 @@ COPY --from=prerelease /usr/src/app/package.json .
# run the app
USER bun
ENTRYPOINT [ "bun", "run", "src/index.ts" ]`;

console.log(packageManager);
// TODO: support to package managers
return dedent /* Dockerfile */`
# Use the official Node.js 22 image.
Expand All @@ -57,17 +59,18 @@ FROM node:22 AS base
# Create app directory
WORKDIR /usr/src/app
${packageManager !== "npm" ? "npm install ${packageManager} -g" : ""}
# Install dependencies into temp directory
# This will cache them and speed up future builds
FROM base AS install
RUN mkdir -p /temp/dev
COPY package.json ${pmLockFilesMap[packageManager]} /temp/dev/
RUN cd /temp/dev && npm ci
RUN cd /temp/dev && ${pmInstallFrozenLockfile[packageManager]}
# Install with --production (exclude devDependencies)
RUN mkdir -p /temp/prod
COPY package.json ${pmLockFilesMap[packageManager]} /temp/prod/
RUN cd /temp/prod && npm ci --production
RUN cd /temp/prod && ${pmInstallFrozenLockfileProduction[packageManager]}
# Copy node_modules from temp directory
# Then copy all (non-ignored) project files into the image
Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,17 @@ export const pmLockFilesMap: Record<PackageManager, string> = {
yarn: "yarn.lock",
pnpm: "pnpm-lock.yaml",
};

export const pmInstallFrozenLockfile: Record<PackageManager, string> = {
npm: "npm ci",
bun: "bun install --frozen-lockfile",
yarn: "yarn install --frozen-lockfile",
pnpm: "pnpm install --frozen-lockfile",
};

export const pmInstallFrozenLockfileProduction: Record<PackageManager, string> = {
npm: "npm ci --production",
bun: "bun install --frozen-lockfile --production",
yarn: "yarn install --frozen-lockfile --production",
pnpm: "pnpm install --frozen-lockfile --prod",
};

0 comments on commit d943aaf

Please sign in to comment.