|
| 1 | +import type { StrapiProject, DockerConfig } from "./types"; |
| 2 | +import { Liquid } from "liquidjs"; |
| 3 | +import { readFileSync } from "fs"; |
| 4 | +import { join } from "path"; |
| 5 | +import { |
| 6 | + writeFilesWithEnv, |
| 7 | + generateSecrets, |
| 8 | + type FileToWrite, |
| 9 | + type EnvFileToWrite, |
| 10 | +} from "./utils/file-writer"; |
| 11 | + |
| 12 | +export async function generateDockerFiles( |
| 13 | + project: StrapiProject, |
| 14 | + config: DockerConfig |
| 15 | +): Promise<void> { |
| 16 | + console.log("🐳 Generating complete Docker setup..."); |
| 17 | + |
| 18 | + const liquid = new Liquid(); |
| 19 | + const secrets = generateSecrets(); |
| 20 | + |
| 21 | + // Map database types to Strapi database clients |
| 22 | + const databaseClientMap = { |
| 23 | + postgresql: "postgres", |
| 24 | + mysql: "mysql", |
| 25 | + mariadb: "mysql", // MariaDB uses mysql client in Strapi |
| 26 | + sqlite: "sqlite", |
| 27 | + }; |
| 28 | + |
| 29 | + // Prepare template variables |
| 30 | + const templateVars = { |
| 31 | + packageManager: project.packageManager, |
| 32 | + environment: |
| 33 | + config.environment === "both" ? "development" : config.environment, |
| 34 | + database: { |
| 35 | + client: databaseClientMap[config.database.type], |
| 36 | + type: config.database.type, |
| 37 | + serviceName: |
| 38 | + config.database.type === "postgresql" |
| 39 | + ? "strapiDB" |
| 40 | + : config.database.type, |
| 41 | + name: config.database.name, |
| 42 | + user: config.database.user, |
| 43 | + password: config.database.password, |
| 44 | + port: config.database.port, |
| 45 | + host: config.database.host, |
| 46 | + image: |
| 47 | + config.database.type === "postgresql" |
| 48 | + ? "postgres" |
| 49 | + : config.database.type === "mariadb" |
| 50 | + ? "mariadb" |
| 51 | + : "mysql", |
| 52 | + tag: |
| 53 | + config.database.type === "postgresql" |
| 54 | + ? "16.0-alpine" |
| 55 | + : config.database.type === "mariadb" |
| 56 | + ? "10.11" |
| 57 | + : "8.0", |
| 58 | + charset: "utf8mb4", |
| 59 | + collation: "utf8mb4_unicode_ci", |
| 60 | + }, |
| 61 | + secrets, |
| 62 | + }; |
| 63 | + |
| 64 | + const files: FileToWrite[] = []; |
| 65 | + |
| 66 | + // Generate Dockerfile (development) |
| 67 | + const dockerfilePath = join( |
| 68 | + __dirname, |
| 69 | + "templates/dockerfile/development.liquid" |
| 70 | + ); |
| 71 | + const dockerfileTemplate = readFileSync(dockerfilePath, "utf8"); |
| 72 | + const dockerfile = await liquid.parseAndRender( |
| 73 | + dockerfileTemplate, |
| 74 | + templateVars |
| 75 | + ); |
| 76 | + |
| 77 | + files.push({ |
| 78 | + path: "Dockerfile", |
| 79 | + content: dockerfile, |
| 80 | + description: "Development Dockerfile", |
| 81 | + }); |
| 82 | + |
| 83 | + // Generate Dockerfile.prod (production) if production or both |
| 84 | + if (config.environment === "production" || config.environment === "both") { |
| 85 | + const dockerfileProdPath = join( |
| 86 | + __dirname, |
| 87 | + "templates/dockerfile/production.liquid" |
| 88 | + ); |
| 89 | + const dockerfileProdTemplate = readFileSync(dockerfileProdPath, "utf8"); |
| 90 | + const dockerfileProd = await liquid.parseAndRender( |
| 91 | + dockerfileProdTemplate, |
| 92 | + templateVars |
| 93 | + ); |
| 94 | + |
| 95 | + files.push({ |
| 96 | + path: "Dockerfile.prod", |
| 97 | + content: dockerfileProd, |
| 98 | + description: "Production Dockerfile", |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + // Generate docker-compose.yml if requested |
| 103 | + if (config.useCompose) { |
| 104 | + // SQLite uses a different template (no separate database service) |
| 105 | + const templateName = |
| 106 | + config.database.type === "sqlite" ? "sqlite" : "complete"; |
| 107 | + const composePath = join( |
| 108 | + __dirname, |
| 109 | + `templates/compose/${templateName}.liquid` |
| 110 | + ); |
| 111 | + const composeTemplate = readFileSync(composePath, "utf8"); |
| 112 | + const compose = await liquid.parseAndRender(composeTemplate, templateVars); |
| 113 | + |
| 114 | + const description = |
| 115 | + config.database.type === "sqlite" |
| 116 | + ? "Docker Compose for Strapi with SQLite" |
| 117 | + : "Docker Compose with Strapi + Database"; |
| 118 | + |
| 119 | + files.push({ |
| 120 | + path: "docker-compose.yml", |
| 121 | + content: compose, |
| 122 | + description, |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + // Prepare .env file for smart merging |
| 127 | + const envFile: EnvFileToWrite = { |
| 128 | + path: ".env", |
| 129 | + templateVars, |
| 130 | + description: "Environment variables (merged with existing)", |
| 131 | + }; |
| 132 | + |
| 133 | + // Write all files with smart env handling |
| 134 | + await writeFilesWithEnv(files, envFile, project.path); |
| 135 | + |
| 136 | + // Show next steps |
| 137 | + console.log("\n🎯 Next steps:"); |
| 138 | + console.log("1. Review the generated files"); |
| 139 | + console.log("2. Run: docker-compose up -d"); |
| 140 | + console.log("3. Your Strapi app will be available at http://localhost:1337"); |
| 141 | + console.log( |
| 142 | + "\n⚠️ Keep your .env file secure - it contains sensitive secrets!" |
| 143 | + ); |
| 144 | +} |
0 commit comments