core/loaders/leak.output.loader.ts

core/loaders/leak.output.loader.ts is a file in Bane's Lab Content. 34 lines of code and 7 definitions.

import {
    ESCAPED_SEPARATOR,
    PATH_SEPARATOR,
    PUBLISHED_ROOT_KEYS,
    PUBLISHED_TEXT_EXTENSIONS,
    VCS_FOLDER,
} from "#configuration/constants/leak.constants";
import { ROOT, absolutePath } from "@ssot/paths";
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { extname, join, relative, sep } from "node:path";
import type { PublishedFile } from "#types/leak.types";
import { UTF8 } from "#configuration/constants/source.constants";

const WINDOWS_SEPARATOR = "\\";

const textFilesUnder = function textFilesUnder(dir: string): string[] {
    return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
        const full = join(dir, entry.name);
        if (entry.isDirectory()) {
            return entry.name === VCS_FOLDER ? [] : textFilesUnder(full);
        }
        return entry.isFile() && PUBLISHED_TEXT_EXTENSIONS.has(extname(entry.name).toLowerCase()) ? [full] : [];
    });
};

export const publishedPaths = function publishedPaths(): string[] {
    return PUBLISHED_ROOT_KEYS.map((key) => absolutePath(key))
        .filter((root) => existsSync(root))
        .flatMap(textFilesUnder);
};

export const readPublished = function readPublished(path: string): PublishedFile {
    return { file: relative(ROOT, path).split(sep).join(PATH_SEPARATOR), text: readFileSync(path, UTF8) };
};

export const machinePaths = function machinePaths(): string[] {
    const segments = ROOT.split(WINDOWS_SEPARATOR).join(PATH_SEPARATOR).split(PATH_SEPARATOR);
    return [...new Set([ROOT, segments.join(PATH_SEPARATOR), segments.join(ESCAPED_SEPARATOR)])];
};