Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix esbuild-related issues #371

Merged
merged 17 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ packages/backend/dev/signal-cli
# Prisma
packages/backend/prisma/client

# local test environment
packages/backend/test/*.sql

# Rust
packages/wasm/*
!packages/wasm/package.json
Expand Down
25 changes: 0 additions & 25 deletions .pnp-ts.loader.mjs

This file was deleted.

21 changes: 17 additions & 4 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ node('rating-tracker-build') {
JOB_ID = sh (script: "#!/bin/bash\nprintf \"%04d\" \$((1 + RANDOM % 8192))", returnStdout: true)
PGPORT = sh (script: "#!/bin/bash\necho -n \$((49151 + 10#$JOB_ID))", returnStdout: true)
REDISPORT = sh (script: "#!/bin/bash\necho -n \$((57343 + 10#$JOB_ID))", returnStdout: true)

// Change config files for use in CI
sh """
echo \"globalFolder: /workdir/global\" >> .yarnrc.yml
echo \"preferAggregateCacheInfo: true\" >> .yarnrc.yml
echo \"enableGlobalCache: true\" >> .yarnrc.yml
sed -i \"s/127.0.0.1/172.17.0.1/ ; s/54321/$PGPORT/ ; s/63791/$REDISPORT/\" packages/backend/test/.env
"""
}
},
docker_env: {
Expand All @@ -42,7 +34,16 @@ node('rating-tracker-build') {
parallel(
testenv: {
stage('Start test environment') {
sh("PGPORT=$PGPORT REDISPORT=$REDISPORT docker compose -p rating-tracker-test-job$JOB_ID -f packages/backend/test/docker-compose.yml up --force-recreate -V -d")
// Create migration script from all migrations and inject IP and ports into test environment
// Since the Jenkins agent is running in a Docker container itself, we cannot mount the script directly
sh """
sed -i \"s/127.0.0.1/172.17.0.1/ ; s/54321/$PGPORT/ ; s/63791/$REDISPORT/\" packages/backend/test/.env
eval \$(cat packages/backend/test/.env | grep DATABASE_URL)
PG_MIGRATIONS=\$(cat packages/backend/prisma/migrations/*/migration.sql | grep -v \"^--\" | tr -d '\\\n')
cat packages/backend/test/docker-compose.yml | grep -v all_migrations | grep -v volumes > packages/backend/test/docker-compose-dind.yml
PGPORT=$PGPORT REDISPORT=$REDISPORT docker compose -p rating-tracker-test-job$JOB_ID -f packages/backend/test/docker-compose-dind.yml up --force-recreate -V -d
while ! docker compose -p rating-tracker-test-job$JOB_ID -f packages/backend/test/docker-compose-dind.yml exec postgres-test psql \$DATABASE_URL -c \"\$PG_MIGRATIONS\"; do sleep 1 ; done
"""
}
},
wasm: {
Expand All @@ -56,8 +57,12 @@ node('rating-tracker-build') {
},
dep: {
stage ('Install dependencies') {
// Copy global cache to workspace
sh("mkdir -p /home/jenkins/.cache/yarn/global && cp -arn /home/jenkins/.cache/yarn/global .")
// Change config files for use in CI and copy global cache to workspace
sh """
echo \"globalFolder: /workdir/global\npreferAggregateCacheInfo: true\nenableGlobalCache: true\" >> .yarnrc.yml
mkdir -p /home/jenkins/.cache/yarn/global
cp -arn /home/jenkins/.cache/yarn/global .
"""

// Install dependencies
docker.build("$imagename:job$JOB_ID-yarn", "-f docker/Dockerfile-yarn .")
Expand Down Expand Up @@ -149,7 +154,7 @@ node('rating-tracker-build') {
// Remove credentials and build artifacts
sh """
docker logout
docker compose -p rating-tracker-test-job$JOB_ID -f packages/backend/test/docker-compose.yml down -t 0
docker compose -p rating-tracker-test-job$JOB_ID -f packages/backend/test/docker-compose-dind.yml down -t 0
docker rmi $imagename:job$JOB_ID $imagename:job$JOB_ID-build $imagename:job$JOB_ID-test $imagename:job$JOB_ID-yarn || true
docker builder prune -f --keep-storage 2G
docker builder prune --builder rating-tracker -f --keep-storage 2G
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ An environment with services for development purposes can quickly be created usi

- Run `yarn build:wasm` to build the WebAssembly utilities and create the `wasm` package. This requires [Rust](https://www.rust-lang.org) and [wasm-pack](https://rustwasm.github.io/wasm-pack/) to be installed.
- Run `yarn dev:tools` to start NGINX, PostgreSQL, Redis, Selenium and the Signal REST API. SSL Certificates and the Redis ACL file must be provided beforehand, and a Signal account must be created before starting the server (see [section Setup steps](#setup-steps) for details). The NGINX configuration might require adjustment to your situation.
- Run `yarn prisma:migrate:dev` to initialize the PostgreSQL database and generate the Prisma client.
- Run `yarn workspace @rating-tracker/backend prisma:migrate:generate` to generate the Prisma client.
- Run `yarn workspace @rating-tracker/backend prisma:migrate:deploy` to initialize the PostgreSQL database.
- Run `yarn dev:server` to start the backend server as well as the Vite frontend development server.

NB: The `dev:tools` command sometimes fails because multiple Docker networks with the same name are created concurrently. In that case, manually delete all but one using `docker network ls` and `docker network rm …` and try again.
Expand Down Expand Up @@ -368,9 +369,8 @@ LOG_LEVEL=trace
A test environment with separate PostgreSQL and Redis instances can be created using the Docker Compose file in the [`test`](/packages/backend/test) folder. The `scripts` section in the [`package.json`](/package.json) provides helpful commands:

- Run `yarn build:wasm` to build the WebAssembly utilities and create the `wasm` package. This requires [Rust](https://www.rust-lang.org) and [wasm-pack](https://rustwasm.github.io/wasm-pack/) to be installed.
- Manually create a `main` entry in the `package.json` of the `wasm` package pointing to the same file as the `module` entry.
- Run `yarn test:tools` to start PostgreSQL and Redis.
- Run `yarn test:prisma:migrate:init` to initialize the PostgreSQL database.
- Run `yarn workspace @rating-tracker/backend prisma:migrate:generate` to generate the Prisma client.
- Run `yarn test` to run all tests from all packages. Additionally, the packages’ `package.json` configurations contain a `test:watch` script to run tests in watch mode.

NB: The `test:tools` command sometimes fails because multiple Docker networks with the same name are created concurrently. In that case, manually delete all but one using `docker network ls` and `docker network rm …` and try again.
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ USER node
COPY --from=assemble --chown=node:node /workdir/app .

# Define health check
HEALTHCHECK --interval=5m --start-period=15s CMD wget -qO /dev/null http://localhost:$PORT/api/status || exit 1
HEALTHCHECK CMD wget -qO /dev/null http://localhost:$PORT/api/status || exit 1

CMD [ "dumb-init", "node", "server.mjs" ]
15 changes: 8 additions & 7 deletions docker/Dockerfile-build
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ ENV FORCE_COLOR true

WORKDIR /workdir

# Copy project files and WebAssembly package
COPY . .
# Copy project files as well as Swagger UI files and WebAssembly package
COPY --from=marvinruder/rating-tracker:wasm /workdir/pkg packages/wasm
COPY .yarn/unplugged/swagger-ui-dist-*/node_modules/swagger-ui-dist/swagger-ui.css .yarn/unplugged/swagger-ui-dist-*/node_modules/swagger-ui-dist/swagger-ui-bundle.js .yarn/unplugged/swagger-ui-dist-*/node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js /workdir/app/public/api-docs/
COPY . .

# Build Node.js bundle
RUN yarn build

# Create directories for target container and copy only necessary files
RUN mkdir -p app/public app/prisma/client && \
RUN \
# Build Node.js bundle
yarn build && \
# Create directories for target container and copy only necessary files
mkdir -p app/public app/prisma/client && \
cp packages/backend/dist/server.mjs app && \
cp -r packages/backend/prisma/client/schema.prisma packages/backend/prisma/client/libquery_engine-* app/prisma/client && \
cp -r packages/frontend/dist/* app/public
Expand Down
7 changes: 2 additions & 5 deletions docker/Dockerfile-yarn
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ COPY packages/wasm/package.json ./packages/wasm/
# Install dependencies
RUN yarn --immutable

# Copy files required for Prisma
# Generate Prisma client
COPY packages/backend/prisma ./packages/backend/prisma
COPY packages/backend/test/.env ./packages/backend/test/

# Initialize Prisma for tests and generate client
RUN yarn workspace @rating-tracker/backend test:prisma:migrate:init
RUN yarn workspace @rating-tracker/backend prisma:generate

ENTRYPOINT [ "/bin/sh" ]
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
"scripts": {
"dev:server": "conc --kill-others \"yarn workspace @rating-tracker/backend dev:run\" \"yarn workspace @rating-tracker/backend dev:watch\" \"yarn workspace @rating-tracker/frontend dev:vite\" \"yarn workspace @rating-tracker/commons dev:build\" -n \",,,ﯤ\" -p \"{name}\" -c #339933,#3178C6,#61DAFB,#3178C6 --timings",
"dev:tools": "conc --kill-others \"yarn workspace @rating-tracker/backend dev:tools\" -n \"\" -p \"{name}\" -c grey --timings",
"dev:wasm": "wasm-pack build -s rating-tracker -d ../packages/wasm --debug wasm",
"prisma:migrate:dev": "yarn workspace @rating-tracker/backend prisma:migrate:dev",
"prisma:studio": "yarn workspace @rating-tracker/backend prisma:studio",
"test": "yarn workspaces foreach -pt run test",
"test:tools": "conc --kill-others \"yarn workspace @rating-tracker/backend test:tools\" -n \"\" -p \"{name}\" -c grey --timings",
"test:prisma:migrate:init": "yarn workspace @rating-tracker/backend test:prisma:migrate:init",
"test:tools": "cat packages/backend/prisma/migrations/*/migration.sql > packages/backend/test/all_migrations.sql && conc --kill-others \"yarn workspace @rating-tracker/backend test:tools\" -n \"\" -p \"{name}\" -c grey --timings",
"build": "yarn workspaces foreach -pt run build",
"build:wasm": "wasm-pack build -s rating-tracker -d ../packages/wasm --release wasm && sed -E -i.bak 's/\"module\": \"([A-Za-z0-9\\-\\.]+)\",/\"main\": \"\\1\",\\\n \"module\": \"\\1\",/g ; s/^}$/}\\\n/' packages/wasm/package.json && rm packages/wasm/package.json.bak",
"lint": "yarn workspaces foreach -pt run lint"
"lint": "yarn workspaces foreach -pt run lint",
"fix:swagger": "mkdir -p packages/backend/dist/public/api-docs && cp .yarn/unplugged/swagger-ui-dist-*/node_modules/swagger-ui-dist/swagger-ui{.css,-bundle.js,-standalone-preset.js} packages/backend/dist/public/api-docs/"
},
"packageManager": "yarn@3.6.1",
"devDependencies": {
Expand All @@ -32,5 +31,10 @@
},
"resolutions": {
"vite/esbuild": "0.19.1"
},
"dependenciesMeta": {
"swagger-ui-dist@5.3.1": {
"unplugged": true
}
}
}
7 changes: 4 additions & 3 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
"dev:selenium": "docker compose -f dev/docker-compose.yml up selenium",
"dev:signal": "docker compose -f dev/docker-compose.yml up signal",
"dev:tools": "conc --kill-others \"yarn dev:nginx\" \"yarn dev:postgres\" \"yarn dev:redis\" \"yarn dev:selenium\" \"yarn dev:signal\" -n \",,,ﰍ,\" -p \"{name}\" -c #43972A,#336791,#D82C20,#43B02A,#4975E8 --timings",
"test:watch": "yarn test:prisma:migrate:init && vitest",
"test:watch": "vitest",
"test": "vitest run --color",
"test:postgres": "PGPORT=54321 docker compose -f test/docker-compose.yml up --force-recreate -V postgres-test",
"test:redis": "REDISPORT=63791 docker compose -f test/docker-compose.yml up --force-recreate -V redis-test",
"test:tools": "conc --kill-others \"yarn test:postgres\" \"yarn test:redis\" -n \",\" -p \"{name}\" -c #336791,#D82C20 --timings",
"test:prisma:migrate:init": "env $(cat test/.env) pnpify prisma migrate dev --name init",
"prisma:studio": "pnpify prisma studio",
"prisma:generate": "pnpify prisma generate",
"prisma:migrate:dev": "pnpify prisma migrate dev",
"build": "esbuild --color=true src/server.ts --bundle --minify --platform=node --format=esm --legal-comments=none --banner:js=\"import r from'path';import{createRequire as e}from'module';import{fileURLToPath as m}from'url';const require=e(import.meta.url),__filename=m(import.meta.url),__dirname=r.dirname(__filename);\" --outfile=dist/server.mjs",
"build": "esbuild --color=true src/server.ts --bundle --minify --keep-names --platform=node --format=esm --legal-comments=none --banner:js=\"import __path from'path';import{createRequire}from'module';import{fileURLToPath}from'url';const require=createRequire(import.meta.url),__filename=fileURLToPath(import.meta.url),__dirname=__path.dirname(__filename);\" --outfile=dist/server.mjs",
"lint": "eslint --cache --ext .ts src/",
"lint:fix": "eslint --cache --ext .ts src/ --fix"
},
Expand All @@ -50,6 +49,7 @@
"redis-om": "0.4.2",
"response-time": "2.3.2",
"selenium-webdriver": "4.11.1",
"swagger-ui-dist": "5.3.1",
"swagger-ui-express": "5.0.0"
},
"devDependencies": {
Expand All @@ -58,6 +58,7 @@
"@types/node": "18.17.5",
"@types/selenium-webdriver": "4.1.15",
"@types/supertest": "2.0.12",
"@types/swagger-ui-express": "4.1.3",
"@typescript-eslint/eslint-plugin": "6.3.0",
"@typescript-eslint/parser": "6.3.0",
"@vitest/coverage-v8": "0.34.1",
Expand Down
14 changes: 13 additions & 1 deletion packages/backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,19 @@ server.app.use((req, res, next) => {
});

// Host the OpenAPI UI
server.app.use("/api-docs", SwaggerUI.serve, SwaggerUI.setup(openapiDocument));
server.app.use(
"/api-docs",
SwaggerUI.serve,
SwaggerUI.setup(
openapiDocument,
undefined,
undefined,
undefined,
"/assets/images/favicon-dev/favicon-192.png",
undefined,
"Rating Tracker API",
),
);

// Host the OpenAPI JSON configuration
server.app.get("/api-spec/v3", (_, res) => res.json(openapiDocument));
Expand Down
Loading