assetsUnder

tcmts.cli.mirror.assetsUnder · #f6johidj · term · runs on the server

uses node:path

import { FILES_MARK, isAsset, NEVER } from "#codebase/tcmts/cli/mirror";
import fs from "node:fs";
import path from "node:path";

export function assetsUnder(dir: string, exclude: Set<string>, marked?: { at: string; skip: string[] }): string[] {
  // in a directory marked as holding files, all of it is, but what the mark's lines leave out (like .gitignore's:
  // `*.vsix`)
  const mark = path.join(dir, FILES_MARK);
  const within = marked ?? (fs.existsSync(mark) ? { at: dir, skip: fs.readFileSync(mark, "utf8").split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("#")) } : undefined);
  return fs.readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
    const p = path.join(dir, e.name);
    if (exclude.has(path.resolve(p)) || NEVER.has(e.name)) return [];
    const rel = within && path.relative(within.at, p);
    if (rel && within.skip.some((g) => path.matchesGlob(rel, g) || path.matchesGlob(e.name, g))) return [];
    // (another codebase, inside this one's directory, is its own)
    if (e.isDirectory()) return ["node_modules", "dist", "build"].includes(e.name) || fs.existsSync(path.join(p, ".tcm")) ? [] : assetsUnder(p, exclude, within);
    return within || isAsset(e.name) ? [p] : [];
  });
}

uses

used by