import fs from "node:fs";
import { Resource } from "../resource.js";
import { ignore } from "../util/ignore.js";
/**
 * Folder Resource
 *
 * Creates and manages directories in the filesystem with automatic parent
 * directory creation and cleanup on deletion.
 *
 * @example
 * // Create a directory using id as path
 * const dir = await Folder("uploads");
 *
 * @example
 * // Create a directory with explicit path
 * const dir = await Folder("uploads", {
 *   path: "uploads"
 * });
 *
 * @example
 * // Create a nested directory structure
 * const logs = await Folder("var/log/app", {
 *   path: "var/log/app"
 * });
 */
export const Folder = Resource("fs::Folder", async function (id, props) {
    const dirPath = props?.path ?? id;
    if (this.phase === "delete") {
        if (props?.delete !== false) {
            // we just do a best effort attempt
            await ignore(["ENOENT", "ENOTEMPTY"], async () => fs.promises.rm(dirPath, { recursive: props?.clean ?? false }));
        }
        return this.destroy();
    }
    await ignore("EEXIST", async () => fs.promises.mkdir(dirPath, { recursive: props?.recursive ?? true }));
    return {
        path: dirPath,
    };
});
//# sourceMappingURL=folder.js.map