import fsp from 'node:fs/promises';
import fs from 'node:fs';
import path from 'node:path';
import { paths } from '../load-config.js';
import { c as chalk } from '../chunks/index.js';
import 'node:url';
import 'node:process';
import 'node:os';
import 'node:tty';

/**
 * vite :: src/cli/copy.mjs.
 *
 * Vite-based build/dev tooling for Vettvangur projects.
 *
 * These docs are generated from inline JSDoc in the repo and are intended for contributors.
 *
 * @generated
 * @module vite
 */

const /**
   * Pad.
   *
   * @param n - Value.
   * @param value - Value.
   * @returns Nothing.
   *
   * @generated
   * @example
   * // vite (src/tools/javascript/vite/src/cli/copy.mjs)
   * // import { pad } from '@vettvangur/vite'
   *
   * pad(n, value)
   */
  pad = (n, w = 2) => String(n).padStart(w, '0');
const /**
   * Ts.
   *
   * @returns String result.
   *
   * @generated
   * @example
   * // vite (src/tools/javascript/vite/src/cli/copy.mjs)
   * // import { ts } from '@vettvangur/vite'
   *
   * ts()
   */
  ts = () => {
    const d = new Date();
    return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}.${pad(d.getMilliseconds(), 3)}`
  };
const /**
   * Vite Tag.
   *
   * @generated
   * @example
   * console.log(viteTag)
   */
  viteTag = chalk.cyan('[assets]');
const /**
   * T.
   *
   * @returns Nothing.
   *
   * @generated
   * @example
   * // vite (src/tools/javascript/vite/src/cli/copy.mjs)
   * // import { t } from '@vettvangur/vite'
   *
   * t()
   */
  t = () => chalk.dim(`[${ts()}]`);

/**
 * Copy assets.
 *
 * @returns Nothing.
 *
 * @example
 * // vite (src/tools/javascript/vite/src/cli/copy.mjs)
 * // import { copyAssets } from '@vettvangur/vite'
 *
 * await copyAssets()
 */
async function copyAssets() {
  const src = paths.assets;
  const dest = paths.dest;

  if (!fs.existsSync(src)) {
    console.log(`${t()} ${viteTag} assets source missing, skipping: ${src}`);

    return
  }

  await ensureDir(dest);

  console.log(`${t()} ${viteTag} copying assets...`);

  const entries = await fsp.readdir(src, { withFileTypes: true });
  await Promise.all(
    entries.map(async (e) => {
      const from = path.join(src, e.name);
      const to = path.join(dest, e.name);
      if (e.isDirectory()) {
        await copyDir(from, to);
      } else if (e.isFile()) {
        await ensureDir(path.dirname(to));
        await fsp.copyFile(from, to);
      }
    })
  );

  console.log(`${t()} ${viteTag} assets successfully copied`);
}

/**
 * Ensure a directory exists.
 * @param {string} p - Directory path.
 */
async function ensureDir(p) {
  await fsp.mkdir(p, { recursive: true });
}

/**
 * Recursively copy a directory.
 * @param {string} src - Source directory.
 * @param {string} dest - Destination directory.
 */
async function copyDir(src, dest) {
  await ensureDir(dest);
  const entries = await fsp.readdir(src, { withFileTypes: true });
  await Promise.all(
    entries.map(async (e) => {
      const from = path.join(src, e.name);
      const to = path.join(dest, e.name);
      if (e.isDirectory()) {
        await copyDir(from, to);
      } else if (e.isFile()) {
        await fsp.copyFile(from, to);
      }
    })
  );
}

export { copyAssets as default };
//# sourceMappingURL=copy.js.map
