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

filePath.ts

107 lines4.1 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/**
34 * `relativePath` written the way its own crumb names it, so a link with a
35 * trailing or repeated separator still finds its crumb.
36 */
37export function breadcrumbPathOf(relativePath: string): string {
38 return fileBreadcrumbs("", relativePath).at(-1)?.path ?? relativePath;
40​
41/**
42 * Whether a host folder crumb names a directory the server can list. A UNC
43 * server (`\\server`) is not one; its shares (`\\server\share`) are.
44 */
45export function isListableHostFolder(path: string): boolean {
46 return isHostPath(path) && !/^\\\\[^\\/]+$/.test(path);
48​
49/** Index of `path`'s crumb among `trail`'s crumbs; -1 when the trail does not pass through it. */
50function trailCrumbIndex(trail: string, path: string): number {
51 const crumbPath = breadcrumbPathOf(path);
52 return fileBreadcrumbs("", trail).findIndex((crumb) => crumb.path === crumbPath);
54​
55/**
56 * The deepest path the breadcrumbs keep showing after the browser moves to `path`.
57 * Going up keeps the folders below visible so the way back down is one click;
58 * moving anywhere else starts over at `path`.
59 */
60export function retainedBreadcrumbTrail(trail: string | undefined, path: string): string {
61 return trail !== undefined && trailCrumbIndex(trail, path) >= 0 ? trail : path;
63​
64/** The entry of `folder` that leads toward `trail`; null when the trail ends at the folder. */
65export function trailChildOf(folder: string, trail: string): string | null {
66 const index = trailCrumbIndex(trail, folder);
67 return index < 0 ? null : (fileBreadcrumbs("", trail)[index + 1]?.label ?? null);
69​
70/**
71 * A host path for `relativePath` (as a host folder's listing names it, with `/`)
72 * inside host folder `folder`, using the folder's own separator.
73 */
74export function joinHostPath(folder: string, relativePath: string): string {
75 const separator = isWindowsAbsolutePath(folder) ? "\\" : "/";
76 const base = folder.endsWith(separator) ? folder : `${folder}${separator}`;
77 return base + relativePath.split("/").join(separator);
79​
80export function fileBreadcrumbChildren(
81 entries: readonly ProjectEntry[],
82 directoryPath: string,
83): FileBreadcrumbChild[] {
84 let collator: Intl.Collator | undefined;
85 const prefix = directoryPath ? `${directoryPath}/` : "";
86 return entries
87 .flatMap((entry) => {
88 if (!entry.path.startsWith(prefix)) return [];
89 const label = entry.path.slice(prefix.length);
90 if (!label || label.includes("/")) return [];
91 return [{ ...entry, label }];
92 })
93 .toSorted((left, right) => {
94 if (left.kind !== right.kind) return left.kind === "directory" ? -1 : 1;
95 collator ??= new Intl.Collator(undefined, {
96 numeric: true,
97 sensitivity: "base",
98 });
99 return collator.compare(left.label, right.label);
100 });
102​
103export function fileBreadcrumbParent(directoryPath: string): string | null {
104 if (!directoryPath) return null;
105 const separatorIndex = directoryPath.lastIndexOf("/");
106 return separatorIndex === -1 ? "" : directoryPath.slice(0, separatorIndex);