From 43f77adcbbc108008e856f67e1f0aae7a7a8194f Mon Sep 17 00:00:00 2001 From: Aryan Pokharkar Date: Tue, 8 Oct 2024 02:14:38 +0530 Subject: [PATCH] fix: Sanitize project name for WSL compatibility while running start-database.sh (#1991) --- cli/src/installers/dbContainer.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cli/src/installers/dbContainer.ts b/cli/src/installers/dbContainer.ts index 5d8fcb6847..0a13f198df 100644 --- a/cli/src/installers/dbContainer.ts +++ b/cli/src/installers/dbContainer.ts @@ -5,6 +5,13 @@ import { PKG_ROOT } from "~/consts.js"; import { type Installer } from "~/installers/index.js"; import { parseNameAndPath } from "~/utils/parseNameAndPath.js"; +// Sanitizes a project name to ensure it adheres to Docker container naming conventions. +const sanitizeName = (name: string): string => { + return name + .replace(/[^a-zA-Z0-9_.-]/g, "_") // Replace invalid characters with underscores + .toLowerCase(); // Convert to lowercase for consistency +}; + export const dbContainerInstaller: Installer = ({ projectDir, databaseProvider, @@ -18,11 +25,14 @@ export const dbContainerInstaller: Installer = ({ const scriptDest = path.join(projectDir, "start-database.sh"); // for configuration with postgresql and mysql when project is created with '.' project name const [projectNameParsed] = - projectName == "." ? parseNameAndPath(projectDir) : [projectName]; + projectName === "." ? parseNameAndPath(projectDir) : [projectName]; + + // Sanitize the project name for Docker container usage + const sanitizedProjectName = sanitizeName(projectNameParsed); fs.writeFileSync( scriptDest, - scriptText.replaceAll("project1", projectNameParsed) + scriptText.replaceAll("project1", sanitizedProjectName) ); fs.chmodSync(scriptDest, "755"); };