/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var dlx_exports = {};
__export(dlx_exports, {
  clearDlx: () => clearDlx,
  clearDlxSync: () => clearDlxSync,
  dlxDirExists: () => dlxDirExists,
  dlxDirExistsAsync: () => dlxDirExistsAsync,
  ensureDlxDir: () => ensureDlxDir,
  ensureDlxDirSync: () => ensureDlxDirSync,
  getDlxInstalledPackageDir: () => getDlxInstalledPackageDir,
  getDlxPackageDir: () => getDlxPackageDir,
  getDlxPackageJsonPath: () => getDlxPackageJsonPath,
  getDlxPackageNodeModulesDir: () => getDlxPackageNodeModulesDir,
  isDlxPackageInstalled: () => isDlxPackageInstalled,
  isDlxPackageInstalledAsync: () => isDlxPackageInstalledAsync,
  listDlxPackages: () => listDlxPackages,
  listDlxPackagesAsync: () => listDlxPackagesAsync,
  removeDlxPackage: () => removeDlxPackage,
  removeDlxPackageSync: () => removeDlxPackageSync
});
module.exports = __toCommonJS(dlx_exports);
var import_node_fs = require("node:fs");
var import_fs = require("./fs");
var import_path = require("./path");
var import_paths = require("./paths");
var import_promises = require("./promises");
let _path;
// @__NO_SIDE_EFFECTS__
function getPath() {
  if (_path === void 0) {
    _path = require("node:path");
  }
  return _path;
}
async function clearDlx() {
  const packages = await listDlxPackagesAsync();
  await (0, import_promises.pEach)(packages, (pkg) => removeDlxPackage(pkg));
}
function clearDlxSync() {
  const packages = listDlxPackages();
  for (const pkg of packages) {
    removeDlxPackageSync(pkg);
  }
}
function dlxDirExists() {
  return (0, import_node_fs.existsSync)((0, import_paths.getSocketDlxDir)());
}
async function dlxDirExistsAsync() {
  try {
    await import_node_fs.promises.access((0, import_paths.getSocketDlxDir)());
    return true;
  } catch {
    return false;
  }
}
async function ensureDlxDir() {
  await import_node_fs.promises.mkdir((0, import_paths.getSocketDlxDir)(), { recursive: true });
}
function ensureDlxDirSync() {
  const { mkdirSync } = require("node:fs");
  mkdirSync((0, import_paths.getSocketDlxDir)(), { recursive: true });
}
function getDlxInstalledPackageDir(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_path.normalizePath)(
    path.join(getDlxPackageNodeModulesDir(packageName), packageName)
  );
}
function getDlxPackageDir(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_path.normalizePath)(path.join((0, import_paths.getSocketDlxDir)(), packageName));
}
function getDlxPackageJsonPath(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_path.normalizePath)(
    path.join(getDlxInstalledPackageDir(packageName), "package.json")
  );
}
function getDlxPackageNodeModulesDir(packageName) {
  const path = /* @__PURE__ */ getPath();
  return (0, import_path.normalizePath)(path.join(getDlxPackageDir(packageName), "node_modules"));
}
function isDlxPackageInstalled(packageName) {
  return (0, import_node_fs.existsSync)(getDlxInstalledPackageDir(packageName));
}
async function isDlxPackageInstalledAsync(packageName) {
  try {
    await import_node_fs.promises.access(getDlxInstalledPackageDir(packageName));
    return true;
  } catch {
    return false;
  }
}
function listDlxPackages() {
  try {
    return (0, import_fs.readDirNamesSync)((0, import_paths.getSocketDlxDir)(), { sort: true });
  } catch {
    return [];
  }
}
async function listDlxPackagesAsync() {
  try {
    const entries = await import_node_fs.promises.readdir((0, import_paths.getSocketDlxDir)(), {
      withFileTypes: true
    });
    return entries.filter((e) => e.isDirectory()).map((e) => e.name).sort();
  } catch {
    return [];
  }
}
async function removeDlxPackage(packageName) {
  const packageDir = getDlxPackageDir(packageName);
  try {
    await (0, import_fs.safeDelete)(packageDir, { recursive: true, force: true });
  } catch (e) {
    throw new Error(`Failed to remove DLX package "${packageName}"`, {
      cause: e
    });
  }
}
function removeDlxPackageSync(packageName) {
  const { rmSync } = require("node:fs");
  const packageDir = getDlxPackageDir(packageName);
  try {
    rmSync(packageDir, { recursive: true, force: true });
  } catch (e) {
    throw new Error(`Failed to remove DLX package "${packageName}"`, {
      cause: e
    });
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  clearDlx,
  clearDlxSync,
  dlxDirExists,
  dlxDirExistsAsync,
  ensureDlxDir,
  ensureDlxDirSync,
  getDlxInstalledPackageDir,
  getDlxPackageDir,
  getDlxPackageJsonPath,
  getDlxPackageNodeModulesDir,
  isDlxPackageInstalled,
  isDlxPackageInstalledAsync,
  listDlxPackages,
  listDlxPackagesAsync,
  removeDlxPackage,
  removeDlxPackageSync
});
//# sourceMappingURL=dlx.js.map
