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
|
||||||
111
wichteln.js
111
wichteln.js
@@ -1,13 +1,13 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require("fs");
|
||||||
const readline = require('readline');
|
const readline = require("readline");
|
||||||
const nodemailer = require('nodemailer');
|
const nodemailer = require("nodemailer");
|
||||||
|
|
||||||
const mailer = nodemailer.createTransport({
|
const mailer = nodemailer.createTransport({
|
||||||
sendmail: true,
|
sendmail: true,
|
||||||
newline: 'unix',
|
newline: "unix",
|
||||||
path: '/usr/sbin/sendmail'
|
path: "/usr/sbin/sendmail"
|
||||||
});
|
});
|
||||||
|
|
||||||
function mail(email, name, pick) {
|
function mail(email, name, pick) {
|
||||||
@@ -25,58 +25,67 @@ Viel Spaß
|
|||||||
Thomas
|
Thomas
|
||||||
|
|
||||||
Ps.: Bitte gebt nicht mehr als 5 Euro pro Geschenk aus!
|
Ps.: Bitte gebt nicht mehr als 5 Euro pro Geschenk aus!
|
||||||
`,
|
`
|
||||||
}, (err, info) => {
|
},
|
||||||
console.log(info.envelope);
|
(err, info) => {
|
||||||
console.log(info.messageId);
|
console.log(info.envelope);
|
||||||
});
|
console.log(info.messageId);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function read() {
|
function read() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const people = [];
|
const people = [];
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: fs.createReadStream('people.txt')
|
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function run() {
|
|
||||||
const people = await read();
|
|
||||||
|
|
||||||
people.forEach(drawer => {
|
|
||||||
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, noone left for ', drawer.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
let pickIndex = Math.floor(Math.random() * pickable.length);
|
|
||||||
drawer.pick = pickable[pickIndex].name;
|
|
||||||
pickable[pickIndex].picked = true;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
rl.on("line", line => {
|
||||||
|
const [name, email, group] = line.split(/\s+/);
|
||||||
|
people.push({ name, email, group });
|
||||||
|
});
|
||||||
|
|
||||||
fs.writeFileSync('./picks.json', JSON.stringify(people.map(({name, pick}) => ({name, pick})), null, 4));
|
rl.on("error", reject);
|
||||||
|
|
||||||
people.forEach(p => mail(p.email, p.name, p.pick));
|
rl.on("close", () => {
|
||||||
|
resolve(people);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (pickable.length === 0) {
|
||||||
|
throw new Error("ohhh, noone left for ", drawer.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
let pickIndex = Math.floor(Math.random() * pickable.length);
|
||||||
|
drawer.pick = pickable[pickIndex].name;
|
||||||
|
pickable[pickIndex].picked = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
`./picks.txt`,
|
||||||
|
people.map(({ name, pick }) => `${name} picked ${pick}`).join("\n")
|
||||||
|
);
|
||||||
|
|
||||||
|
people.forEach(p => mail(p.email, p.name, p.pick));
|
||||||
|
};
|
||||||
|
|
||||||
run();
|
run();
|
||||||
|
|||||||
Reference in New Issue
Block a user