Files
t3code/apps/server/src/worktreesDirectory.ts
T

48 lines
1.7 KiB
TypeScript

import type * as Path from "effect/Path";
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
/**
* Directory new worktrees are created under: the `worktreesDirectory`
* setting (`~` expands to `home`), or `defaultDir` (`<T3 home>/worktrees`)
* when it is empty. Null when
* the setting is not an absolute path on this machine, such as `D:\worktrees`
* configured for a Windows server and synced to a Linux one, or when it is a
* filesystem root, which would make every path on that drive look managed.
*/
export function resolveWorktreesDirectory(
setting: string,
defaultDir: string,
path: Path.Path,
home: string,
): string | null {
if (setting === "") return defaultDir;
const expanded = expandHomePath(setting, home);
if (!path.isAbsolute(expanded)) return null;
const resolved = path.resolve(expanded);
return isFilesystemRoot(resolved, path) ? null : resolved;
}
/** Callers re-check after resolving symlinks: a link can point at a root. */
export function isFilesystemRoot(directory: string, path: Path.Path): boolean {
return path.dirname(directory) === directory;
}
/** Every directory that holds T3-managed worktrees on this machine. */
export function managedWorktreesDirectories(
settings: {
readonly worktreesDirectory: string;
readonly previousWorktreesDirectories: ReadonlyArray<string>;
},
defaultDir: string,
path: Path.Path,
home: string,
): ReadonlyArray<string> {
const directories = new Set([defaultDir]);
for (const setting of [settings.worktreesDirectory, ...settings.previousWorktreesDirectories]) {
const directory = resolveWorktreesDirectory(setting, defaultDir, path, home);
if (directory !== null) directories.add(directory);
}
return [...directories];
}