@@ -13,15 +13,14 @@ fs.readdirSync(".").forEach(function(file) {
1313 if ( ! match ) return ;
1414 var text = fs . readFileSync ( file , "utf8" ) ;
1515 var exerciseSection = text . indexOf ( "\n== Exercises ==\n" ) ;
16- if ( exerciseSection < 0 ) return ;
1716
1817 var chapter = { number : + chapNum ,
1918 title : text . match ( / \n = ( .* ?) = \n / ) [ 1 ] ,
2019 exercises : [ ] } ;
2120 var includes = text . match ( / \n : l o a d _ f i l e s : ( .* ) / ) ;
2221 if ( includes ) chapter . include = JSON . parse ( includes [ 1 ] ) ;
2322
24- var exerciseBlock = text . slice ( exerciseSection ) ;
23+ var exerciseBlock = exerciseSection ? text . slice ( exerciseSection ) : "" ;
2524 var header = / \n = = = ( .* ?) = = = \n / g;
2625 var num = 1 ;
2726 while ( match = header . exec ( exerciseBlock ) ) {
@@ -59,46 +58,47 @@ fs.readdirSync(".").forEach(function(file) {
5958 ++ num ;
6059 }
6160
62- if ( chapter . exercises . length ) output . push ( chapter ) ;
63- } ) ;
61+ var nodeInfo = "// Node exercises can not be ran in the browser,\n// but you can look at their solution here.\n" ;
62+ if ( chapter . number == 20 ) chapter . exercises = [
63+ { name : "Content negotiation, again" ,
64+ file : "code/solutions/20_1_content_negotiation_again.js" ,
65+ number : 1 ,
66+ type : "js" ,
67+ code : nodeInfo ,
68+ solution : fs . readFileSync ( "code/solutions/20_1_content_negotiation_again.js" , "utf8" )
69+ } ,
70+ { name : "Fixing a leak" ,
71+ file : "code/solutions/20_2_fixing_a_leak.js" ,
72+ number : 2 ,
73+ type : "js" ,
74+ code : nodeInfo ,
75+ solution : fs . readFileSync ( "code/solutions/20_2_fixing_a_leak.js" , "utf8" )
76+ } ,
77+ { name : "Creating directories" ,
78+ file : "code/solutions/20_3_creating_directories.js" ,
79+ number : 3 ,
80+ type : "js" ,
81+ code : nodeInfo ,
82+ solution : fs . readFileSync ( "code/solutions/20_3_creating_directories.js" , "utf8" )
83+ } ,
84+ { name : "A public space on the web" ,
85+ file : "code/solutions/20_4_a_public_space_on_the_web" ,
86+ number : 4 ,
87+ type : "js" ,
88+ code : nodeInfo ,
89+ solution : "// This solutions consists of multiple files. Download it\n// though the link below.\n"
90+ }
91+ ] ;
6492
65- output . push ( { number : 20 , title : "Node.js" , exercises : [
66- { name : "Content negotiation, again" ,
67- file : "code/solutions/20_1_content_negotiation_again.js" ,
68- number : 1 ,
69- type : "js" ,
70- code : "// Node exercises can not be ran in the browser" ,
71- solution : fs . readFileSync ( "code/solutions/20_1_content_negotiation_again.js" , "utf8" )
72- } ,
73- { name : "Fixing a leak" ,
74- file : "code/solutions/20_2_fixing_a_leak.js" ,
75- number : 2 ,
76- type : "js" ,
77- code : "// Node exercises can not be ran in the browser" ,
78- solution : fs . readFileSync ( "code/solutions/20_2_fixing_a_leak.js" , "utf8" )
79- } ,
80- { name : "Creating directories" ,
81- file : "code/solutions/20_3_creating_directories.js" ,
82- number : 3 ,
83- type : "js" ,
84- code : "// Node exercises can not be ran in the browser" ,
85- solution : fs . readFileSync ( "code/solutions/20_3_creating_directories.js" , "utf8" )
86- } ,
87- { name : "A public space on the web" ,
88- file : "code/solutions/20_4_a_public_space_on_the_web" ,
89- number : 4 ,
90- type : "js" ,
91- code : "// Node exercises can not be ran in the browser\n\n// The solution for this exercise can be downloaded from:\n//\n// http://eloquentjavascript.net/code/solutions/20_4_a_public_space_on_the_web.zip" ,
92- solution : "// Node exercises can not be ran in the browser"
93- }
94- ] } ) ;
93+ output . push ( chapter ) ;
94+ } ) ;
9595
9696if ( allSolutions . length ) {
9797 console . error ( "Solution files " + allSolutions + " were not used." ) ;
9898 failed = true ;
9999}
100100
101101if ( ! failed )
102- console . log ( "var exerciseData = " + JSON . stringify ( output , null , 2 ) + ";" ) ;
102+ console . log ( "var chapterData = " + JSON . stringify ( output , null , 2 ) + ";" ) ;
103103else
104104 process . exit ( 1 ) ;
0 commit comments