Last active
February 28, 2022 17:39
-
-
Save weiland/9374c98f359d69118976a09a2cda5949 to your computer and use it in GitHub Desktop.
Report working days (wednesdays and fridays) for a month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S deno run --allow-run --quiet | |
// Collect 5 complete days from the last working month for reporting. | |
// Everything this or higher adresses the current month. | |
const MIN_DAY = 27; | |
const WORK_DAYS:Array<number> = [3, 5]; // ['Wendnesday', 'Friday'] | |
const JANUARY = 0; | |
const DECEMBER = 11; | |
const MAX_DAYS = 31; | |
// Settings | |
const MAX_WORKING_DAYS = 5; | |
const excludeCarry = true; | |
const openMail = true; // otherwise the copy-method will be used | |
const { args } = Deno; | |
const printOnly = args?.[0] === '--print'; | |
const today = new Date(); | |
// If less than 27 we are likely at the beginning of a new month. | |
const usePreviousMonth:boolean = today.getDate() < MIN_DAY; | |
const isJanuary:boolean = today.getMonth() === JANUARY; | |
const month = usePreviousMonth ? (isJanuary ? DECEMBER : today.getMonth()) : today.getMonth() + 1; | |
const year:number = (usePreviousMonth && isJanuary) ? today.getFullYear() - 1 : today.getFullYear(); | |
const workdays:Array<string> = []; | |
const isInvalid = (date:Date):boolean => date.toString() === 'Invalid Date'; | |
for(let day = 1; day <= MAX_DAYS; day++) { | |
const date = new Date(`${year}-${month}-${day}`); | |
if (isInvalid(date)) break; // this does not seem to catch in Deno | |
if (date.getDate() != day) break; // instead the month will be increased | |
if (WORK_DAYS.includes(date.getDay())) { | |
workdays.push(date.getDate().toString()); | |
} | |
} | |
const carry = workdays.length - MAX_WORKING_DAYS; | |
if (carry > 0) { | |
workdays[1] = `(${workdays[1]})`; | |
if (excludeCarry) workdays.splice(1, 1); | |
} | |
// TODO(pascal): Following codes just repeats... | |
if (carry > 1) { | |
workdays[workdays.length - 2] = `(${workdays[workdays.length - 2]})`; | |
if (excludeCarry) workdays.splice(workdays.length - 2, 1); | |
} | |
if (carry > 2) { | |
workdays[workdays.length - 3] = `(${workdays[workdays.length - 3]})`; | |
if (excludeCarry) workdays.splice(workdays.length - 3, 1); | |
} | |
if (carry > 3) { | |
workdays[workdays.length - 4] = `(${workdays[workdays.length - 4]})`; | |
if (excludeCarry) workdays.splice(workdays.length - 4, 1); | |
} | |
const translate = (date:Date) => (new Intl.DateTimeFormat('de-DE', { dateStyle: 'full' })).formatToParts(date); | |
const workingMonthName = translate(new Date(`${year}-${month}-01`))[4].value; | |
const greetingDay = today.getHours() <= 11 ? 'Morgen' : (today.getHours() <= 13 ? 'Mittag' : 'Tag'); | |
const signature = ` | |
Viele Grüße | |
Pascal | |
Pronomen: er, ihm | |
`; | |
const buildText = (days:Array<string>) => `Guten ${greetingDay}, | |
hier sind meine Arbeitstage für ${workingMonthName} ${year}: | |
- ${days.join('\r\n- ')} | |
${signature}`; | |
const copy = async (text:string) => { | |
const p = Deno.run({ | |
cmd: ['pbcopy'], | |
stdin: 'piped', | |
}); | |
await p.stdin.write(new TextEncoder().encode(text)); | |
p.stdin.close(); | |
await p.status(); | |
console.debug(text, 'wurde kopiert.') | |
}; | |
console.log('Heute ist', translate(today)[0].value, `der ${today.getDate()}.`); | |
console.log('Workdays:', `(${carry} Tage zu viel)`); | |
const recepients = '[email protected]'; | |
const mailBody = buildText(workdays); | |
if (printOnly) { | |
console.log(mailBody); | |
} else if (openMail) { | |
const mailCommand = Deno.run({ | |
cmd: ['open', '-a', 'Mail', `mailto:${recepients}?subject=Arbeitstage ${workingMonthName}&body=${mailBody}`] | |
}); | |
await mailCommand.status(); | |
} else { | |
await copy(mailBody); | |
} | |
// Deno.exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment