// src/actions/copy-assets.ts
import path from "node:path/posix";
import chalk from "chalk";
import cpy from "cpy";

// src/pm/workspaces.ts
import { existsSync, readFileSync } from "node:fs";
import PATH from "node:path";
import { globSync } from "glob";
function readJsonFile(path2) {
  if (!existsSync(path2)) return void 0;
  try {
    return JSON.parse(readFileSync(path2, "utf8"));
  } catch {
    return void 0;
  }
}
function readRootPackageJson(cwd = process.cwd()) {
  return readJsonFile(PATH.resolve(cwd, "package.json"));
}
function detectPackageManagerFromPackageJson(cwd = process.cwd()) {
  const packageManager = readRootPackageJson(cwd)?.packageManager;
  if (!packageManager) return void 0;
  return packageManager.split("@")[0];
}

// src/pm/detectPackageManager.ts
import { existsSync as existsSync2 } from "node:fs";
var knownPackageManagers = /* @__PURE__ */ new Set(["bun", "npm", "pnpm", "yarn"]);
function detectPackageManager() {
  const packageManager = detectPackageManagerFromPackageJson();
  if (packageManager && knownPackageManagers.has(packageManager)) {
    return packageManager;
  }
  if (existsSync2("pnpm-lock.yaml") || existsSync2("pnpm-workspace.yaml")) return "pnpm";
  if (existsSync2("bun.lock") || existsSync2("bun.lockb")) return "bun";
  if (existsSync2("yarn.lock") || existsSync2(".yarnrc.yml")) return "yarn";
  if (existsSync2("package-lock.json") || existsSync2("npm-shrinkwrap.json")) return "npm";
  return "yarn";
}

// src/pm/registry.ts
var implementations = /* @__PURE__ */ new Map();
function getPackageManager(name) {
  const pmName = name ?? detectPackageManager();
  const pm = implementations.get(pmName);
  if (!pm) {
    throw new Error(
      `No package manager implementation registered for "${pmName}". Ensure registerPackageManager() has been called before getPackageManager().`
    );
  }
  return pm;
}

// src/actions/copy-assets.ts
var copyPackageTargetAssets = async (target, name, location) => {
  try {
    const values = await cpy(
      ["**/*.jpg", "**/*.png", "**/*.gif", "**/*.svg", "**/*.webp", "**/*.sass", "**/*.scss", "**/*.gif", "**/*.css"],
      `../dist/${target}`,
      {
        cwd: path.join(process.cwd(), location, "src"),
        flat: false
      }
    );
    for (const value of values) {
      console.log(`${value.split("/").pop()} => ./dist/${target}`);
    }
    return 0;
  } catch (reason) {
    console.log(`Copy Failed: ${name}: ${String(reason)}`);
    return 1;
  }
};
var copyTargetAssets = async (target, pkg) => {
  const workspaces = getPackageManager().listWorkspaces();
  console.log(chalk.green(`Copying Assets [${target.toUpperCase()}]`));
  const workspaceList = workspaces.filter(({ name }) => {
    return pkg === void 0 || name === pkg;
  });
  if (workspaceList.length === 0) {
    console.error(`Package not found [${pkg}]`);
  } else {
    const results = await Promise.all(
      workspaceList.map(async (workspace) => {
        const { location, name } = workspace;
        return await copyPackageTargetAssets(target, name, location);
      })
    );
    return results.reduce((prev, result) => prev || result, 0);
  }
  return 0;
};
var copyAssets = async ({ target, pkg }) => {
  switch (target) {
    case "esm": {
      return await copyTargetAssets("esm", pkg);
    }
    case "cjs": {
      return await copyTargetAssets("cjs", pkg);
    }
    default: {
      return await copyTargetAssets("esm", pkg) || await copyTargetAssets("cjs", pkg);
    }
  }
};
export {
  copyAssets
};
//# sourceMappingURL=copy-assets.mjs.map
