feat: using theme name for folder instead themeid

This commit is contained in:
Moyasee
2025-11-09 15:28:52 +02:00
parent 53bc3551e1
commit e272470a7b
7 changed files with 77 additions and 28 deletions

View File

@@ -38,23 +38,56 @@ export const normalizePath = (str: string) =>
export const addTrailingSlash = (str: string) =>
str.endsWith("/") ? str : `${str}/`;
export const getThemePath = (themeId: string) =>
path.join(THEMES_PATH, themeId);
const sanitizeFolderName = (name: string): string => {
return name
.toLowerCase()
.replace(/[^a-z0-9-_\s]/g, "")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "");
};
export const getThemeSoundPath = (themeId: string): string | null => {
const themeDir = getThemePath(themeId);
export const getThemePath = (themeId: string, themeName?: string): string => {
if (themeName) {
const sanitizedName = sanitizeFolderName(themeName);
if (sanitizedName) {
return path.join(THEMES_PATH, sanitizedName);
}
}
return path.join(THEMES_PATH, themeId);
};
export const getThemeSoundPath = (
themeId: string,
themeName?: string
): string | null => {
const themeDir = getThemePath(themeId, themeName);
const legacyThemeDir = themeName ? path.join(THEMES_PATH, themeId) : null;
const checkDir = (dir: string): string | null => {
if (!fs.existsSync(dir)) {
return null;
}
const formats = ["wav", "mp3", "ogg", "m4a"];
for (const format of formats) {
const soundPath = path.join(dir, `achievement.${format}`);
if (fs.existsSync(soundPath)) {
return soundPath;
}
}
if (!fs.existsSync(themeDir)) {
return null;
};
const soundPath = checkDir(themeDir);
if (soundPath) {
return soundPath;
}
const formats = ["wav", "mp3", "ogg", "m4a"];
for (const format of formats) {
const soundPath = path.join(themeDir, `achievement.${format}`);
if (fs.existsSync(soundPath)) {
return soundPath;
}
if (legacyThemeDir) {
return checkDir(legacyThemeDir);
}
return null;