Open History to see the last commit
1import type { ProjectTrackedFolder } from "@t3tools/contracts";
2import { hasTrailingPathSeparator } from "@t3tools/client-runtime/state/projects";
3import {
4 fileBasename,
5 isAbsolutePath,
6 normalizeProjectPathForDispatch,
7 workspaceRelativeFilePath,
8} from "@t3tools/shared/path";
9
10/** Relative to the project folder when inside it, absolute otherwise. */
11export function trackedFolderDisplayPath(path: string, workspaceRoot: string): string {
12 const relative = workspaceRelativeFilePath(path, workspaceRoot);
13 return relative === null || relative === "." ? path : relative;
14}
15
16/**
17 * The folder a typed path adds. A listing resolves `~/` and names the directory exactly; without
18 * one, only an absolute path is safe to send.
19 */
20export function resolveTrackedFolderInput(input: {
21 readonly query: string;
22 readonly listedDirectoryPath: string | null;
23 readonly exactEntryPath: string | null;
24}): ProjectTrackedFolder | null {
25 const query = input.query.trim();
26 if (query.length === 0) return null;
27 const rawPath =
28 input.exactEntryPath ??
29 (hasTrailingPathSeparator(query) && input.listedDirectoryPath !== null
30 ? input.listedDirectoryPath
31 : isAbsolutePath(query)
32 ? query
33 : null);
34 if (rawPath === null) return null;
35 const path = normalizeProjectPathForDispatch(rawPath);
36 // `ProjectTrackedFolder.name` takes at most 128 characters.
37 const name = fileBasename(path).slice(0, 128).trim();
38 if (path.length === 0 || name.length === 0) return null;
39 return { path, name };
40}