delete unnecessary lib fns
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Rebecca-Chou 2022-05-22 19:46:11 -04:00
parent 7e11317842
commit 90cc61bc7b
1 changed files with 0 additions and 109 deletions

View File

@ -1,109 +0,0 @@
import fs from "fs/promises";
import path from "path";
import type { Props } from "../pages/about/executives/[year]/[term]";
import { Term, TERMS } from "../utils";
import { getPastTerm, getFutureTerm } from "./events";
import { getMemberImagePath } from "./team";
const PAST_EXECS_PATH = path.join("content", "past-execs");
export interface Exec {
name: string;
role: string;
image: string;
}
export async function getExecsYears(): Promise<string[]> {
return (await fs.readdir(PAST_EXECS_PATH, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.sort();
}
export async function getExecsTermsByYear(year: string): Promise<Term[]> {
return (await fs.readdir(path.join(PAST_EXECS_PATH, year)))
.filter((filename) => filename.endsWith(".json"))
.map((filename) => filename.slice(0, -".json".length) as Term)
.sort((a, b) => TERMS.indexOf(a) - TERMS.indexOf(b));
}
type ExecNameRole = Omit<Exec, "image">;
export async function getExecsByTerm(
year: string,
term: Term
): Promise<Exec[]> {
try {
const file = await fs.readFile(
path.join(PAST_EXECS_PATH, year, `${term}.json`),
"utf-8"
);
return await getExecsWithImages(JSON.parse(file) as ExecNameRole[]);
} catch {
return [];
}
}
export async function getExecsWithImages(execs: ExecNameRole[]) {
return await Promise.all(
execs.map(async (exec) => {
const image = await getMemberImagePath(exec.name);
return {
...exec,
image,
};
})
);
}
export async function getExecsPageProps({
year,
term,
}: {
year: string;
term: Term;
}): Promise<Props> {
const execs = await getExecsByTerm(year, term);
const execsYears = await getExecsYears();
const minYear = execsYears[0];
const pastTerms: { year: string; term: Term }[] = [];
let curPastYear = year;
let curPastTerm = term;
while (parseInt(curPastYear) >= parseInt(minYear) && pastTerms.length < 2) {
const pastTerm = getPastTerm(curPastYear, curPastTerm);
curPastYear = pastTerm.year;
curPastTerm = pastTerm.term;
if ((await getExecsByTerm(curPastYear, curPastTerm)).length !== 0) {
pastTerms.push(pastTerm);
}
}
pastTerms.reverse();
const maxYear = execsYears[execsYears.length - 1];
const futureTerms: { year: string; term: Term }[] = [];
let curFutureYear = year;
let curFutureTerm = term;
while (
parseInt(curFutureYear) <= parseInt(maxYear) &&
futureTerms.length < 2
) {
const futureTerm = getFutureTerm(curFutureYear, curFutureTerm);
curFutureYear = futureTerm.year;
curFutureTerm = futureTerm.term;
if ((await getExecsByTerm(curFutureYear, curFutureTerm)).length !== 0) {
futureTerms.push(futureTerm);
}
}
return {
year: year,
term: term,
execs: execs,
pastTerms: pastTerms,
futureTerms: futureTerms,
};
}