Add script to change date

This commit is contained in:
Aditya Thakral 2021-10-09 20:55:27 -04:00
parent f1749cfa0f
commit 70336f13c6
1 changed files with 57 additions and 0 deletions

57
scripts/change-dates.ts Normal file
View File

@ -0,0 +1,57 @@
import fs from "fs/promises";
import path from "path";
import { format } from "date-fns";
import {
DATE_FORMAT,
getEventsByTerm,
getEventTermsByYear,
getEventYears,
} from "../lib/events";
import {
getNewsByTerm,
getNewsTermsByYear,
getNewsYears,
NEWS_PATH,
} from "../lib/news";
const EVENTS_PATH = path.join("content", "events");
export async function main() {
for (const year of await getEventYears()) {
for (const term of await getEventTermsByYear(year)) {
for (const slug of await getEventsByTerm(year, term)) {
const filePath = path.join(EVENTS_PATH, year, term, `${slug}.md`);
const file = await fs.readFile(filePath, "utf-8");
await fs.writeFile(filePath, replaceDate(file));
}
}
}
for (const year of await getNewsYears()) {
for (const term of await getNewsTermsByYear(year)) {
for (const slug of await getNewsByTerm(year, term)) {
const filePath = path.join(NEWS_PATH, year, term, `${slug}.md`);
const file = await fs.readFile(filePath, "utf-8");
await fs.writeFile(filePath, replaceDate(file));
}
}
}
}
function replaceDate(file: string) {
const lines = file.split("\n");
const dateLineIdx = lines.findIndex((line) => line.startsWith("date: "));
const dateLine = lines[dateLineIdx];
const date = new Date(dateLine.slice("date: ".length + 1, -1));
lines[dateLineIdx] = `date: '${format(date, DATE_FORMAT)}'`;
return lines.join("\n");
}
void main();