Renders filePath relative to root, POSIX-separated whatever the platform.
Two deliberate non-conversions:
An already-relative filePath is returned as given (POSIX-normalized). That makes the function
idempotent, which matters because a model's file is relativized once and then passed through render
helpers that call this again. Resolving a relative path against process.cwd() first, which is what
path.relative does on its own, silently gives a wrong answer whenever the working directory is not root.
An absolute path that would need to climb outside root is returned unchanged, never rewritten into a
../ escape: a file discovered in another package legitimately lives outside root, and its absolute path
is the only unambiguous thing to say about it.
Parameters
root: string
The directory paths are published relative to.
filePath: string
An absolute path, or one already relative to root.
Renders
filePathrelative toroot, POSIX-separated whatever the platform.Two deliberate non-conversions:
filePathis returned as given (POSIX-normalized). That makes the function idempotent, which matters because a model'sfileis relativized once and then passed through render helpers that call this again. Resolving a relative path againstprocess.cwd()first, which is whatpath.relativedoes on its own, silently gives a wrong answer whenever the working directory is notroot.rootis returned unchanged, never rewritten into a../escape: a file discovered in another package legitimately lives outsideroot, and its absolute path is the only unambiguous thing to say about it.