moving v2 to top level

This commit is contained in:
2024-12-17 09:19:21 -07:00
parent 5f0b3554dc
commit 576ee02afb
468 changed files with 79 additions and 15430 deletions

View File

@@ -0,0 +1,52 @@
import { promises as fs } from "fs";
import path from "path";
export const hasFileSystemEntries = async (
directoryPath: string
): Promise<boolean> => {
try {
const entries = await fs.readdir(directoryPath);
return entries.length > 0;
} catch {
return false;
}
};
export const directoryOrFileExists = async (directoryPath: string): Promise<boolean> => {
try {
await fs.access(directoryPath);
return true;
} catch {
return false;
}
};
export async function getCourseNames() {
console.log("loading course ids");
const courseDirectories = await fs.readdir(basePath, {
withFileTypes: true,
});
const coursePromises = await Promise.all(
courseDirectories
.filter((dirent) => dirent.isDirectory())
.map(async (dirent) => {
const coursePath = path.join(basePath, dirent.name);
const settingsPath = path.join(coursePath, "settings.yml");
const hasSettings = await directoryOrFileExists(settingsPath);
return {
dirent,
hasSettings,
};
})
);
const courseNamesFromDirectories = coursePromises
.filter(({ hasSettings }) => hasSettings)
.map(({ dirent }) => dirent.name);
return courseNamesFromDirectories;
}
export const basePath = process.env.STORAGE_DIRECTORY ?? "./storage";
console.log("base path", basePath);

View File

@@ -0,0 +1,48 @@
import { getWeekNumber } from "@/app/course/[courseName]/calendar/calendarMonthUtils";
import { extractLabelValue } from "@/models/local/assignment/utils/markdownUtils";
import { Lecture } from "@/models/local/lecture";
import { getDateFromStringOrThrow } from "@/models/local/utils/timeUtils";
export function parseLecture(fileContent: string): Lecture {
try {
const settings = fileContent.split("---\n")[0];
const name = extractLabelValue(settings, "Name");
const date = extractLabelValue(settings, "Date");
const content = fileContent.split("---\n").slice(1).join("---\n").trim();
return {
name,
date,
content,
};
} catch (error) {
console.error("Error parsing lecture: ", fileContent);
throw error;
}
}
export function lectureToString(lecture: Lecture) {
return `Name: ${lecture.name}
Date: ${lecture.date}
---
${lecture.content}`;
}
export const lectureFolderName = "00 - lectures";
export function getLectureWeekName(semesterStart: string, lectureDate: string) {
const startDate = getDateFromStringOrThrow(
semesterStart,
"semester start date in update lecture"
);
const targetDate = getDateFromStringOrThrow(
lectureDate,
"lecture start date in update lecture"
);
const weekNumber = getWeekNumber(startDate, targetDate)
.toString()
.padStart(2, "0");
const weekName = `week-${weekNumber}`;
return weekName;
}