From 6132aaef3eff41c20539974e2b44893f88b7a95e Mon Sep 17 00:00:00 2001 From: Naomi Carrigan Date: Wed, 8 Oct 2025 10:28:12 -0700 Subject: [PATCH] feat: sort files based on order.json --- .gitignore | 1 + README.md | 5 +++-- data/.gitkeep | 1 + src/index.ts | 20 +++++++++++++++++++- tsconfig.json | 5 ++++- 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 data/.gitkeep diff --git a/.gitignore b/.gitignore index e457964..4c87b7e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ prod node_modules review.md content/**/* +data/**/* !.gitkeep fcc-review-pages.pdf \ No newline at end of file diff --git a/README.md b/README.md index f248e7f..a0082f2 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ This tool allows you to compile all of freeCodeCamp's review lessons into a sing 1. Clone the repo, `pnpm install`. 2. Go to the freeCodeCamp repo, copy all of the review blocks into the `content` directory here. Nested directories are fine, we walk the file tree recursively. Only markdown files will be parsed. -3. Run `pnpm start` to start reading the files, compiling them into a single doc, and turning that doc into a PDF. -4. Profit. +3. Go to the freeCodeCamp repo, copy the Full Stack order in `/curriculum/structure/superblocks/fullstack.json` to the `data/order.json` here. +4. Run `pnpm start` to start reading the files, compiling them into a single doc, and turning that doc into a PDF. +5. Profit. ## Feedback and Bugs diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..2c95ac0 --- /dev/null +++ b/data/.gitkeep @@ -0,0 +1 @@ +This is where the superblock order goes. \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 3299d82..7e096c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,9 +13,16 @@ import { } from "node:fs/promises"; import { join } from "node:path"; import { mdToPdf } from "md-to-pdf"; +import order from "../data/order.json" assert { type: "json" }; import { options } from "./config/options.js"; import { starterText } from "./config/text.js"; +const flattenedOrder: Array = order.chapters.flatMap((chapter) => { + return chapter.modules.flatMap((module) => { + return module.blocks; + }); +}); + const readDirectoryRecursively = async( directory: string, ): Promise> => { @@ -31,6 +38,14 @@ const readDirectoryRecursively = async( return files.flat(); }; +const sortFiles = (a: string, b: string): number => { + const aDashedName = a.split("/").at(1); + const bDashedName = b.split("/").at(1); + const aIndex = flattenedOrder.indexOf(aDashedName ?? ""); + const bIndex = flattenedOrder.indexOf(bDashedName ?? ""); + return aIndex - bIndex; +}; + const rollupFiles = async( inputDirectory: string, outputFile: string, @@ -39,7 +54,10 @@ const rollupFiles = async( console.log("Process started..."); await writeFile(outputFile, `${starterText}\n`); console.log("Reading content directory..."); - const files = await readDirectoryRecursively(inputDirectory); + const unsortedFiles = await readDirectoryRecursively(inputDirectory); + console.log(unsortedFiles); + const files = unsortedFiles.toSorted(sortFiles); + console.log("Files found, processing..."); for (const file of files) { if (file === ".gitkeep") { continue; diff --git a/tsconfig.json b/tsconfig.json index 58b6fc3..b7be854 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,9 @@ "extends": "@nhcarrigan/typescript-config", "compilerOptions": { "outDir": "./prod", - "rootDir": "./src" + "rootDir": "./src", + "resolveJsonModule": true, + "module": "esnext", + "lib": ["es2024"] }, } \ No newline at end of file