Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions support/src/documentationHelpers/generateGitBookSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@ import fs, { type Dirent } from "fs";
import path from "path";
import process from "process";

type ProgramStructure = {
readonly courses: readonly {
readonly name: string;
readonly location: string;
readonly modules: readonly {
readonly name: string;
readonly location: string;
}[];
}[];
};

const jsonPath = "programStructure.json";
import programStructure from "./programStructure.json" with { type: "json" };
import type { ProgramStructure } from "./programStructure.js";

let rootDir = process.cwd();
let debug = false;
Expand Down Expand Up @@ -88,15 +78,9 @@ const linkLine = (level: number, text: string, filePath: string): string =>

// ---------------- Core ----------------
function generateSummary(structure: ProgramStructure, rootDir: string): string {
if (!structure || !Array.isArray(structure.courses)) {
throw new Error("Invalid structure JSON: expected { courses: [...] }");
}

const lines = [];

for (const course of structure.courses) {
if (!course?.name || !course?.location) continue;

const courseAbs = path.resolve(rootDir, course.location);
const courseRel = toPosixFromRoot(courseAbs);
if (debug)
Expand All @@ -106,10 +90,7 @@ function generateSummary(structure: ProgramStructure, rootDir: string): string {
const courseLink = ensureReadmeLink(courseAbs, courseRel);
lines.push(linkLine(0, course.name, courseLink));

const modules = Array.isArray(course.modules) ? course.modules : [];
for (const module of modules) {
if (!module?.name || !module?.location) continue;

for (const module of course.modules) {
const modAbs = path.resolve(rootDir, module.location);
const modRel = toPosixFromRoot(modAbs);
if (debug)
Expand Down Expand Up @@ -184,9 +165,7 @@ function generateSummary(structure: ProgramStructure, rootDir: string): string {

// ---------------- Run ----------------
try {
const raw = fs.readFileSync(jsonPath, "utf8");
const structure = JSON.parse(raw) as ProgramStructure;
const md = generateSummary(structure, rootDir);
const md = generateSummary(programStructure as ProgramStructure, rootDir);

process.stdout.write(md);
} catch (err: any) {
Expand Down
25 changes: 7 additions & 18 deletions support/src/documentationHelpers/generateLearningGoals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,10 @@ import { fileURLToPath } from "url";
import process from "process";
import updateFileSection from "./updateFileSection.js";

const scriptDir = dirname(fileURLToPath(import.meta.url));

type Course = {
readonly name: string;
readonly location: string;
readonly modules: readonly {
readonly name: string;
readonly location: string;
}[];
};

type ProgramStructure = {
readonly courses: readonly Course[];
};
import programStructure from "./programStructure.json" with { type: "json" };
import type { Course, ProgramStructure } from "./programStructure.js";

const jsonPath = "programStructure.json";
const scriptDir = dirname(fileURLToPath(import.meta.url));

function extractLearningGoals(content: string): {
readonly found: boolean;
Expand Down Expand Up @@ -114,13 +102,14 @@ if (!courseName) {
process.exit(1);
}

const data = JSON.parse(await readFile(jsonPath, "utf-8")) as ProgramStructure;
const course = data.courses.find((c) => c.name === courseName);
const course = (programStructure as ProgramStructure).courses.find(
(c) => c.name === courseName,
);

if (!course) {
console.log(
"Course not found in json. Please provide one of:",
data.courses.map((c) => c.name).join(", "),
programStructure.courses.map((c) => c.name).join(", "),
);
process.exit(1);
}
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions support/src/documentationHelpers/programStructure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Module = {
readonly name: string;
readonly location: string;
};

export type Course = {
readonly name: string;
readonly location: string;
readonly modules: readonly Module[];
};

export type ProgramStructure = {
readonly courses: readonly Course[];
};
1 change: 1 addition & 0 deletions support/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"outDir": "./dist/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"strict": true,
"skipLibCheck": true
}
Expand Down