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

filePath.ts

92 lines3.6 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 // A bare `~` is not a host path; the home folder is `~/`.
27 path: /^[A-Za-z]:$/.test(path) ? `${path}\\` : hostPath && path === "~" ? "~/" : path,
28 kind: index === parts.length - 1 ? ("file" as const) : ("directory" as const),
29 };
30 }),
31 ];
33​
34/** Use the crumb's path spelling so trailing or repeated separators still match. */
35export function breadcrumbPathOf(relativePath: string): string {
36 return fileBreadcrumbs("", relativePath).at(-1)?.path ?? relativePath;
38​
39/** UNC servers are not listable directories; their shares are. */
40export function isListableHostFolder(path: string): boolean {
41 return isHostPath(path) && !/^\\\\[^\\/]+$/.test(path);
43​
44function trailCrumbIndex(trail: string, path: string): number {
45 const crumbPath = breadcrumbPathOf(path);
46 return fileBreadcrumbs("", trail).findIndex((crumb) => crumb.path === crumbPath);
48​
49/** Retain descendant crumbs when navigating up; other navigation resets the trail to path. */
50export function retainedBreadcrumbTrail(trail: string | undefined, path: string): string {
51 return trail !== undefined && trailCrumbIndex(trail, path) >= 0 ? trail : path;
53​
54export function trailChildOf(folder: string, trail: string): string | null {
55 const index = trailCrumbIndex(trail, folder);
56 return index < 0 ? null : (fileBreadcrumbs("", trail)[index + 1]?.label ?? null);
58​
59export function joinHostPath(folder: string, relativePath: string): string {
60 const separator = isWindowsAbsolutePath(folder) ? "\\" : "/";
61 const base = folder.endsWith(separator) ? folder : `${folder}${separator}`;
62 return base + relativePath.split("/").join(separator);
64​
65export function fileBreadcrumbChildren(
66 entries: readonly ProjectEntry[],
67 directoryPath: string,
68): FileBreadcrumbChild[] {
69 let collator: Intl.Collator | undefined;
70 const prefix = directoryPath ? `${directoryPath}/` : "";
71 return entries
72 .flatMap((entry) => {
73 if (!entry.path.startsWith(prefix)) return [];
74 const label = entry.path.slice(prefix.length);
75 if (!label || label.includes("/")) return [];
76 return [{ ...entry, label }];
77 })
78 .toSorted((left, right) => {
79 if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;
80 collator ??= new Intl.Collator(undefined, {
81 numeric: true,
82 sensitivity: "base",
83 });
84 return collator.compare(left.label, right.label);
85 });
87​
88export function fileBreadcrumbParent(directoryPath: string): string | null {
89 if (!directoryPath) return null;
90 const separatorIndex = directoryPath.lastIndexOf("/");
91 return separatorIndex === -1 ? "" : directoryPath.slice(0, separatorIndex);