From 48ea4b6c9f053de88abf779b16d9336bb33b0bee Mon Sep 17 00:00:00 2001 From: Yuren Ju Date: Wed, 27 Sep 2023 13:16:17 +0800 Subject: [PATCH] fix: ensure docker images can be built --- apps/docker/project.json | 4 ++++ apps/server/.env.example | 7 +++++++ apps/server/project.json | 12 +++++++++--- apps/server/scripts/copy-env-file.js | 19 +++++++++++++++++++ package.json | 7 +------ 5 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 apps/server/.env.example create mode 100644 apps/server/scripts/copy-env-file.js diff --git a/apps/docker/project.json b/apps/docker/project.json index cbec64c..921b542 100644 --- a/apps/docker/project.json +++ b/apps/docker/project.json @@ -6,6 +6,10 @@ "targets": { "build": { "dependsOn": [ + { + "target": "copy-env-file", + "projects": ["server"] + }, { "target": "build", "projects": ["web", "server"] diff --git a/apps/server/.env.example b/apps/server/.env.example new file mode 100644 index 0000000..6d3c748 --- /dev/null +++ b/apps/server/.env.example @@ -0,0 +1,7 @@ +MONGO_INITDB_ROOT_USERNAME=initdbrootusername +MONGO_INITDB_ROOT_PASSWORD=initdbrootpassword +MONGO_DATABASE=dpassport +MONGO_DATABASE_USERNAME=dpassportusername +MONGO_DATABASE_PASSWORD=dpassportpassword +MONGO_HOST=localhost +MONGO_CONTAINER_NAME=dpassport-mongo diff --git a/apps/server/project.json b/apps/server/project.json index f14ee20..b0d0c4b 100644 --- a/apps/server/project.json +++ b/apps/server/project.json @@ -73,9 +73,15 @@ } } }, - "docker-build": { - "dependsOn": ["build"], - "command": "docker build -f apps/server/Dockerfile . -t server" + "setup-db": { + "command": "node apps/server/scripts/setup-db.js" + }, + "start-db": { + "command": "node apps/server/scripts/start-db.js" + }, + "copy-env-file": { + "dependsOn": ["setup-db"], + "command": "node apps/server/scripts/copy-env-file.js" } }, "tags": [] diff --git a/apps/server/scripts/copy-env-file.js b/apps/server/scripts/copy-env-file.js new file mode 100644 index 0000000..afcdd56 --- /dev/null +++ b/apps/server/scripts/copy-env-file.js @@ -0,0 +1,19 @@ +const { existsSync, copyFileSync } = require('fs'); +const { join } = require('path'); + +function copyEnvFile() { + const serverPath = join(__dirname, '..'); + const targetPath = join(serverPath, '.env.local'); + const examplePath = join(serverPath, '.env.example'); + + if (!existsSync(targetPath)) { + console.log('.env.local not exists, copy from .env.example'); + copyFileSync(examplePath, targetPath); + } else { + console.log('.env.local does exist, do nothing.'); + } +} + +if (require.main === module) { + copyEnvFile(); +} diff --git a/package.json b/package.json index 2a0e5b9..c2532c5 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,7 @@ "license": "MIT", "scripts": { "start": "nx serve web", - "acceptance": "nx acceptance acceptance", - "setup:db": "node apps/server/scripts/setup-db.js", - "start:db": "node apps/server/scripts/start-db.js", - "predocker-compose:up": "nx build server && npm run setup:db", - "docker-compose:up": "docker-compose --env-file=./apps/server/.env.local up --build --detach", - "docker-compose:down": "docker-compose down" + "acceptance": "nx acceptance acceptance" }, "private": true, "devDependencies": {