fileLeaf

tcmts.cli.files.fileLeaf · #tvtsmzrz · term · runs on the server

uses node:path

A file's name in its namespace: its name without the extension (dashes kept; no dots), and without the dot a hidden file's name starts with (.gitignore is gitignore, .mcp.json is mcp).

import path from "node:path";

export function fileLeaf(file: string): string {
  const name = path.basename(file).replace(/^\./, "");
  const leaf = name.slice(0, name.length - path.extname(name).length);
  if (!/^[\w$-]+$/.test(leaf)) throw new Error(`${path.basename(file)}: a file is named in the codebase by its name without the extension, which can have letters, digits, _ and - (not dots)`);
  return leaf;
}

used by