mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 23:58:31 -06:00
some more tests
This commit is contained in:
@@ -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);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user