import consola from "consola";
import fs from "fs";
import nodemailer from "nodemailer";


nodemailer.createTestAccount((err, account) => {
    if (err) {
        console.error('Failed to create a testing account. ' + err.message);
        return process.exit(1);
    }
    let apikey = '';
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const charactersLength = characters.length;
    for (var i = 0; i < 64; i++) {
        apikey += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    const env = `
NODE_ENV=dev
MAIL_SERVER=${account.smtp.host}
MAIL_PORT=${account.smtp.port}
MAIL_USER=${account.user}
MAIL_PASSWORD=${account.pass}
MAIL_FROM=${account.user}
API_KEY=${apikey}
`

    try {
        fs.writeFileSync("./.env", env, { encoding: "utf-8" });
        consola.success("Exported ci env to .env");
    } catch (error) {
        consola.error("Couldn't export the ci env");
    }

});