import { rmSync, cpSync } from "node:fs";
import execa from "execa";
import chalk from "chalk";

(async () => {
    console.log(
        chalk.italic`Generating Magnetar types, this may take a while...`
    );

    const { failed } = await execa.command(
        "cargo test --release --package magnetar_sdk",
        {
            cwd: "../../magnetar_sdk",
            stdout: "inherit",
        }
    );

    if (failed) {
        console.error(chalk.red`Type generation failed.`);
        return;
    }

    console.log(chalk.green`Successfully generated Magnetar types`);

    const srcPath = "../../magnetar_sdk/bindings";
    const destPath = "magnetar-common/src/types";

    console.log(chalk.italic`Deleting old`, `"${destPath}"`);
    rmSync(destPath, { recursive: true, force: true });
    console.log(chalk.italic`Copying generated Magnetar types...`);
    console.log(
        chalk.italic`From`,
        `"${srcPath}"`,
        chalk.italic`to`,
        `"${destPath}"`
    );
    cpSync(`${srcPath}/`, `${destPath}/`, { recursive: true });
    console.log(chalk.italic`Deleting`, `"${srcPath}"`);
    rmSync(srcPath, { recursive: true, force: true });
})();
