#!/usr/bin/env node
/**
 * Post-install script: creates symlinks for workspace packages
 * so they resolve as @bobfrankston/mailx-* in node_modules.
 */
import fs from "node:fs";
import path from "node:path";
// Fail the install loudly on an unsupported Node. Floor is 24.2.0:
//   - node:sqlite (DatabaseSync) is only usable flag-free from Node 24, and
//     rmfmail launches plain `node` with no --experimental-sqlite flag;
//   - import.meta.main was added in Node 24.2.0.
// `engines` only warns; this hard-stops the install.
{
    const [maj, min] = process.versions.node.split(".").map(Number);
    if (maj < 24 || (maj === 24 && min < 2)) {
        console.error(`rmfmail requires Node.js >= 24.2.0 (found ${process.version}). Upgrade Node and re-run the install.`);
        process.exit(1);
    }
}
const root = path.resolve(import.meta.dirname, "..");
const packagesDir = path.join(root, "packages");
const nmDir = path.join(root, "node_modules", "@bobfrankston");
if (!fs.existsSync(packagesDir))
    process.exit(0); // not in workspace layout
fs.mkdirSync(nmDir, { recursive: true });
for (const dir of fs.readdirSync(packagesDir)) {
    const pkgPath = path.join(packagesDir, dir, "package.json");
    if (!fs.existsSync(pkgPath))
        continue;
    const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
    const name = pkg.name?.split("/")[1]; // e.g., "mailx-store" from "@bobfrankston/mailx-store"
    if (!name)
        continue;
    const linkPath = path.join(nmDir, name);
    const targetPath = path.join(packagesDir, dir);
    if (fs.existsSync(linkPath))
        continue; // already linked
    try {
        // Use junction on Windows (no admin needed), symlink on Unix
        if (process.platform === "win32") {
            fs.symlinkSync(targetPath, linkPath, "junction");
        }
        else {
            fs.symlinkSync(targetPath, linkPath, "dir");
        }
    }
    catch (e) {
        console.error(`Failed to link ${name}: ${e.message}`);
    }
}
//# sourceMappingURL=postinstall.js.map