From f97b281b6e0713fb30286761ff1e34e7e8885d7e Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 17 Feb 2023 12:11:27 -0500 Subject: [PATCH] feat(node): create fastify setup closer to what fastify-cli creates --- e2e/node/src/node-server.test.ts | 5 +--- .../src/generators/application/application.ts | 18 +++++++++++++- .../files/fastify/src/app/app.ts__tmpl__ | 24 +++++++++++++++---- .../src/app/plugins/sensible.ts__tmpl__ | 12 ++++++++++ .../fastify/src/app/routes/root.ts__tmpl__ | 7 ++++++ .../setup-docker/files/Dockerfile__tmpl__ | 4 ++++ packages/node/src/utils/versions.ts | 14 ++++++----- 7 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 packages/node/src/generators/application/files/fastify/src/app/plugins/sensible.ts__tmpl__ create mode 100644 packages/node/src/generators/application/files/fastify/src/app/routes/root.ts__tmpl__ diff --git a/e2e/node/src/node-server.test.ts b/e2e/node/src/node-server.test.ts index fd824d7d1b18a8..dc2c1ea886b641 100644 --- a/e2e/node/src/node-server.test.ts +++ b/e2e/node/src/node-server.test.ts @@ -1,7 +1,5 @@ import { checkFilesDoNotExist, - updateFile, - readFile, checkFilesExist, cleanupProject, killPort, @@ -13,7 +11,6 @@ import { uniq, updateFile, } from '@nrwl/e2e/utils'; -import { apps } from 'open'; describe('Node Applications + webpack', () => { beforeEach(() => newProject()); @@ -84,7 +81,7 @@ describe('Node Applications + webpack', () => { await runE2eTests(expressApp); await runE2eTests(fastifyApp); await runE2eTests(koaApp); - }, 480_000); + }, 300_000); it('should generate a Dockerfile', async () => { const expressApp = uniq('expressapp'); diff --git a/packages/node/src/generators/application/application.ts b/packages/node/src/generators/application/application.ts index 79422b4676e081..3e04f8f7b0b857 100644 --- a/packages/node/src/generators/application/application.ts +++ b/packages/node/src/generators/application/application.ts @@ -32,6 +32,8 @@ import { esbuildVersion, expressTypingsVersion, expressVersion, + fastifyAutoloadVersion, + fastifySensibleVersion, fastifyVersion, koaTypingsVersion, koaVersion, @@ -99,6 +101,7 @@ function getEsBuildConfig( ), // Use CJS for Node apps for widest compatibility. format: ['cjs'], + bundle: false, main: joinPathFragments( project.sourceRoot, 'main' + (options.js ? '.js' : '.ts') @@ -293,13 +296,26 @@ function addProjectDependencies( }, fastify: { fastify: fastifyVersion, + '@fastify/autoload': fastifyAutoloadVersion, + '@fastify/sensible': fastifySensibleVersion, }, }; + const frameworkDevDependencies = { + express: { + '@types/express': expressTypingsVersion, + }, + koa: { + '@types/koa': koaTypingsVersion, + }, + fastify: {}, + }; return addDependenciesToPackageJson( tree, - {}, { ...frameworkDependencies[options.framework], + }, + { + ...frameworkDevDependencies[options.framework], ...bundlers[options.bundler], } ); diff --git a/packages/node/src/generators/application/files/fastify/src/app/app.ts__tmpl__ b/packages/node/src/generators/application/files/fastify/src/app/app.ts__tmpl__ index 77116eef8c49aa..6c7d1a8640ee8b 100644 --- a/packages/node/src/generators/application/files/fastify/src/app/app.ts__tmpl__ +++ b/packages/node/src/generators/application/files/fastify/src/app/app.ts__tmpl__ @@ -1,11 +1,27 @@ +import * as path from 'path'; import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; +import AutoLoad from '@fastify/autoload'; /* eslint-disable-next-line */ -export interface AppOptions { -} +export interface AppOptions { } export async function app(fastify: FastifyInstance, opts: AppOptions) { - fastify.get('/', async function(request: FastifyRequest, reply: FastifyReply) { - return { message: 'Hello API' }; + // Place here your custom code! + + // Do not touch the following lines + + // This loads all plugins defined in plugins + // those should be support plugins that are reused + // through your application + fastify.register(AutoLoad, { + dir: path.join(__dirname, 'plugins'), + options: { ...opts }, + }); + + // This loads all plugins defined in routes + // define your routes in one of these + fastify.register(AutoLoad, { + dir: path.join(__dirname, 'routes'), + options: { ...opts }, }); } diff --git a/packages/node/src/generators/application/files/fastify/src/app/plugins/sensible.ts__tmpl__ b/packages/node/src/generators/application/files/fastify/src/app/plugins/sensible.ts__tmpl__ new file mode 100644 index 00000000000000..e39d76c5898074 --- /dev/null +++ b/packages/node/src/generators/application/files/fastify/src/app/plugins/sensible.ts__tmpl__ @@ -0,0 +1,12 @@ +import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; +import fp from 'fastify-plugin'; +import sensible from '@fastify/sensible'; + +/** + * This plugins adds some utilities to handle http errors + * + * @see https://github.com/fastify/fastify-sensible + */ +export default fp(async function(fastify: FastifyInstance, opts: {}) { + fastify.register(sensible); +}); diff --git a/packages/node/src/generators/application/files/fastify/src/app/routes/root.ts__tmpl__ b/packages/node/src/generators/application/files/fastify/src/app/routes/root.ts__tmpl__ new file mode 100644 index 00000000000000..f6410b4415b99b --- /dev/null +++ b/packages/node/src/generators/application/files/fastify/src/app/routes/root.ts__tmpl__ @@ -0,0 +1,7 @@ +import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; + +export default async function(fastify: FastifyInstance, opts: {}) { + fastify.get('/', async function(request: FastifyRequest, reply: FastifyReply) { + return { message: 'Hello API' }; + }); +} diff --git a/packages/node/src/generators/setup-docker/files/Dockerfile__tmpl__ b/packages/node/src/generators/setup-docker/files/Dockerfile__tmpl__ index f22b4e72140b7d..f6752d0547dc5d 100644 --- a/packages/node/src/generators/setup-docker/files/Dockerfile__tmpl__ +++ b/packages/node/src/generators/setup-docker/files/Dockerfile__tmpl__ @@ -17,4 +17,8 @@ RUN addgroup --system <%= project %> && \ COPY <%= buildLocation %> <%= project %> RUN chown -R <%= project %>:<%= project %> . +# You can remove this install step if you build with `--bundle` option. +# The bundled output will include external dependencies. +RUN npm --prefix <%= project %> --omit=dev -f install + CMD [ "node", "<%= project %>" ] diff --git a/packages/node/src/utils/versions.ts b/packages/node/src/utils/versions.ts index a3ffa233c01de6..4b9cbadcdd304d 100644 --- a/packages/node/src/utils/versions.ts +++ b/packages/node/src/utils/versions.ts @@ -2,16 +2,18 @@ export const nxVersion = require('../../package.json').version; export const tslibVersion = '^2.3.0'; -export const typesNodeVersion = '18.7.1'; +export const typesNodeVersion = '~18.7.1'; export const esbuildVersion = '^0.17.5'; -export const expressVersion = '^4.18.1'; -export const expressTypingsVersion = '4.17.13'; +export const expressVersion = '~4.18.1'; +export const expressTypingsVersion = '~4.17.13'; -export const koaVersion = '2.14.1'; -export const koaTypingsVersion = '2.13.5'; +export const koaVersion = '~2.14.1'; +export const koaTypingsVersion = '~2.13.5'; -export const fastifyVersion = '4.11.0'; +export const fastifyVersion = '~4.13.0'; +export const fastifyAutoloadVersion = '~5.7.1'; +export const fastifySensibleVersion = '~5.2.0'; export const axiosVersion = '^1.0.0';