Skip to content

Commit

Permalink
fix(node): Ensure docker file is generated when nest framework is sup…
Browse files Browse the repository at this point in the history
…plied (#27153)

When we generate a nest application and supply the `--docker` flag it
should generate a DockerFile.

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

Currently, no DockerFile is created.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

A DockerFile to be created.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #17343
  • Loading branch information
ndcunningham authored Jul 26, 2024
1 parent 2d2c0b5 commit bff23d1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
29 changes: 29 additions & 0 deletions e2e/node/src/__snapshots__/node.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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" ]
"
`;
17 changes: 15 additions & 2 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'],
});
});

Expand Down Expand Up @@ -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 () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/node/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down

0 comments on commit bff23d1

Please sign in to comment.