keptAs

tcmts.cli.main.keptAs · #egguur5z · term · runs on the server

uses node:path

The format file is saved in as a file kept as written (as assetFormat says: "" for a Dockerfile), or undefined if it's a scratch file of code. A page, a stylesheet, an image, a file without an extension (Makefile, .gitignore) always is: anything but TypeScript. Code is when the name it goes to holds a file (an example's ui.tsx, as tcm edit writes it), so it's saved back as written rather than read as definitions, or when it's in a directory marked as files (.tcm-files, as a mirror's are): how a new one is added.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { fileLeaf } from "#codebase/tcmts/cli/files";
import { assetFormat, FILES_MARK, joinName } from "#codebase/tcmts/cli/mirror";
import fs from "node:fs";
import path from "node:path";

export function keptAs(cb: Codebase, file: string, into: string | undefined): string | undefined {
  const format = assetFormat(file);
  if (!/^\.?(m|c)?tsx?$/.test(format)) return format;
  if (into === undefined) return undefined;
  let leaf: string;
  try {
    leaf = fileLeaf(file);
  } catch {
    return undefined;
  }
  const holdsFile = cb.store
    .hashesAt(joinName(into, leaf))
    .some((h) => cb.store.getDef(h)?.kind === "file");
  const marked = (() => {
    for (let dir = path.dirname(path.resolve(file)); ; dir = path.dirname(dir)) {
      if (fs.existsSync(path.join(dir, FILES_MARK))) return true;
      if (path.dirname(dir) === dir || fs.existsSync(path.join(dir, ".tcm"))) return false;
    }
  })();
  return holdsFile || marked
    ? `${format.startsWith(".") ? "." : ""}${path.extname(file).slice(1)}`
    : undefined;
}

uses

used by