Created
August 12, 2024 20:56
-
-
Save zacjones93/341860d461d7aa5a3361e2807aeb5910 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: er-prepare-module-titles-and-code | |
import "@johnlindquist/kit" | |
import { readdir, readFile, stat } from "node:fs/promises"; | |
let dir | |
// dir = "/Users/zacjones/Documents/01.Projects/Epic-React-v2/repos/react-fundamentals/exercises" | |
if (!dir) dir = await getSelectedDir() | |
if (!dir) dir = await drop("Select a Epic React v2 exercise directory") | |
console.log(await readdir(dir)) | |
const getTitleFromMarkdownFile = (readme: string) => { | |
let h1Match = readme.match(/^# (.+)$/m) | |
let h1Title = h1Match ? h1Match[1] : 'No title found' | |
return h1Title | |
} | |
let sectionItems = await readdir(dir) | |
async function isDirectory(path: string) { | |
try { | |
const stats = await stat(path); | |
return stats.isDirectory(); | |
} catch (error) { | |
console.warn(`Error checking if path is directory: ${path}`, error); | |
return false; | |
} | |
} | |
async function processModuleFiles(dir: string) { | |
let exercises = await readdir(dir) | |
let result = {} | |
for (let exercise of exercises) { | |
let exercisePath = dir + "/" + exercise | |
if (exercise === "README.mdx") { | |
let exerciseReadme = await readFile(exercisePath, "utf-8") | |
console.log("Exercise title:", getTitleFromMarkdownFile(exerciseReadme)) | |
} | |
if (exercise === "README.mdx" || exercise === "FINISHED.mdx") { | |
continue | |
} else { | |
let exerciseReadme = await readFile(exercisePath + "/README.mdx", "utf-8") | |
let exerciseTitle = getTitleFromMarkdownFile(exerciseReadme) | |
if(!result.hasOwnProperty(exerciseTitle)) { | |
result = {...result, [exerciseTitle]: {}} | |
} | |
// console.log("===", {exercise}) | |
let lessons = await readdir(exercisePath) | |
for (let lesson of lessons) { | |
let lessonPath = exercisePath + "/" + lesson | |
if (lesson === "README.mdx") { | |
let lessonReadme = await readFile(lessonPath, "utf-8") | |
console.log("lesson title:", getTitleFromMarkdownFile(lessonReadme)) | |
} | |
if (lesson === "FINISHED.mdx" || lesson === "README.mdx") { | |
continue | |
} else { | |
let lessonTitle = getTitleFromMarkdownFile(await readFile(lessonPath + "/README.mdx", "utf-8")) | |
if(!result[exerciseTitle].hasOwnProperty(lessonTitle)) { | |
result[exerciseTitle] = {...result[exerciseTitle], [lessonTitle]: []} | |
} | |
// console.log("===", "===", {lesson}) | |
let lessonParts = await readdir(lessonPath) | |
for (let lessonPart of lessonParts) { | |
if (lessonPart === "README.mdx") { | |
let lessonPartPath = lessonPath + "/" + lessonPart | |
let lessonPartTitle = getTitleFromMarkdownFile(await readFile(lessonPartPath, "utf-8")) | |
let exerciseNumber = exercise.split(".")[0] | |
let [lessonNumber, lessonPartType] = lesson.split(".") | |
// console.log("===", "===", "===", {lessonPart}) | |
result[exerciseTitle][lessonTitle].push({ | |
identifier: `${exerciseNumber}-${lessonNumber}`, | |
code: `/${exerciseNumber}/${lessonNumber}/${lessonPartType}`, | |
type: lessonPartType === "problem" ? "exercise" : "solution", | |
title: lessonTitle | |
}) | |
} | |
} | |
} | |
} | |
} | |
} | |
return result | |
} | |
// Call the function | |
const moduleObject = await processModuleFiles(dir); | |
console.log(JSON.stringify(moduleObject, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment