diff --git a/src/index.ts b/src/index.ts index 9048d6d..cce0a82 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/templates/mysql.docker-compose.yml b/src/templates/mysql.docker-compose.yml new file mode 100644 index 0000000..7743169 --- /dev/null +++ b/src/templates/mysql.docker-compose.yml @@ -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 diff --git a/src/templates/postgres.docker-compose.yml b/src/templates/postgres.docker-compose.yml new file mode 100644 index 0000000..0d410b8 --- /dev/null +++ b/src/templates/postgres.docker-compose.yml @@ -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 diff --git a/src/utils/generate-docker-compose-file.ts b/src/utils/generate-docker-compose-file.ts index 650bac9..d664314 100644 --- a/src/utils/generate-docker-compose-file.ts +++ b/src/utils/generate-docker-compose-file.ts @@ -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; }