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

trackedFolders.logic.ts

42 lines1.7 KB

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