shownPath

tcmts.cli.ui.shownPath · #awptckof · term · runs on the server

uses node:fs

A file's path as tcm says it: from from (where it runs) when the file is in there, or else whole. Where the two are is compared as they really are, so a file reached through a link (a session's directory, in a codebase linked from another checkout) isn't shown as a climb out of one and down the other.

import fs from "node:fs";
import path from "node:path";

export function shownPath(file: string, from: string = process.cwd()): string {
  const real = (p: string): string => {
    try {
      return fs.realpathSync(p);
    } catch {
      // (not there yet: its directory, as it really is)
      const dir = path.dirname(p);
      return dir === p ? p : path.join(real(dir), path.basename(p));
    }
  };
  const abs = path.resolve(from, file);
  const rel = path.relative(real(from), real(abs));
  return rel && rel.split(path.sep)[0] !== ".." && !path.isAbsolute(rel) ? rel : abs;
}

used by