import { PathError } from '../errors.ts'; /** * Checks a project path against the lexicon's rules: relative, `/`-separated, with no empty, `.`, or `..` segments. * Returns it in NFC so that one file has one spelling across operating systems. */ export function normalizePath(input: string): string { const path = input.normalize('NFC'); if (path === '') throw new PathError('path is empty', path); if (path.includes('\\')) throw new PathError(`path ${path} uses a backslash`, path); if (path.startsWith('/')) throw new PathError(`path ${path} is absolute`, path); for (const segment of path.split('/')) { if (segment === '') throw new PathError(`path ${path} has an empty segment`, path); if (segment === '.' || segment === '..') throw new PathError(`path ${path} has a ${segment} segment`, path); } if (new TextEncoder().encode(path).length > 1024) throw new PathError(`path ${path} is over 1024 bytes`, path); return path; }