moving data to be held by react query

This commit is contained in:
2024-08-30 09:12:25 -06:00
parent 9d6a3d1199
commit 5d9ece63fa
15 changed files with 185 additions and 175 deletions

View File

@@ -1,27 +1,70 @@
import { promises as fs } from "fs";
import path from "path";
import { LocalCourse } from "@/models/local/localCourse";
import { courseMarkdownLoader } from "./utils/couresMarkdownLoader";
import {
LocalCourse,
LocalCourseSettings,
localCourseYamlUtils,
} from "@/models/local/localCourse";
import { courseMarkdownLoader } from "./utils/courseMarkdownLoader";
import { courseMarkdownSaver } from "./utils/courseMarkdownSaver";
import {
directoryOrFileExists,
hasFileSystemEntries,
} from "./utils/fileSystemUtils";
const basePath = process.env.STORAGE_DIRECTORY ?? "./storage";
console.log("base path", basePath);
export const fileStorageService = {
async saveCourseAsync(
course: LocalCourse,
previouslyStoredCourse?: LocalCourse
) {
await courseMarkdownSaver.save(course, previouslyStoredCourse);
},
// async saveCourseAsync(
// course: LocalCourse,
// previouslyStoredCourse?: LocalCourse
// ) {
// await courseMarkdownSaver.save(course, previouslyStoredCourse);
// },
async loadSavedCourses(): Promise<LocalCourse[]> {
console.log("loading pages from file system");
return (await courseMarkdownLoader.loadSavedCourses()) || [];
// async loadSavedCourses(): Promise<LocalCourse[]> {
// console.log("loading pages from file system");
// return (await courseMarkdownLoader.loadSavedCourses()) || [];
// },
async getCourseNames() {
console.log("loading course ids");
const courseDirectories = await fs.readdir(basePath, {
withFileTypes: true,
});
const coursePromises = courseDirectories
.filter((dirent) => dirent.isDirectory())
.filter(async (dirent) => {
const coursePath = path.join(basePath, dirent.name);
const settingsPath = path.join(coursePath, "settings.yml");
return await directoryOrFileExists(settingsPath);
});
const courseNamesFromDirectories = (await Promise.all(coursePromises)).map(
(c) => c.name
);
return courseNamesFromDirectories;
},
async getCourseSettings(courseName: string): Promise<LocalCourseSettings> {
const courseDirectory = path.join(basePath, courseName);
const settingsPath = path.join(courseDirectory, "settings.yml");
if (!(await directoryOrFileExists(settingsPath))) {
const errorMessage = `Error loading settings for ${courseName}, settings file ${settingsPath}`;
console.log(errorMessage);
throw new Error(errorMessage);
}
const settingsString = await fs.readFile(settingsPath, "utf-8");
const settings = localCourseYamlUtils.parseSettingYaml(settingsString);
const folderName = path.basename(courseDirectory);
return { ...settings, name: folderName };
},
async getEmptyDirectories(): Promise<string[]> {
if (!(await this.directoryExists(basePath))) {
if (!(await directoryOrFileExists(basePath))) {
throw new Error(
`Cannot get empty directories, ${basePath} does not exist`
);
@@ -31,26 +74,8 @@ export const fileStorageService = {
const emptyDirectories = directories
.filter((dirent) => dirent.isDirectory())
.map((dirent) => path.join(basePath, dirent.name))
.filter(async (dir) => !(await this.hasFileSystemEntries(dir)));
.filter(async (dir) => !(await hasFileSystemEntries(dir)));
return emptyDirectories;
},
async directoryExists(directoryPath: string): Promise<boolean> {
try {
await fs.access(directoryPath);
return true;
} catch {
return false;
}
},
async hasFileSystemEntries(directoryPath: string): Promise<boolean> {
try {
const entries = await fs.readdir(directoryPath);
return entries.length > 0;
} catch {
return false;
}
},
};

View File

@@ -18,54 +18,55 @@ import {
} from "@/models/local/quiz/localQuiz";
import { promises as fs } from "fs";
import path from "path";
import { directoryOrFileExists } from "./fileSystemUtils";
const basePath = process.env.STORAGE_DIRECTORY ?? "./storage";
export const courseMarkdownLoader = {
async loadSavedCourses(): Promise<LocalCourse[]> {
const courseDirectories = await fs.readdir(basePath, {
withFileTypes: true,
});
const coursePromises = courseDirectories
.filter((dirent) => dirent.isDirectory())
.map(async (dirent) => {
const coursePath = path.join(basePath, dirent.name);
const settingsPath = path.join(coursePath, "settings.yml");
if (await this.fileExists(settingsPath)) {
return this.loadCourseByPath(coursePath);
}
return null;
});
// async loadSavedCourses(): Promise<LocalCourse[]> {
// const courseDirectories = await fs.readdir(basePath, {
// withFileTypes: true,
// });
// const coursePromises = courseDirectories
// .filter((dirent) => dirent.isDirectory())
// .map(async (dirent) => {
// const coursePath = path.join(basePath, dirent.name);
// const settingsPath = path.join(coursePath, "settings.yml");
// if (await directoryOrFileExists(settingsPath)) {
// return this.loadCourseByPath(coursePath);
// }
// return null;
// });
const courses = (await Promise.all(coursePromises)).filter(
(course) => course !== null
) as LocalCourse[];
return courses.sort((a, b) =>
a.settings.name.localeCompare(b.settings.name)
);
},
// const courses = (await Promise.all(coursePromises)).filter(
// (course) => course !== null
// ) as LocalCourse[];
// return courses.sort((a, b) =>
// a.settings.name.localeCompare(b.settings.name)
// );
// },
async loadCourseByPath(courseDirectory: string): Promise<LocalCourse> {
if (!(await this.directoryExists(courseDirectory))) {
const errorMessage = `Error loading course by name, could not find folder ${courseDirectory}`;
console.log(errorMessage);
throw new Error(errorMessage);
}
// async loadCourseByPath(courseDirectory: string): Promise<LocalCourse> {
// if (!(await directoryOrFileExists(courseDirectory))) {
// const errorMessage = `Error loading course by name, could not find folder ${courseDirectory}`;
// console.log(errorMessage);
// throw new Error(errorMessage);
// }
const settings = await this.loadCourseSettings(courseDirectory);
const modules = await this.loadCourseModules(courseDirectory);
// const settings = await this.loadCourseSettings(courseDirectory);
// const modules = await this.loadCourseModules(courseDirectory);
return {
settings,
modules,
};
},
// return {
// settings,
// modules,
// };
// },
async loadCourseSettings(
courseDirectory: string
): Promise<LocalCourseSettings> {
const settingsPath = path.join(courseDirectory, "settings.yml");
if (!(await this.fileExists(settingsPath))) {
if (!(await directoryOrFileExists(settingsPath))) {
const errorMessage = `Error loading course by name, settings file ${settingsPath}`;
console.log(errorMessage);
throw new Error(errorMessage);
@@ -110,7 +111,7 @@ export const courseMarkdownLoader = {
modulePath: string
): Promise<LocalAssignment[]> {
const assignmentsPath = path.join(modulePath, "assignments");
if (!(await this.directoryExists(assignmentsPath))) {
if (!(await directoryOrFileExists(assignmentsPath))) {
console.log(
`Error loading course by name, assignments folder does not exist in ${modulePath}`
);
@@ -132,7 +133,7 @@ export const courseMarkdownLoader = {
async loadQuizzesFromPath(modulePath: string): Promise<LocalQuiz[]> {
const quizzesPath = path.join(modulePath, "quizzes");
if (!(await this.directoryExists(quizzesPath))) {
if (!(await directoryOrFileExists(quizzesPath))) {
console.log(
`Quizzes folder does not exist in ${modulePath}, creating now`
);
@@ -156,7 +157,7 @@ export const courseMarkdownLoader = {
modulePath: string
): Promise<LocalCoursePage[]> {
const pagesPath = path.join(modulePath, "pages");
if (!(await this.directoryExists(pagesPath))) {
if (!(await directoryOrFileExists(pagesPath))) {
console.log(`Pages folder does not exist in ${modulePath}, creating now`);
await fs.mkdir(pagesPath);
}
@@ -173,22 +174,4 @@ export const courseMarkdownLoader = {
return await Promise.all(pagePromises);
},
async fileExists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
},
async directoryExists(directoryPath: string): Promise<boolean> {
try {
await fs.access(directoryPath);
return true;
} catch {
return false;
}
},
};

View File

@@ -0,0 +1,20 @@
import { promises as fs } from "fs";
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;
}
};