1
- import simpleGit from "simple-git/promise " ;
2
- import yamlParser from "js-yaml " ;
3
- import path from "path " ;
4
- import _ from "lodash " ;
5
- import fs from "fs " ;
1
+ import * as yamlParser from "js-yaml " ;
2
+ import * as path from "path " ;
3
+ import * as _ from "lodash " ;
4
+ import * as fs from "fs " ;
5
+ import * as T from "../typings/tutorial " ;
6
6
// import validate from './validator';
7
7
8
+ // import not working
9
+ const simpleGit = require ( "simple-git/promise" ) ;
10
+
8
11
const workingDir = "tmp" ;
9
12
10
- function parseContent ( md ) {
11
- let start = - 1 ;
12
- const parts = [ ] ;
13
+ type TutorialContent = { } ;
14
+
15
+ function parseContent ( md : string ) : TutorialContent {
16
+ let start : number = - 1 ;
17
+ const parts : any [ ] = [ ] ;
13
18
14
19
const lines = md . split ( "\n" ) ;
15
20
@@ -76,7 +81,7 @@ function parseContent(md) {
76
81
return sections ;
77
82
}
78
83
79
- function rmDir ( dir , rmSelf ) {
84
+ function rmDir ( dir : string , rmSelf = false ) {
80
85
try {
81
86
let files ;
82
87
rmSelf = rmSelf === undefined ? true : rmSelf ;
@@ -89,11 +94,11 @@ function rmDir(dir, rmSelf) {
89
94
}
90
95
91
96
if ( files . length > 0 ) {
92
- files . forEach ( function ( x , i ) {
93
- if ( fs . statSync ( path . join ( dir , x ) ) . isDirectory ( ) ) {
94
- rmDir ( path . join ( dir , x ) ) ;
97
+ files . forEach ( function ( filePath : string ) {
98
+ if ( fs . statSync ( path . join ( dir , filePath ) ) . isDirectory ( ) ) {
99
+ rmDir ( path . join ( dir , filePath ) ) ;
95
100
} else {
96
- fs . unlinkSync ( path . join ( dir , x ) ) ;
101
+ fs . unlinkSync ( path . join ( dir , filePath ) ) ;
97
102
}
98
103
} ) ;
99
104
}
@@ -107,7 +112,7 @@ function rmDir(dir, rmSelf) {
107
112
}
108
113
}
109
114
110
- async function cleanupFiles ( workingDir ) {
115
+ async function cleanupFiles ( workingDir : string ) {
111
116
try {
112
117
const gitModule = simpleGit ( process . cwd ( ) ) ;
113
118
@@ -121,17 +126,18 @@ async function cleanupFiles(workingDir) {
121
126
}
122
127
}
123
128
124
- /**
125
- *
126
- * @param {string } repo Git url to the repo. It should finish with .git
127
- * @param {string } codeBranch The branch containing the tutorial code
128
- * @param {string } setupBranch The branch containing the configuration files
129
- * @param {string } isLocal define if the repo is local or remote
130
- */
131
- async function build ( { repo, codeBranch, setupBranch, isLocal } ) {
132
- let git ;
129
+ export type BuildOptions = {
130
+ repo : string ; // Git url to the repo. It should finish with .git
131
+ codeBranch : string ; // The branch containing the tutorial code
132
+ setupBranch : string ; // The branch containing the tutorialuration files
133
+ isLocal : boolean ; // define if the repo is local or remote
134
+ output : string ;
135
+ } ;
136
+
137
+ async function build ( { repo, codeBranch, setupBranch, isLocal } : BuildOptions ) {
138
+ let git : any ;
133
139
let isSubModule = false ;
134
- let localPath ;
140
+ let localPath : string ;
135
141
136
142
if ( isLocal ) {
137
143
git = simpleGit ( repo ) ;
@@ -154,30 +160,33 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
154
160
155
161
await git . fetch ( ) ;
156
162
157
- // checkout the branch to load configuration and content branch
163
+ // checkout the branch to load tutorialuration and content branch
158
164
await git . checkout ( setupBranch ) ;
159
165
160
166
// Load files
161
167
const _mdContent = fs . readFileSync (
162
168
path . join ( localPath , "TUTORIAL.md" ) ,
163
169
"utf8"
164
170
) ;
165
- let _config = fs . readFileSync ( path . join ( localPath , "coderoad.yaml" ) , "utf8" ) ;
171
+ let _tutorial = fs . readFileSync (
172
+ path . join ( localPath , "coderoad.yaml" ) ,
173
+ "utf8"
174
+ ) ;
166
175
167
176
// Add one more line to the content as per Shawn's request
168
- const mdContent = parseContent ( _mdContent ) ;
177
+ const mdContent : any = parseContent ( _mdContent ) ;
169
178
170
- // Parse config to JSON
171
- const config = yamlParser . load ( _config ) ;
179
+ // Parse tutorial to JSON
180
+ const tutorial : T . Tutorial = yamlParser . load ( _tutorial ) ;
172
181
173
- // Add the summary to the config file
174
- config [ " summary" ] = mdContent . summary ;
182
+ // Add the summary to the tutorial file
183
+ tutorial . summary = mdContent . summary ;
175
184
176
- // merge content and config
177
- config . levels . forEach ( ( level ) => {
185
+ // merge content and tutorial
186
+ tutorial . levels . forEach ( ( level : T . Level ) => {
178
187
const { steps, ...content } = mdContent [ level . id ] ;
179
188
180
- level . steps . forEach ( ( step ) => _ . merge ( step , steps [ step . id ] ) ) ;
189
+ level . steps . forEach ( ( step : T . Step ) => _ . merge ( step , steps [ step . id ] ) ) ;
181
190
182
191
_ . merge ( level , content ) ;
183
192
} ) ;
@@ -200,29 +209,50 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
200
209
// Uses a set to make sure only the latest commit is proccessed
201
210
parts . add ( matches [ 0 ] ) ;
202
211
203
- // Add the content and git hash to the config
212
+ // Add the content and git hash to the tutorial
204
213
if ( matches . groups . stepId ) {
205
214
// If it's a step: add the content and the setup/solution hashes depending on the type
206
- const theStep = config . levels
207
- . find ( ( level ) => level . id === matches . groups . levelId )
208
- . steps . find ( ( step ) => step . id === matches . groups . stepId ) ;
209
-
210
- if ( matches . groups . stepType === "Q" ) {
211
- theStep . setup . commits . push ( commit . hash . substr ( 0 , 7 ) ) ;
212
- } else if (
213
- matches . groups . stepType === "A" &&
214
- theStep . solution . commits
215
- ) {
216
- theStep . solution . commits . push ( commit . hash . substr ( 0 , 7 ) ) ;
215
+ const level : T . Level | null =
216
+ tutorial . levels . find (
217
+ ( level : T . Level ) => level . id === matches . groups . levelId
218
+ ) || null ;
219
+ if ( ! level ) {
220
+ console . log ( `Level ${ matches . groups . levelId } not found` ) ;
221
+ } else {
222
+ const theStep : T . Step | null =
223
+ level . steps . find (
224
+ ( step : T . Step ) => step . id === matches . groups . stepId
225
+ ) || null ;
226
+
227
+ if ( ! theStep ) {
228
+ console . log ( `Step ${ matches . groups . stepId } not found` ) ;
229
+ } else {
230
+ if ( matches . groups . stepType === "Q" ) {
231
+ theStep . setup . commits . push ( commit . hash . substr ( 0 , 7 ) ) ;
232
+ } else if (
233
+ matches . groups . stepType === "A" &&
234
+ theStep . solution &&
235
+ theStep . solution . commits
236
+ ) {
237
+ theStep . solution . commits . push ( commit . hash . substr ( 0 , 7 ) ) ;
238
+ }
239
+ }
217
240
}
218
241
} else {
219
- // If it's level: add the commit hash (if the level has the commit key) and the content to the config
220
- const theLevel = config . levels . find (
221
- ( level ) => level . id === matches . groups . levelId
222
- ) ;
223
-
224
- if ( _ . has ( theLevel , "config.commits" ) ) {
225
- theLevel . setup . commits . push ( commit . hash . substr ( 0 , 7 ) ) ;
242
+ // If it's level: add the commit hash (if the level has the commit key) and the content to the tutorial
243
+ const theLevel : T . Level | null =
244
+ tutorial . levels . find (
245
+ ( level : T . Level ) => level . id === matches . groups . levelId
246
+ ) || null ;
247
+
248
+ if ( ! theLevel ) {
249
+ console . log ( `Level ${ matches . groups . levelId } not found` ) ;
250
+ } else {
251
+ if ( _ . has ( theLevel , "tutorial.commits" ) ) {
252
+ if ( theLevel . setup ) {
253
+ theLevel . setup . commits . push ( commit . hash . substr ( 0 , 7 ) ) ;
254
+ }
255
+ }
226
256
}
227
257
}
228
258
}
@@ -247,14 +277,14 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
247
277
}
248
278
}
249
279
250
- // const isValid = validate(config );
280
+ // const isValid = validate(tutorial );
251
281
252
282
// if (!isValid) {
253
283
// console.log(JSON.stringify(validate.errors, null, 2));
254
284
// return;
255
285
// }
256
286
257
- return config ;
287
+ return tutorial ;
258
288
}
259
289
260
290
export default build ;
0 commit comments