-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Add templates for generating Docker Compose files
- Loading branch information
1 parent
0fe80b9
commit 32f327b
Showing
4 changed files
with
79 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
version: "3.9" | ||
|
||
name: ${selectedProjectName}-mysql | ||
|
||
services: | ||
# This is your local MySQL database instance | ||
mysql-db: | ||
image: mysql | ||
restart: always | ||
environment: | ||
MYSQL_DATABASE: ${selectedProjectName} | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_USER: dev | ||
MYSQL_PASSWORD: dev | ||
volumes: | ||
- ${selectedProjectName}-data:/var/lib/mysql | ||
ports: | ||
- "6969:3306" # Access the DB at port 6969 | ||
|
||
# Use Adminer to quickly view the database at localhost:8089 | ||
adminer: | ||
image: adminer | ||
restart: always | ||
ports: | ||
- "8089:8080" | ||
|
||
volumes: | ||
${selectedProjectName}-data: | ||
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
version: "3.9" | ||
|
||
name: ${selectedProjectName}-postgres | ||
|
||
services: | ||
# This is your local Postgres database instance | ||
postgres-db: | ||
image: postgres | ||
restart: always | ||
environment: | ||
POSTGRES_DB: ${selectedProjectName} | ||
POSTGRES_USER: dev | ||
POSTGRES_PASSWORD: dev | ||
volumes: | ||
- ${selectedProjectName}-data:/var/lib/postgresql/data | ||
ports: | ||
- "6969:5432" # Access the DB at port 6969 | ||
|
||
# Use Adminer to quickly view the database at localhost:8069 | ||
adminer: | ||
image: adminer | ||
restart: always | ||
ports: | ||
- "8069:8080" | ||
|
||
volumes: | ||
${selectedProjectName}-data: | ||
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,29 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
export function generateDockerCompose( | ||
selectedDatabase: string, | ||
selectedProjectName: string, | ||
): string { | ||
switch (selectedDatabase) { | ||
case "postgres": | ||
return ` | ||
version: "3.9" | ||
name: ${selectedProjectName}-postgres | ||
services: | ||
# This is your local Postgres database instance | ||
postgres-db: | ||
image: postgres | ||
restart: always | ||
environment: | ||
POSTGRES_DB: ${selectedProjectName} | ||
POSTGRES_USER: dev | ||
POSTGRES_PASSWORD: dev | ||
volumes: | ||
- ${selectedProjectName}-data:/var/lib/postgresql/data | ||
ports: | ||
- "6969:5432" # Access the DB at port 6969 | ||
# Use Adminer to quickly view the database at localhost:8069 | ||
adminer: | ||
image: adminer | ||
restart: always | ||
ports: | ||
- "8069:8080" | ||
volumes: | ||
${selectedProjectName}-data: | ||
driver: local | ||
`; | ||
case "mysql": | ||
return ` | ||
version: "3.9" | ||
name: ${selectedProjectName}-mysql | ||
const templatePath = path.join( | ||
__dirname, | ||
"..", | ||
"templates", | ||
`${selectedDatabase}.docker-compose.yml`, | ||
); | ||
|
||
if (!fs.existsSync(templatePath)) { | ||
throw new Error(`Template for ${selectedDatabase} not found`); | ||
} | ||
|
||
services: | ||
# This is your local MySQL database instance | ||
mysql-db: | ||
image: mysql | ||
restart: always | ||
environment: | ||
MYSQL_DATABASE: ${selectedProjectName} | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_USER: dev | ||
MYSQL_PASSWORD: dev | ||
volumes: | ||
- ${selectedProjectName}-data:/var/lib/mysql | ||
ports: | ||
- "6969:3306" # Access the DB at port 6969 | ||
// Read the template file | ||
let templateContent = fs.readFileSync(templatePath, "utf-8"); | ||
|
||
# Use Adminer to quickly view the database at localhost:8089 | ||
adminer: | ||
image: adminer | ||
restart: always | ||
ports: | ||
- "8089:8080" | ||
// Replace placeholders (like ${selectedProjectName}) in the template | ||
templateContent = templateContent.replace( | ||
/\$\{selectedProjectName\}/g, | ||
selectedProjectName, | ||
); | ||
|
||
volumes: | ||
${selectedProjectName}-data: | ||
driver: local | ||
`; | ||
default: | ||
throw new Error("Database not supported"); | ||
} | ||
return templateContent; | ||
} |