Skip to content

Commit

Permalink
♻️ Add templates for generating Docker Compose files
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Sep 21, 2024
1 parent 0fe80b9 commit 32f327b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ program
// Get the current directory name to use as the default project name
const currentDir = path.basename(process.cwd());

console.log("\n");
p.intro(`create-docker-db`);

// Prompts user to select database and project name
Expand Down
29 changes: 29 additions & 0 deletions src/templates/mysql.docker-compose.yml
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
28 changes: 28 additions & 0 deletions src/templates/postgres.docker-compose.yml
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
85 changes: 21 additions & 64 deletions src/utils/generate-docker-compose-file.ts
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;
}

0 comments on commit 32f327b

Please sign in to comment.