From 8310dd16cf128e7ceac33948d9e758a6f4d3f8d8 Mon Sep 17 00:00:00 2001 From: Raimo Radczewski Date: Thu, 28 Oct 2021 20:45:30 +0200 Subject: [PATCH] Use git to order welcome tweets --- scripts/print_welcome_tweets.js | 34 +++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/scripts/print_welcome_tweets.js b/scripts/print_welcome_tweets.js index 59ebb80f7..b36e07e64 100755 --- a/scripts/print_welcome_tweets.js +++ b/scripts/print_welcome_tweets.js @@ -1,5 +1,7 @@ #!/usr/bin/env node require("@js-joda/timezone"); +const util = require("util"); +const exec = util.promisify(require("child_process").exec); const glob = require("glob").sync; const { resolve, basename, dirname } = require("path"); const { readFileSync, statSync } = require("fs"); @@ -28,27 +30,43 @@ const dateFormat = DateTimeFormatter.ofPattern( "eeee 'at' HH:mm ('UTC'x)" ).withLocale(Locale.US); -glob(__dirname + "/../_data/**/.SCHEMA.json") - .sort() - .forEach((schemaFile) => { +const gitCreationDate = async (file) => { + const output = await exec(`git log --format=%at "${file}" | tail -1`); + if (!!output.stderr) throw output.stderr; + return Number(output.stdout.trim()); +}; + +const run = async () => { + const schemas = glob(__dirname + "/../_data/**/.SCHEMA.json"); + for (let schemaFile of schemas) { console.log( "EVENTS FOR", dirname(schemaFile), "\n--------------------------------" ); - const events = glob(dirname(schemaFile) + "/*.json").sort( - (file_1, file_2) => statSync(file_1).mtimeMs - statSync(file_2).mtimeMs - ); + const events = ( + await Promise.all( + glob(dirname(schemaFile) + "/*.json").map(async (event) => { + const creationTime = await gitCreationDate(event); + return { event, creationTime }; + }) + ) + ) + .sort((eventA, eventB) => eventA.creationTime - eventB.creationTime) + .map((e) => e.event); for (let eventFile of events) { const event = JSON.parse(readFileSync(eventFile).toString()); const date = ZonedDateTime.parse(event.date.start); - if(date.isBefore(ZonedDateTime.now())) continue; + if (date.isBefore(ZonedDateTime.now())) continue; const tweet = `🌐 Welcome ${event.title}${toWelcomeTweetModerators( event.moderators )} on ${dateFormat.format(date)} to #gdcr21! ${event.url}`; console.log(tweet); } console.log(""); - }); + } +}; + +run(); \ No newline at end of file