#!/usr/bin/env node

// src/bin/package/copy-assets-cjs.ts
import chalk2 from "chalk";

// src/lib/copyAssetFiles.ts
import { copyFileSync, mkdirSync } from "node:fs";
import path from "node:path/posix";
import { globSync } from "glob";
var ASSET_PATTERNS = [
  "**/*.jpg",
  "**/*.png",
  "**/*.gif",
  "**/*.svg",
  "**/*.webp",
  "**/*.sass",
  "**/*.scss",
  "**/*.css"
];
function copyAssetFiles(srcDir, destDir) {
  const matches = globSync(ASSET_PATTERNS, { cwd: srcDir, nodir: true });
  const copied = [];
  for (const relativePath of matches) {
    const from = path.join(srcDir, relativePath);
    const to = path.join(destDir, relativePath);
    mkdirSync(path.dirname(to), { recursive: true });
    copyFileSync(from, to);
    copied.push(to);
  }
  return copied;
}

// src/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/lib/packageName.ts
import { readFileSync } from "node:fs";
import path2 from "node:path";
function packageName() {
  if (process.env.npm_package_name != null && process.env.npm_package_name.length > 0) {
    return process.env.npm_package_name;
  }
  try {
    const pkgPath = path2.join(INIT_CWD(), "package.json");
    const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
    return pkg.name;
  } catch {
    return void 0;
  }
}

// src/actions/package/copy-assets.ts
import path3 from "node:path/posix";
import chalk from "chalk";
var copyTargetAssets = async (target, name, location) => {
  try {
    const srcDir = path3.join(location, "src");
    const destDir = path3.join(srcDir, "..", "dist", target);
    const values = copyAssetFiles(srcDir, destDir);
    if (values.length > 0) {
      console.log(chalk.green(`Copying Assets [${target.toUpperCase()}] - ${name} - ${location}`));
    }
    for (const value of values) {
      console.log(`${value.split("/").pop()} => ./dist/${target}`);
    }
    return 0;
  } catch (ex) {
    const error = ex;
    console.log(`Copy Assets Failed: ${name}: ${error.message}`);
    return 1;
  }
};
var packageCopyAssets = async ({ target }) => {
  const pkg = INIT_CWD();
  const pkgName = packageName() ?? "Unknown";
  switch (target) {
    case "esm": {
      return await copyTargetAssets("esm", pkgName, pkg);
    }
    case "cjs": {
      return await copyTargetAssets("cjs", pkgName, pkg);
    }
    default: {
      return await copyTargetAssets("esm", pkgName, pkg) || await copyTargetAssets("cjs", pkgName, pkg);
    }
  }
};

// src/bin/package/copy-assets-cjs.ts
try {
  process.exitCode = await packageCopyAssets({ target: "cjs" });
} catch (reason) {
  console.error(chalk2.red(reason));
  process.exitCode = 1;
}
//# sourceMappingURL=copy-assets-cjs.mjs.map
