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" ;
1515import { join , dirname } from "path" ;
1616import { fileURLToPath } from "url" ;
1717import process from "process" ;
18+ import updateFileSection from "./updateFileSection.js" ;
1819
1920const 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+
3235const jsonPath = "programStructure.json" ;
33- const outputLines : string [ ] = [ ] ;
3436
3537function 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
117111const 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