import { promises as dns } from "node:dns";
import fs from "node:fs";
import path from "node:path";
async function checkConnection() {
  return dns.resolve("example.com").then(
    () => true,
    () => false
  );
}
async function getPkgProp(fullPath, prop, defaultValue) {
  const pkg = JSON.parse(
    fs.readFileSync(path.join(fullPath, "package.json")).toString()
  );
  const propPath = prop.split(".");
  let value = pkg;
  for (const p of propPath) {
    value = value[p];
    if (value === void 0)
      break;
  }
  if (value === void 0 && defaultValue === void 0) {
    throw new Error(
      `Could not find required property ${prop} in package.json of ${fullPath}`
    );
  }
  return value ?? defaultValue;
}
function getErrorMessage(error) {
  if (typeof error === "string")
    return error;
  if (error && typeof error === "object" && "message" in error && typeof error.message === "string") {
    return error.message;
  }
  console.error("Unable to get error message for error", error);
  return "Unknown Error";
}
export {
  checkConnection,
  getErrorMessage,
  getPkgProp
};
