Skip to content

Commit 933bd07

Browse files
committed
feat: have 'generate:learning-goals' update the target file
1 parent d594e9f commit 933bd07

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

courses/foundation/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Total: 16 weeks
2222

2323
## Learning goals overview
2424

25-
<!-- This summary can be automatically generated by running "npm run generate:learning-goals and pasted here -->
25+
<!-- To update this section, use "npm run generate:learning-goals Foundation" -->
26+
<!-- prettier-ignore-start -->
27+
<!-- BEGIN generate:learning-goals -->
2628

2729
### [HTML & CSS](/courses/foundation/html-and-css)
2830

@@ -155,3 +157,6 @@ Total: 16 weeks
155157
- [ ] Managing your own tasks within a tight deadline
156158
- [ ] Understanding how and when to ask for help at the right time
157159
- [ ] Taking a project from idea to completion, including deploying it to the web
160+
161+
<!-- END generate:learning-goals -->
162+
<!-- prettier-ignore-end -->

support/src/documentationHelpers/generateLearningGoals.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,28 @@
1111
* npm run generate:learning-goals <course-name>
1212
*/
1313

14-
import { readFile, writeFile, stat } from "fs/promises";
14+
import { readFile, stat } from "fs/promises";
1515
import { join, dirname } from "path";
1616
import { fileURLToPath } from "url";
1717
import process from "process";
18+
import updateFileSection from "./updateFileSection.js";
1819

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

21-
type ProgramStructure = {
22-
readonly courses: readonly {
22+
type Course = {
23+
readonly name: string;
24+
readonly location: string;
25+
readonly modules: readonly {
2326
readonly name: string;
2427
readonly location: string;
25-
readonly modules: readonly {
26-
readonly name: string;
27-
readonly location: string;
28-
}[];
2928
}[];
3029
};
3130

31+
type ProgramStructure = {
32+
readonly courses: readonly Course[];
33+
};
34+
3235
const jsonPath = "programStructure.json";
33-
const outputLines: string[] = [];
3436

3537
function extractLearningGoals(content: string): {
3638
readonly found: boolean;
@@ -51,16 +53,8 @@ function extractLearningGoals(content: string): {
5153
};
5254
}
5355

54-
async function processCourse(courseName: string): Promise<void> {
55-
const data = JSON.parse(
56-
await readFile(jsonPath, "utf-8"),
57-
) as ProgramStructure;
58-
const course = data.courses.find((c) => c.name === courseName);
59-
60-
if (!course) {
61-
console.log("Course not found in json. Please provide a valid one.");
62-
process.exit(1);
63-
}
56+
async function processCourse(course: Course): Promise<string> {
57+
const outputLines: string[] = [];
6458

6559
for (const module of course.modules) {
6660
outputLines.push(`### [${module.name}](/${module.location})\n`);
@@ -111,7 +105,7 @@ async function processCourse(courseName: string): Promise<void> {
111105
outputLines.push("");
112106
}
113107

114-
console.log(outputLines.join("\n"));
108+
return outputLines.join("\n");
115109
}
116110

117111
const courseName = process.argv.slice(2)[0];
@@ -121,11 +115,30 @@ if (!courseName) {
121115
);
122116
process.exit(1);
123117
}
124-
console.log(
125-
"Here's a summary of the learning goals for the course:",
126-
courseName,
118+
119+
const data = JSON.parse(await readFile(jsonPath, "utf-8")) as ProgramStructure;
120+
const course = data.courses.find((c) => c.name === courseName);
121+
122+
if (!course) {
123+
console.log(
124+
"Course not found in json. Please provide one of:",
125+
data.courses.map((c) => c.name).join(", "),
126+
);
127+
process.exit(1);
128+
}
129+
130+
const markdown = await processCourse(course);
131+
132+
const patt = /|/;
133+
const errorsAndWarnings = markdown.split(/\n/).filter((t) => patt.exec(t));
134+
if (errorsAndWarnings.length > 0) {
135+
console.log(errorsAndWarnings.join("\n"));
136+
}
137+
138+
await updateFileSection(
139+
markdown,
140+
`${course.location}/README.md`,
141+
"generate:learning-goals",
127142
);
128-
console.log("You can copy and paste these into the course Readme.md :-)");
129-
console.log("---------------------------------------------------\n");
130143

131-
processCourse(courseName);
144+
if (errorsAndWarnings.length > 0) process.exit(1);

0 commit comments

Comments
 (0)