From a6b1b7d7daf271168c62077a639ccdf013759a69 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Fri, 26 Jul 2024 10:31:37 -0600 Subject: [PATCH] fix(node): Ensure docker file is generated when nest framework is supplied (#27153) When we generate a nest application and supply the `--docker` flag it should generate a DockerFile. ## Current Behavior Currently, no DockerFile is created. ## Expected Behavior A DockerFile to be created. ## Related Issue(s) Fixes #17343 (cherry picked from commit bff23d1b75a5e66d4acd8e0cd3e5ba55cf03c151) --- e2e/node/src/__snapshots__/node.test.ts.snap | 29 +++++++++++++++++++ e2e/node/src/node.test.ts | 17 +++++++++-- .../src/generators/application/application.ts | 12 +++++++- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 e2e/node/src/__snapshots__/node.test.ts.snap diff --git a/e2e/node/src/__snapshots__/node.test.ts.snap b/e2e/node/src/__snapshots__/node.test.ts.snap new file mode 100644 index 0000000000000..89ec9e052bacc --- /dev/null +++ b/e2e/node/src/__snapshots__/node.test.ts.snap @@ -0,0 +1,29 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Node Applications should generate a nest application with docker 1`] = ` +"# This file is generated by Nx. +# +# Build the docker image with \`npx nx docker-build node-nest-docker-test\`. +# Tip: Modify "docker-build" options in project.json to change docker build args. +# +# Run the container with \`docker run -p 3000:3000 -t node-nest-docker-test\`. +FROM docker.io/node:lts-alpine + +ENV HOST=0.0.0.0 +ENV PORT=3000 + +WORKDIR /app + +RUN addgroup --system node-nest-docker-test && \\ + adduser --system -G node-nest-docker-test node-nest-docker-test + +COPY dist/node-nest-docker-test node-nest-docker-test/ +RUN chown -R node-nest-docker-test:node-nest-docker-test . + +# You can remove this install step if you build with \`--bundle\` option. +# The bundled output will include external dependencies. +RUN npm --prefix node-nest-docker-test --omit=dev -f install + +CMD [ "node", "node-nest-docker-test" ] +" +`; diff --git a/e2e/node/src/node.test.ts b/e2e/node/src/node.test.ts index 555741a6514a1..255effbbd65dc 100644 --- a/e2e/node/src/node.test.ts +++ b/e2e/node/src/node.test.ts @@ -23,7 +23,7 @@ import { updateFile, updateJson, } from '@nx/e2e/utils'; -import { exec, execSync } from 'child_process'; +import { execSync } from 'child_process'; import * as http from 'http'; import { getLockFileName } from '@nx/js'; import { satisfies } from 'semver'; @@ -58,7 +58,7 @@ describe('Node Applications', () => { beforeAll(() => { originalEnvPort = process.env.PORT; newProject({ - packages: ['@nx/node', '@nx/express', '@nx/nest'], + packages: ['@nx/node', '@nx/express', '@nx/nest', '@nx/webpack'], }); }); @@ -340,6 +340,19 @@ module.exports = { await promisifiedTreeKill(p.pid, 'SIGKILL'); }, 120000); + it('should generate a nest application with docker', async () => { + const nestapp = 'node-nest-docker-test'; + + runCLI( + `generate @nx/node:app ${nestapp} --project-name-and-root-format=as-provided --bundler=webpack --framework=nest --docker` + ); + + checkFilesExist(`${nestapp}/Dockerfile`); + + const dockerFile = readFile(`${nestapp}/Dockerfile`); + expect(dockerFile).toMatchSnapshot(); + }); + // TODO(crystal, @ndcunningham): how do we handle this now? // Revisit when NxWebpackPlugin({}) outputFilename is working. xit('should be able to run ESM applications', async () => { diff --git a/packages/node/src/generators/application/application.ts b/packages/node/src/generators/application/application.ts index 98b2694c33b35..9ad70f32eafe0 100644 --- a/packages/node/src/generators/application/application.ts +++ b/packages/node/src/generators/application/application.ts @@ -396,9 +396,19 @@ export async function applicationGeneratorInternal(tree: Tree, schema: Schema) { ...options, skipFormat: true, }); + tasks.push(nestTasks); + + if (options.docker) { + const dockerTask = await setupDockerGenerator(tree, { + ...options, + project: options.name, + skipFormat: true, + }); + tasks.push(dockerTask); + } return runTasksInSerial( ...[ - nestTasks, + ...tasks, () => { logShowProjectCommand(options.name); },