mirror of
https://github.com/tomru/wichteln.git
synced 2026-03-03 14:37:16 +01:00
96 lines
2.1 KiB
JavaScript
Executable File
96 lines
2.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const fs = require("fs");
|
|
const readline = require("readline");
|
|
const nodemailer = require("nodemailer");
|
|
|
|
const mailer = nodemailer.createTransport({
|
|
sendmail: true,
|
|
newline: "unix",
|
|
path: "/usr/sbin/sendmail"
|
|
});
|
|
|
|
function mail(email, name, pick) {
|
|
mailer.sendMail(
|
|
{
|
|
from: "Wichtelbot <tomru@uber.space>",
|
|
to: email,
|
|
subject: `Dein Wichtel...`,
|
|
text: `Hey ${name},
|
|
|
|
ich hab dieses Jahr für Dich >> ${pick} << gezogen!
|
|
|
|
Viel Spaß beim Wichteln!
|
|
Dein *ai freier* Weihnachtswichtel™
|
|
`
|
|
},
|
|
(err, info) => {
|
|
console.log(info.envelope);
|
|
console.log(info.messageId);
|
|
}
|
|
);
|
|
}
|
|
|
|
function read() {
|
|
return new Promise((resolve, reject) => {
|
|
const people = [];
|
|
const rl = readline.createInterface({
|
|
input: process.stdin
|
|
});
|
|
|
|
rl.on("line", line => {
|
|
const [name, email, group] = line.split(/\s+/);
|
|
people.push({ name, email, group });
|
|
});
|
|
|
|
rl.on("error", reject);
|
|
|
|
rl.on("close", () => {
|
|
resolve(people);
|
|
});
|
|
});
|
|
}
|
|
|
|
const run = async () => {
|
|
const people = await read();
|
|
const send = process.argv[2] === "--send";
|
|
|
|
if (!people || !people.length) {
|
|
throw new Error("input file empty");
|
|
}
|
|
|
|
people.forEach((drawer, index) => {
|
|
let picked;
|
|
|
|
let pickable = people.filter(
|
|
p =>
|
|
!p.picked &&
|
|
drawer.name !== p.name &&
|
|
(!drawer.group || drawer.group !== p.group)
|
|
);
|
|
|
|
if (pickable.length === 0) {
|
|
throw new Error(`ohhh shoot. None left for ${drawer.name}. Just try to roll again...`);
|
|
}
|
|
|
|
let pickIndex = Math.floor(Math.random() * pickable.length);
|
|
drawer.pick = pickable[pickIndex].name;
|
|
pickable[pickIndex].picked = true;
|
|
});
|
|
|
|
console.log('Yay, I could draw someone for everybody! Writing ./picks.txt');
|
|
|
|
fs.writeFileSync(
|
|
`./picks.txt`,
|
|
people.map(({ name, pick }) => `${name} picked ${pick}`).join("\n")
|
|
);
|
|
|
|
if (!send) {
|
|
console.log("DRY RUN: did not send emails! To send pass --send option");
|
|
return;
|
|
}
|
|
people.forEach(p => mail(p.email, p.name, p.pick));
|
|
};
|
|
|
|
run().catch((e) => console.error(`Error: ${e.message}`));
|