Skip to content

Commit f860fa4

Browse files
authored
112 change env reader to dotenvx (#115)
* feat: use dotenvx instead of dotenv * build: update how env files are added to docker-compose flow & adapt docs * build(docker-compose): use nextjs convention to load .env files
1 parent f58c7fc commit f860fa4

9 files changed

Lines changed: 228 additions & 23 deletions

File tree

#.env

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
DISCORD_TOKEN=
1+
NODE_ENV=development
22
POSTGRES_DB=
33
POSTGRES_USER=
4-
POSTGRES_PASSWORD=
5-
DATABASE_PORT=5432
6-
DATABASE_HOST=localhost
7-
NODE_ENV=development
8-
SENDGRID_API_KEY=

#.env.development

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DISCORD_TOKEN=
2+
POSTGRES_PASSWORD=
3+
DATABASE_PORT=5432
4+
DATABASE_HOST=localhost
5+
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${POSTGRES_DB}?schema=public
6+
SENDGRID_API_KEY=

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env.keys

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ FROM node:lts-alpine
33
WORKDIR /app
44

55
COPY . .
6-
RUN yarn install --frozen-lockfile \
7-
&& yarn env-gen
6+
7+
RUN apk --no-cache add curl \
8+
&& yarn install --frozen-lockfile \
9+
&& yarn env-gen \
10+
&& curl -fsS https://dotenvx.sh/install.sh | sh

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,32 @@
33

44
Discord bot built with Discord.JS for Section IODA guild.
55

6+
This application uses environment variables to build and run properly.
67
The following environment variables must be filled in a `.env` file.
7-
This file is used when building the container to generate a production-ready .env file.
88
```dotenv
9-
DISCORD_TOKEN=
9+
NODE_ENV=development
1010
POSTGRES_DB=
1111
POSTGRES_USER=
12+
```
13+
An empty copy of this file is available as [#.env](./#.env).
14+
15+
The following environment ones in `.env.<NODE_ENV>` file.
16+
```dotenv
17+
DISCORD_TOKEN=
1218
POSTGRES_PASSWORD=
1319
DATABASE_PORT=5432
1420
DATABASE_HOST=localhost
15-
NODE_ENV=development
21+
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${POSTGRES_DB}?schema=public
1622
SENDGRID_API_KEY=
1723
```
18-
An empty copy of this file is available as [#.env](./#.env).
24+
An empty copy of this file is available as [#.env.development](./#.env.development).
1925

2026
## Deployment
2127
As the app is dockerized, you can deploy it on your server or locally on your machine.
2228

23-
If you wish to deploy it with a development configuration*, you can run `docker-compose up`.
29+
If you wish to deploy it with a development configuration*, you can run `docker compose up`.
2430

2531
If you need it to be deployed on production ground, change the `NODE_ENV=development` value in the `.env` file by `NODE_ENV=production`.
26-
You can then run the `docker-compose up` command!
32+
You can then run the `docker compose up` command!
2733

2834
_\* the Developer eXperience (DX) is a priority to us, which means default commands will always trigger processes for the development environment, never for the production one!_

docker-compose.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
x-env_file:
22
&env-file
3-
env_file: ./.env
3+
env_file:
4+
- ./.env
5+
- ./.env.${NODE_ENV}
6+
47
x-network:
58
&network
69
networks:
710
- postgres-network
11+
812
x-restart-policy:
913
&restart-policy
1014
restart: always
@@ -26,12 +30,15 @@ services:
2630
interval: 1s
2731
timeout: 1s
2832
retries: 60
33+
2934
bot:
3035
<<: [*env-file, *network, *restart-policy]
36+
environment:
37+
- NODE_ENV=${NODE_ENV}
3138
build:
3239
context: .
3340
dockerfile: ./Dockerfile
34-
command: yarn tsx ./index.ts | tee -a /var/log/datadrop/console.log
41+
command: dotenvx run --convention=nextjs -- yarn start
3542
volumes:
3643
- bot_logs:/var/log/datadrop/
3744
depends_on:

index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { GatewayIntentBits } from "discord.js";
2-
import * as dotenv from "dotenv";
2+
import * as dotenvx from "@dotenvx/dotenvx";
33

44
import { readConfig } from "./src/config.js";
55
import { DatadropClient } from "./src/datadrop.js";
66
import type { Configuration } from "./src/models/Configuration.js";
77

8-
dotenv.config({ debug: Boolean(process.env.DEBUG), encoding: "utf-8" });
8+
dotenvx.config({ debug: Boolean(process.env.DEBUG), encoding: "utf-8" });
99

1010
let client: DatadropClient;
1111
readConfig()

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"type": "module",
55
"main": "./build/index.js",
66
"scripts": {
7-
"start": "docker-compose up --build",
8-
"stop": "docker-compose down",
9-
"deploy:commands": "tsc && node ./scripts/deploy-commands.cjs",
7+
"cu": "docker compose up --build",
8+
"cd": "docker compose down",
9+
"start": "tsx ./index.ts | tee -a /var/log/datadrop/console.log",
1010
"build": "rm -rf build/ && tsc",
11-
"lint": "biome check --write ./src index.ts ./scripts",
11+
"deploy:commands": "yarn build && node ./scripts/deploy-commands.cjs",
12+
"lint": "biome check --write index.ts ./src ./scripts",
1213
"env-gen": "node ./scripts/envgen.cjs"
1314
},
1415
"repository": {
@@ -22,14 +23,14 @@
2223
},
2324
"homepage": "https://github.com/section-IG/DataDrop#readme",
2425
"dependencies": {
26+
"@dotenvx/dotenvx": "^1.21.1",
2527
"@hunteroi/advanced-logger": "^0.2.0",
2628
"@hunteroi/discord-selfrole": "^4.0.4",
2729
"@hunteroi/discord-temp-channels": "^3.3.0",
2830
"@hunteroi/discord-verification": "^1.5.0",
2931
"@sendgrid/mail": "8.1.3",
3032
"discord-sync-commands": "^0.3.0",
3133
"discord.js": "^14.16.2",
32-
"dotenv": "^16.4.5",
3334
"ts-postgres": "1.3.0"
3435
},
3536
"devDependencies": {

0 commit comments

Comments
 (0)