surshar / claude-code
Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.
Built by Anthropic · github.com/anthropics
| 1 | /** |
| 2 | * A path the model handed a Read, made absolute as the Read tool reads it. |
| 3 | * |
| 4 | * As given when it is absolute; under the home directory (less any trailing |
| 5 | * separator) for `~` or a `~/` spelling when a home is known; else under the |
| 6 | * working directory. |
| 7 | * |
| 8 | * @param path the Read's `file_path` as the model spelled it |
| 9 | * @param root the session's working directory, absolute |
| 10 | * @param home the home directory, absolute, when the environment names one |
| 11 | * @returns the absolute path |
| 12 | */ |
| 13 | export function absoluteOf(path: string, root: string, home?: string): string { |
| 14 | const isHomeSpelling = path === '~' || path.startsWith('~/') |
| 15 | |
| 16 | if (isHomeSpelling && home !== undefined) { |
| 17 | const bare = home.replace(/(?<=.)[\\/]+$/, '') |
| 18 | |
| 19 | return path === '~' ? bare : `${bare}${separatorOf(bare)}${path.slice(2)}` |
| 20 | } |
| 21 | |
| 22 | return /^(?:[A-Za-z]:)?[\\/]/.test(path) |
| 23 | ? path |
| 24 | : `${root}${separatorOf(root)}${path}` |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * The separator a directory's own spelling uses. |
| 29 | * |
| 30 | * @param dir an absolute directory |
| 31 | * @returns `\` for a backslash spelling, `/` otherwise |
| 32 | */ |
| 33 | const separatorOf = (dir: string): string => (dir.includes('\\') ? '\\' : '/') |