Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

filePath.ts

91 lines3.5 KB

1import type { ProjectEntry } from "@t3tools/contracts";
2import { isWindowsAbsolutePath, isHostPath } from "@t3tools/shared/path";
3​
4export interface FileBreadcrumb {
5 label: string;
6 path: string;
7 kind: "project" | "directory" | "file";
8}
9​
10export interface FileBreadcrumbChild extends ProjectEntry {
11 label: string;
13​
14export function fileBreadcrumbs(projectName: string, relativePath: string): FileBreadcrumb[] {
15 const hostPath = isHostPath(relativePath);
16 const separator = isWindowsAbsolutePath(relativePath) ? "\\" : "/";
17 const parts = relativePath.split(/[\\/]/).filter(Boolean);
18 const root = relativePath.startsWith("\\\\") ? "\\\\" : relativePath.startsWith("/") ? "/" : "";
19 return [
20 ...(hostPath ? [] : [{ label: projectName, path: "", kind: "project" as const }]),
21 ...parts.map((part, index) => {
22 const path = root + parts.slice(0, index + 1).join(separator);
23 return {
24 label: part,
25 // A bare drive (`C:`) means the drive's current directory; its root is `C:\`.
26 path: /^[A-Za-z]:$/.test(path) ? `${path}\\` : path,
27 kind: index === parts.length - 1 ? ("file" as const) : ("directory" as const),
28 };
29 }),
30 ];
32​
33/** Use the crumb's path spelling so trailing or repeated separators still match. */
34export function breadcrumbPathOf(relativePath: string): string {
35 return fileBreadcrumbs("", relativePath).at(-1)?.path ?? relativePath;
37​
38/** UNC servers are not listable directories; their shares are. */
39export function isListableHostFolder(path: string): boolean {
40 return isHostPath(path) && !/^\\\\[^\\/]+$/.test(path);
42​
43function trailCrumbIndex(trail: string, path: string): number {
44 const crumbPath = breadcrumbPathOf(path);
45 return fileBreadcrumbs("", trail).findIndex((crumb) => crumb.path === crumbPath);
47​
48/** Retain descendant crumbs when navigating up; other navigation resets the trail to path. */
49export function retainedBreadcrumbTrail(trail: string | undefined, path: string): string {
50 return trail !== undefined && trailCrumbIndex(trail, path) >= 0 ? trail : path;
52​
53export function trailChildOf(folder: string, trail: string): string | null {
54 const index = trailCrumbIndex(trail, folder);
55 return index < 0 ? null : (fileBreadcrumbs("", trail)[index + 1]?.label ?? null);
57​
58export function joinHostPath(folder: string, relativePath: string): string {
59 const separator = isWindowsAbsolutePath(folder) ? "\\" : "/";
60 const base = folder.endsWith(separator) ? folder : `${folder}${separator}`;
61 return base + relativePath.split("/").join(separator);
63​
64export function fileBreadcrumbChildren(
65 entries: readonly ProjectEntry[],
66 directoryPath: string,
67): FileBreadcrumbChild[] {
68 let collator: Intl.Collator | undefined;
69 const prefix = directoryPath ? `${directoryPath}/` : "";
70 return entries
71 .flatMap((entry) => {
72 if (!entry.path.startsWith(prefix)) return [];
73 const label = entry.path.slice(prefix.length);
74 if (!label || label.includes("/")) return [];
75 return [{ ...entry, label }];
76 })
77 .toSorted((left, right) => {
78 if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;
79 collator ??= new Intl.Collator(undefined, {
80 numeric: true,
81 sensitivity: "base",
82 });
83 return collator.compare(left.label, right.label);
84 });
86​
87export function fileBreadcrumbParent(directoryPath: string): string | null {
88 if (!directoryPath) return null;
89 const separatorIndex = directoryPath.lastIndexOf("/");
90 return separatorIndex === -1 ? "" : directoryPath.slice(0, separatorIndex);