feat(infra): reworked Dockerfile

- changed from npm to pnpm in Dockerfile => improved install time
    Fixed errors from change in .ts files.
This commit is contained in:
Maieul BOYER 2025-08-03 14:40:48 +02:00
parent 704ce37909
commit 5f9fd5629c
7 changed files with 51 additions and 35 deletions

View file

@ -6,7 +6,7 @@
// By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ //
// +#+#+#+#+#+ +#+ //
// Created: 2025/07/28 17:36:22 by maiboyer #+# #+# //
// Updated: 2025/07/30 21:19:05 by maiboyer ### ########.fr //
// Updated: 2025/08/03 13:36:25 by maiboyer ### ########.fr //
// //
// ************************************************************************** //
@ -66,20 +66,6 @@ export class Database {
this.st.set(query, st);
return st;
}
public createUser(user: string): Result<UUIDv7, DBUserExists> {
const st = this.prepare('INSERT INTO users VALUES (?, ?) RETURNING id');
try {
st.get(newUUIDv7(), user)
}
catch (e: any) {
console.log(e)
console.log(typeof e)
}
return Result.ok(newUUIDv7());
}
}
// When using .decorate you have to specify added properties for Typescript
@ -96,8 +82,7 @@ export type DatabaseOption = {
export const uDatabase = fp<DatabaseOption>(async function(
_fastify: FastifyInstance,
_options: DatabaseOption) {
console.log("Database has been hooked up to fastify ?!");
});
export default uDatabase;

View file

@ -1,29 +1,34 @@
FROM node:24-alpine as builder
FROM guergeiro/pnpm:22-10-alpine as builder
RUN apk add jq;
ARG SERVICE
RUN apk add jq python3 musl-dev gcc g++ make;
WORKDIR /build
COPY ./@shared/package.json /build/@shared/package.json
COPY ./${SERVICE}/package.json /build/service/package.json
COPY ./${SERVICE}/package.json /build/package.json
COPY ./tsconfig.base.json /build/tsconfig.base.json
COPY ./pnpm-workspace.yaml /build/pnpm-workspace.yaml
RUN echo "{\"name\":\"workspace\", \"workspaces\": [\"@shared\", \"${SERVICE}\" ]}" | jq . >/build/package.json;
RUN npm install && npm install --prefix=/build/service;
RUN pnpm install;
RUN ls -a *;
COPY ./@shared/ /build/@shared/
COPY ./${SERVICE}/ /build/service/
RUN (cd /build/service && npm run build:prod) \
&& jq -s '.[0] * .[1]' /build/@shared/package.json /build/service/package.json >/dist/package.json;
RUN cd /build/service && \
pnpm run build:prod && \
jq -s '.[0] * .[1]' /build/*/package.json >/dist/package.json && \
cp /build/pnpm-workspace.yaml /dist/pnpm-workspace.yaml && \
true;
FROM node:24
FROM guergeiro/pnpm:22-10-slim
WORKDIR /src
COPY --from=builder /dist /src
RUN npm install;
RUN pnpm install --prod;
CMD ["node", "/src/run.cjs"]

View file

@ -3,8 +3,11 @@ import fastifyFormBody from '@fastify/formbody'
import fastifyMultipart from '@fastify/multipart'
import { mkdir } from 'node:fs/promises'
import fp from 'fastify-plugin'
import * as db from '@shared/database'
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
@ -21,13 +24,13 @@ const app: FastifyPluginAsync = async (
): Promise<void> => {
// Place here your custom code!
for (const plugin of Object.values(plugins)) {
void fastify.register(plugin, {});
void fastify.register(plugin as any, {});
}
for (const route of Object.values(routes)) {
void fastify.register(route, {});
void fastify.register(route as any, {});
}
//void fastify.register(MyPlugin, {})
void fastify.register(db.uDatabase as any, {})
void fastify.register(fastifyFormBody, {})
void fastify.register(fastifyMultipart, {})

View file

@ -2,7 +2,6 @@ import { FastifyPluginAsync } from 'fastify'
import { join } from 'node:path'
import { open } from 'node:fs/promises'
import sharp from 'sharp'
import { newUUIDv7 } from '@shared/uuid'
import rawBody from 'raw-body'
const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
@ -12,7 +11,7 @@ const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
// we register a route handler for: `/<USERID_HERE>`
// it sets some configuration options, and set the actual function that will handle the request
fastify.addContentTypeParser('*', function(request, payload, done) {
fastify.addContentTypeParser('*', function(request, payload, done: any) {
done()
});

View file

@ -2,12 +2,32 @@ import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'
import nodeExternals from 'rollup-plugin-node-externals'
import path from 'node:path'
import fs from 'node:fs'
function collectDeps(...pkgJsonPaths) {
const allDeps = new Set();
for (const pkgPath of pkgJsonPaths) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
for (const dep of Object.keys(pkg.dependencies || {})) {
allDeps.add(dep);
}
for (const peer of Object.keys(pkg.peerDependencies || {})) {
allDeps.add(peer);
}
}
return Array.from(allDeps);
}
const externals = collectDeps(
'./package.json',
'../@shared/package.json'
);
export default defineConfig({
root: __dirname, // service root
plugins: [tsconfigPaths(), nodeExternals()],
build: {
ssr: true,
outDir: 'dist',
emptyOutDir: true,
@ -17,9 +37,9 @@ export default defineConfig({
fileName: () => 'index.js',
},
rollupOptions: {
external: [],
external: externals,
},
target: 'node24', // or whatever Node version you use
target: 'node22', // or whatever Node version you use
sourcemap: true,
minify: false, // for easier debugging
}

4
src/pnpm-workspace.yaml Normal file
View file

@ -0,0 +1,4 @@
pacakges:
- ./*
nodeLinker: hoisted

View file

@ -2,7 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"target": "ES2023",
"module": "NodeNext",
"module": "esnext",
"moduleResolution": "node",
"newLine": "lf",