some more tests

This commit is contained in:
2024-09-27 08:30:37 -06:00
parent 0cbf88de3e
commit 1112b646f3
5 changed files with 127 additions and 40 deletions

View File

@@ -7,32 +7,53 @@ import path from "path";
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
import { promises as fs } from "fs";
export const quizFileStorageService = {
async getQuizNames(courseName: string, moduleName: string) {
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
if (!(await directoryOrFileExists(filePath))) {
console.log(
`Error loading course by name, quiz folder does not exist in ${filePath}`
);
await fs.mkdir(filePath);
}
const getQuizNames = async (courseName: string, moduleName: string) => {
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
if (!(await directoryOrFileExists(filePath))) {
console.log(
`Error loading course by name, quiz folder does not exist in ${filePath}`
);
await fs.mkdir(filePath);
}
const files = await fs.readdir(filePath);
return files.map((f) => f.replace(/\.md$/, ""));
},
async getQuiz(courseName: string, moduleName: string, quizName: string) {
const filePath = path.join(
basePath,
courseName,
moduleName,
"quizzes",
quizName + ".md"
);
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
/\r\n/g,
"\n"
);
return localQuizMarkdownUtils.parseMarkdown(rawFile);
const files = await fs.readdir(filePath);
return files.map((f) => f.replace(/\.md$/, ""));
};
const getQuiz = async (
courseName: string,
moduleName: string,
quizName: string
) => {
const filePath = path.join(
basePath,
courseName,
moduleName,
"quizzes",
quizName + ".md"
);
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(/\r\n/g, "\n");
return localQuizMarkdownUtils.parseMarkdown(rawFile);
};
export const quizFileStorageService = {
getQuizNames,
getQuiz,
async getQuizzes(courseName: string, moduleName: string) {
const fileNames = await getQuizNames(courseName, moduleName);
const quizzes = (
await Promise.all(
fileNames.map(async (name) => {
try {
return await getQuiz(courseName, moduleName, name);
} catch {
return null;
}
})
)
).filter((a) => a !== null);
return quizzes;
},
async updateQuiz(
@@ -72,6 +93,6 @@ export const quizFileStorageService = {
quizName + ".md"
);
console.log("removing quiz", filePath);
await fs.unlink(filePath)
}
await fs.unlink(filePath);
},
};