#!/usr/bin/env node
/**
 * copy-docs.js — pre-pack hook for mailx-settings.
 *
 * The reference docs live at `<workspace>/app/docs/*.md` so they're easy
 * to edit and discover in the source tree. The published npm package
 * needs them as a sibling of index.js so `deployDocs()` can find them at
 * runtime via `__dirname/docs/`. This script copies the workspace docs
 * into a local `./docs/` directory just before npm publish, then npm's
 * `files` field includes them in the tarball.
 *
 * Idempotent — safe to run multiple times. Source path is the workspace
 * root resolved upward from this package.
 */
import fs from "node:fs";
import path from "node:path";

const __dirname = import.meta.dirname;
const src = path.join(__dirname, "..", "..", "docs");
const dst = path.join(__dirname, "docs");

if (!fs.existsSync(src)) {
    console.log(`[mailx-settings] no source docs at ${src} — skipping copy`);
    process.exit(0);
}
fs.mkdirSync(dst, { recursive: true });
const files = fs.readdirSync(src).filter(f => f.endsWith(".md"));
for (const f of files) {
    fs.copyFileSync(path.join(src, f), path.join(dst, f));
}
console.log(`[mailx-settings] copied ${files.length} .md file(s) from app/docs/ → docs/`);
