/**
 * @license
 * Copyright 2022-2026 Greg Lauckhart <greg@lauckhart.com>
 * SPDX-License-Identifier: Apache-2.0
 */
import { readdirSync, readFileSync, statSync } from "node:fs";
import { resolve } from "node:path";
import { ignoreErrorSync } from "./errors.js";
function isNotFoundError(e) {
  return typeof e === "object" && e !== null && "code" in e && (e.code === "ENOENT" || e.code === "ENOTDIR");
}
function maybeStatSync(path) {
  try {
    return statSync(path);
  } catch (e) {
    if (isNotFoundError(e)) {
      return;
    }
    throw e;
  }
}
function maybeReadJsonSync(path) {
  try {
    return JSON.parse(readFileSync(path, "utf-8"));
  } catch (e) {
    if (isNotFoundError(e)) {
      return;
    }
    if (e instanceof SyntaxError) {
      e.message = `Error parsing ${path}: ${e.message}`;
      throw e;
    }
    throw e;
  }
}
function maybeReaddirSync(path) {
  try {
    return readdirSync(path);
  } catch (e) {
    if (isNotFoundError(e)) {
      return;
    }
  }
}
function isDirectory(path) {
  return !!ignoreErrorSync("ENOENT", () => statSync(path).isDirectory());
}
function isFile(path) {
  return !!ignoreErrorSync("ENOENT", () => statSync(path).isFile());
}
function FilesystemSync() {
  return {
    resolve(...segments) {
      return resolve(...segments);
    },
    readdir(path) {
      return maybeReaddirSync(path);
    },
    stat(path) {
      const stats = maybeStatSync(path);
      if (stats) {
        return {
          get isFile() {
            return stats.isFile();
          },
          get isDirectory() {
            return stats.isDirectory();
          }
        };
      }
    }
  };
}
export {
  FilesystemSync,
  isDirectory,
  isFile,
  maybeReadJsonSync,
  maybeReaddirSync,
  maybeStatSync
};
//# sourceMappingURL=file.js.map
