"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureDirSync = exports.ensureDir = void 0;
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const dntShim = __importStar(require("../../../../../_dnt.shims.js"));
const _get_file_info_type_js_1 = require("./_get_file_info_type.js");
/**
 * Ensures that the directory exists.
 * If the directory structure does not exist, it is created. Like mkdir -p.
 * Requires the `--allow-read` and `--allow-write` flag.
 *
 * @example
 * ```ts
 * import { ensureDir } from "@std/fs";
 *
 * ensureDir("./bar"); // returns a promise
 * ```
 */
async function ensureDir(dir) {
    try {
        const fileInfo = await dntShim.Deno.lstat(dir);
        if (!fileInfo.isDirectory) {
            throw new Error(`Ensure path exists, expected 'dir', got '${(0, _get_file_info_type_js_1.getFileInfoType)(fileInfo)}'`);
        }
        return;
    }
    catch (err) {
        if (!(err instanceof dntShim.Deno.errors.NotFound)) {
            throw err;
        }
    }
    // The dir doesn't exist. Create it.
    // This can be racy. So we catch AlreadyExists and check lstat again.
    try {
        await dntShim.Deno.mkdir(dir, { recursive: true });
    }
    catch (err) {
        if (!(err instanceof dntShim.Deno.errors.AlreadyExists)) {
            throw err;
        }
        const fileInfo = await dntShim.Deno.lstat(dir);
        if (!fileInfo.isDirectory) {
            throw new Error(`Ensure path exists, expected 'dir', got '${(0, _get_file_info_type_js_1.getFileInfoType)(fileInfo)}'`);
        }
    }
}
exports.ensureDir = ensureDir;
/**
 * Ensures that the directory exists.
 * If the directory structure does not exist, it is created. Like mkdir -p.
 * Requires the `--allow-read` and `--allow-write` flag.
 *
 * @example
 * ```ts
 * import { ensureDirSync } from "@std/fs";
 *
 * ensureDirSync("./ensureDirSync"); // void
 * ```
 */
function ensureDirSync(dir) {
    try {
        const fileInfo = dntShim.Deno.lstatSync(dir);
        if (!fileInfo.isDirectory) {
            throw new Error(`Ensure path exists, expected 'dir', got '${(0, _get_file_info_type_js_1.getFileInfoType)(fileInfo)}'`);
        }
        return;
    }
    catch (err) {
        if (!(err instanceof dntShim.Deno.errors.NotFound)) {
            throw err;
        }
    }
    // The dir doesn't exist. Create it.
    // This can be racy. So we catch AlreadyExists and check lstat again.
    try {
        dntShim.Deno.mkdirSync(dir, { recursive: true });
    }
    catch (err) {
        if (!(err instanceof dntShim.Deno.errors.AlreadyExists)) {
            throw err;
        }
        const fileInfo = dntShim.Deno.lstatSync(dir);
        if (!fileInfo.isDirectory) {
            throw new Error(`Ensure path exists, expected 'dir', got '${(0, _get_file_info_type_js_1.getFileInfoType)(fileInfo)}'`);
        }
    }
}
exports.ensureDirSync = ensureDirSync;
