All files / src/utils path.ts

100% Statements 10/10
100% Branches 16/16
100% Functions 4/4
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40              86x             64x             44x             21x 3x   18x 1x   17x 8x   9x    
import { homedir } from 'node:os';
import { isAbsolute, join } from 'node:path';
 
/**
 * Normalize path separators to forward slashes for cross-platform consistency.
 */
export function normalizePath(path: string): string {
  return path.replace(/\\/g, '/');
}
 
/**
 * Check whether a normalized path stays within a repository-relative boundary.
 */
export function isRepoRelativePath(path: string): boolean {
  return path !== '' && path !== '..' && !path.startsWith('../') && !isAbsolute(path);
}
 
/**
 * Check whether a target string should be treated as a filesystem path.
 */
export function isPathLike(value: string): boolean {
  return value === '~' || value.startsWith('.') || value.includes('/') || value.includes('\\');
}
 
/**
 * Resolve a CLI path target against a base directory.
 */
export function resolvePathTarget(path: string, baseDir?: string): string {
  if (path.startsWith('~/')) {
    return join(homedir(), path.slice(2));
  }
  if (path === '~') {
    return homedir();
  }
  if (isAbsolute(path)) {
    return path;
  }
  return baseDir ? join(baseDir, path) : path;
}