Skip to content

Commit

Permalink
Consider indentation in pnpm Dockerfile autofix (#1715)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Oct 19, 2024
1 parent 91af3b6 commit 6b865a8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ describe('patchPnpmDockerImages', () => {

it('should return apply and not modify files if mode is lint', async () => {
jest.mocked(fg).mockResolvedValueOnce(['Dockerfile']);
jest
.mocked(readFile)
.mockResolvedValueOnce(
`RUN pnpm config set store-dir /root/.pnpm-store` as never,
);
jest.mocked(readFile).mockResolvedValueOnce(
`RUN --mount=type=bind,source=package.json,target=package.json \\
corepack enable pnpm && corepack install
RUN pnpm config set store-dir /root/.pnpm-store` as never,
);

await expect(
tryPatchPnpmDockerImages({
Expand Down Expand Up @@ -109,6 +110,49 @@ RUN --mount=type=bind,source=.npmrc,target=.npmrc \\
);
});

it('should patch Dockerfiles with different indents', async () => {
jest.mocked(fg).mockResolvedValueOnce(['Dockerfile']);
jest.mocked(readFile).mockResolvedValueOnce(
`RUN --mount=type=bind,source=package.json,target=package.json \\
corepack enable pnpm && corepack install
RUN pnpm config set store-dir /root/.pnpm-store
RUN --mount=type=bind,source=.npmrc,target=.npmrc \\
--mount=type=bind,source=patches,target=patches \\
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \\
--mount=type=secret,id=npm,dst=/root/.npmrc,required=true \\
pnpm fetch
` as never,
);

await expect(
tryPatchPnpmDockerImages({
mode: 'format',
} as PatchConfig),
).resolves.toEqual({
result: 'apply',
});

expect(writeFile).toHaveBeenNthCalledWith(
1,
'Dockerfile',
`RUN --mount=type=bind,source=package.json,target=package.json \\
corepack enable pnpm && corepack install
RUN --mount=type=bind,source=package.json,target=package.json \\
pnpm config set store-dir /root/.pnpm-store
RUN --mount=type=bind,source=.npmrc,target=.npmrc \\
--mount=type=bind,source=package.json,target=package.json \\
--mount=type=bind,source=patches,target=patches \\
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \\
--mount=type=secret,id=npm,dst=/root/.npmrc,required=true \\
pnpm fetch
`,
);
});

it('should patch Dockerfiles with extra mounts', async () => {
jest.mocked(fg).mockResolvedValueOnce(['Dockerfile']);
jest.mocked(readFile).mockResolvedValueOnce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type { PatchFunction, PatchReturnType } from '../..';
import { log } from '../../../../../../utils/logging';

const DOCKER_IMAGE_CONFIG_REGEX =
/^(RUN )(pnpm config set store-dir \/root\/.pnpm-store)/gm;
/^(RUN --mount=type=bind,source=package.json,target=package.json \\\n(\s+)corepack enable pnpm && corepack install(?:.|\n)+?RUN )(pnpm config set store-dir \/root\/.pnpm-store)/gm;
const DOCKER_IMAGE_FETCH_REGEX =
/^(RUN --mount=type=bind,source=.npmrc,target=.npmrc \\\n)((?:(?!--mount=type=bind,source=package\.json,target=package\.json)[\s\S])*pnpm (fetch|install))/gm;
/^(RUN --mount=type=bind,source=.npmrc,target=.npmrc \\\n)((?:(?!--mount=type=bind,source=package\.json,target=package\.json)[\s\S])+?\n(\s+)pnpm (fetch|install))/gm;

const PACKAGE_JSON_MOUNT =
'--mount=type=bind,source=package.json,target=package.json \\\n';
Expand Down Expand Up @@ -62,8 +62,16 @@ const patchPnpmDockerImages: PatchFunction = async ({
await Promise.all(
dockerFilesToPatch.map(async ({ file, contents }) => {
const patchedContents = contents
.replace(DOCKER_IMAGE_CONFIG_REGEX, `$1${PACKAGE_JSON_MOUNT} $2`)
.replace(DOCKER_IMAGE_FETCH_REGEX, `$1 ${PACKAGE_JSON_MOUNT}$2`);
.replace(
DOCKER_IMAGE_CONFIG_REGEX,
(_, earlyCommands, whitespace, pnpmSetConfigLine) =>
`${earlyCommands}${PACKAGE_JSON_MOUNT}${whitespace}${pnpmSetConfigLine}`,
)
.replace(
DOCKER_IMAGE_FETCH_REGEX,
(_, npmrcLine, rest, whitespace) =>
`${npmrcLine}${whitespace}${PACKAGE_JSON_MOUNT}${rest}`,
);

await writeFile(file, patchedContents);
}),
Expand Down

0 comments on commit 6b865a8

Please sign in to comment.