Files
deepseek-harness/packages/util/workspace-path/src/index.ts
T

52 lines
2.1 KiB
TypeScript

/**
* Browser-safe Workspace path and display helpers.
* @module @deepseek-ai/dsh-util-workspace-path
*/
/** Whether a path uses a Windows drive or UNC prefix. */
function isWindowsStylePath(value: string): boolean {
return /^[A-Za-z]:[/\\]/.test(value) || value.startsWith('\\\\')
}
/**
* Resolve a Workspace-relative path into the Host-facing spelling used by path operations.
* @param cwd - Session Workspace root, when known.
* @param path - Absolute or Workspace-relative path.
* @returns an absolute path when a Workspace root is available, otherwise the original path.
*/
export function resolveWorkspacePath(cwd: string | undefined, path: string): string {
if (path.startsWith('/') || isWindowsStylePath(path)) return path
if (cwd === undefined || cwd === '') return path
const base = cwd.replace(/[/\\]+$/, '')
const relative = path.replace(/^[/\\]+/, '')
return `${base}/${relative}`
}
/**
* Abbreviate a POSIX home directory for display.
* @param path - Absolute or already-short display path.
* @param home - Host account home; absent skips abbreviation.
* @returns `~` or `~/…` for the POSIX home and its descendants, otherwise `path`.
*/
export function abbreviateHomePath(path: string, home?: string): string {
if (home === undefined || home === '') return path
if (isWindowsStylePath(path) || isWindowsStylePath(home)) return path
const root = home.replace(/\/+$/, '')
if (root === '' || root === '/') return path
if (path.replace(/\/+$/, '') === root) return '~'
if (path.startsWith(`${root}/`)) return `~${path.slice(root.length)}`
return path
}
/**
* Read the final non-empty segment of a Workspace path for display.
* Workspace-label surfaces use this helper instead of deriving another basename.
* @param path - Workspace directory path using POSIX or Windows separators.
* @returns the final segment, or an empty string for a separator-only path.
*/
export function workspaceTitleOf(path: string): string {
const trimmed = path.replace(/[/\\]+$/, '')
const separator = Math.max(trimmed.lastIndexOf('/'), trimmed.lastIndexOf('\\'))
return trimmed.slice(separator + 1)
}