mirror of
https://github.com/tomru/wichteln.git
synced 2026-03-03 06:27:15 +01:00
read from stdin
This commit is contained in:
2
.editorconfig
Normal file
2
.editorconfig
Normal file
@@ -0,0 +1,2 @@
|
||||
[*.js]
|
||||
indent_size = 2
|
||||
51
wichteln.js
51
wichteln.js
@@ -1,13 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const nodemailer = require('nodemailer');
|
||||
const fs = require("fs");
|
||||
const readline = require("readline");
|
||||
const nodemailer = require("nodemailer");
|
||||
|
||||
const mailer = nodemailer.createTransport({
|
||||
sendmail: true,
|
||||
newline: 'unix',
|
||||
path: '/usr/sbin/sendmail'
|
||||
newline: "unix",
|
||||
path: "/usr/sbin/sendmail"
|
||||
});
|
||||
|
||||
function mail(email, name, pick) {
|
||||
@@ -25,47 +25,54 @@ Viel Spaß
|
||||
Thomas
|
||||
|
||||
Ps.: Bitte gebt nicht mehr als 5 Euro pro Geschenk aus!
|
||||
`,
|
||||
}, (err, info) => {
|
||||
`
|
||||
},
|
||||
(err, info) => {
|
||||
console.log(info.envelope);
|
||||
console.log(info.messageId);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function read() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const people = [];
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream('people.txt')
|
||||
input: process.stdin
|
||||
});
|
||||
|
||||
rl.on('line', line => {
|
||||
rl.on("line", line => {
|
||||
const [name, email, group] = line.split(/\s+/);
|
||||
people.push({ name, email, group });
|
||||
});
|
||||
|
||||
rl.on('error', reject);
|
||||
rl.on("error", reject);
|
||||
|
||||
rl.on('close', () => {
|
||||
rl.on("close", () => {
|
||||
resolve(people);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const run = async () => {
|
||||
const people = await read();
|
||||
|
||||
if (!people || !people.length) {
|
||||
throw new Error("input file empty");
|
||||
}
|
||||
|
||||
people.forEach(drawer => {
|
||||
let picked;
|
||||
|
||||
let pickable = people.filter(p =>
|
||||
!p.picked
|
||||
&& drawer.name !== p.name
|
||||
&& (!drawer.group || drawer.group !== p.group)
|
||||
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, noone left for ', drawer.name);
|
||||
throw new Error("ohhh, noone left for ", drawer.name);
|
||||
}
|
||||
|
||||
let pickIndex = Math.floor(Math.random() * pickable.length);
|
||||
@@ -73,10 +80,12 @@ async function run() {
|
||||
pickable[pickIndex].picked = true;
|
||||
});
|
||||
|
||||
|
||||
fs.writeFileSync('./picks.json', JSON.stringify(people.map(({name, pick}) => ({name, pick})), null, 4));
|
||||
fs.writeFileSync(
|
||||
`./picks.txt`,
|
||||
people.map(({ name, pick }) => `${name} picked ${pick}`).join("\n")
|
||||
);
|
||||
|
||||
people.forEach(p => mail(p.email, p.name, p.pick));
|
||||
}
|
||||
};
|
||||
|
||||
run();
|
||||
|
||||
Reference in New Issue
Block a user