import { readdir, readFile } from "node:fs/promises";
import { createRequire } from "node:module";
import path from "node:path";
const require = createRequire(import.meta.url);
const createIgnore = require("ignore");
export async function readDockerBuildContextFiles(buildContext) {
    const rootEntries = await readdir(buildContext, { withFileTypes: true });
    const dockerignoreEntry = rootEntries.find((entry) => entry.isFile() && String(entry.name) === ".dockerignore");
    const ignored = createIgnore();
    if (dockerignoreEntry !== undefined) {
        ignored.add(String(await readFile(path.join(buildContext, ".dockerignore"))));
    }
    const files = [];
    await collectBuildContextFiles(buildContext, "", files, ignored, rootEntries);
    return files.sort((left, right) => left.relativePath.localeCompare(right.relativePath));
}
async function collectBuildContextFiles(buildContext, relativeDir, files, ignored, entries) {
    const absoluteDir = path.join(buildContext, relativeDir);
    const dirEntries = entries ?? (await readdir(absoluteDir, { withFileTypes: true }));
    for (const entry of dirEntries) {
        const relativePath = path.join(relativeDir, String(entry.name));
        const dockerPath = relativePath.split(path.sep).join("/");
        if (entry.isDirectory()) {
            if (!ignored.ignores(`${dockerPath}/`)) {
                await collectBuildContextFiles(buildContext, relativePath, files, ignored);
            }
            continue;
        }
        if (!entry.isFile() || (dockerPath !== ".dockerignore" && ignored.ignores(dockerPath))) {
            continue;
        }
        files.push({
            relativePath: dockerPath,
            bytes: await readFile(path.join(buildContext, relativePath))
        });
    }
}
