forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath-helpers.ts
More file actions
38 lines (32 loc) · 1.19 KB
/
Copy pathpath-helpers.ts
File metadata and controls
38 lines (32 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os from 'os'
import path from 'path'
import { getCliEnv } from './env'
import { getProjectRoot } from '../project-files'
import type { CliEnv } from '../types/env'
/**
* Format a path for display, replacing home directory with ~
* @param cwd - The path to format
* @param env - Optional environment object (defaults to CLI env)
*/
export function formatCwd(cwd: string | undefined, env?: CliEnv): string {
if (!cwd) return ''
const resolvedEnv = env ?? getCliEnv()
const homeDir = resolvedEnv.HOME || resolvedEnv.USERPROFILE || os.homedir()
if (homeDir && cwd.startsWith(homeDir)) {
return '~' + cwd.slice(homeDir.length)
}
return cwd
}
/**
* Get relative path from the project root
* e.g., "/Users/foo/project/src/utils/helper.ts" -> "src/utils/helper.ts"
* If already relative or project root unavailable, returns the path as-is
*/
export function getRelativePath(filePath: string): string {
// If it's already a relative path, return as-is
if (!filePath.startsWith('/')) return filePath
const projectRoot = getProjectRoot()
if (!projectRoot) return filePath
// Use Node's path.relative for proper relative path calculation
return path.relative(projectRoot, filePath)
}