fix(sql-files): distinguish files with duplicate names

This commit is contained in:
t8y2 committed 2026-07-11 18:32:17 +08:00
1 parent e4563f9659
commit 1dd4623919
13 files changed
+151 -12

No files matched your search

+36 -2
View File
@@ -3,12 +3,46 @@ export function isSqlFilePath(path: string): boolean {
}
export function sqlFileTitleFromPath(path: string): string {
const trimmed = path.trim();
const normalized = trimmed.replace(/\\/g, "/");
const normalized = normalizeExternalSqlPath(path);
const name = normalized.split("/").filter(Boolean).pop();
return name || "script.sql";
}
export function normalizeExternalSqlPath(path: string): string {
const normalized = path
.trim()
.replace(/\\/g, "/")
.replace(/\/{2,}/g, "/");
return normalized.length > 1 ? normalized.replace(/\/$/, "") : normalized;
}
export function externalSqlFileDisplayTitles(paths: string[]): string[] {
const segments = paths.map((path) => normalizeExternalSqlPath(path).split("/").filter(Boolean));
const titles = segments.map((parts) => parts[parts.length - 1] || "script.sql");
const collisions = new Map<string, number[]>();
titles.forEach((title, index) => {
const indexes = collisions.get(title) ?? [];
indexes.push(index);
collisions.set(title, indexes);
});
for (const indexes of collisions.values()) {
if (indexes.length < 2) continue;
const maxDepth = Math.max(...indexes.map((index) => segments[index].length));
for (let depth = 2; depth <= maxDepth; depth++) {
const candidates = indexes.map((index) => segments[index].slice(-depth).join("/"));
if (new Set(candidates).size !== candidates.length) continue;
indexes.forEach((index, candidateIndex) => {
titles[index] = candidates[candidateIndex];
});
break;
}
}
return titles;
}
export function externalSqlFilePaths(paths: string[]): string[] {
return paths.filter(isSqlFilePath);
}