|
1 | 1 | "use strict"; |
2 | 2 | var fs = require('fs'); |
| 3 | +var chalk = require('chalk'); |
3 | 4 | function line(char, times) { |
4 | 5 | return new RegExp('^' + char + '{' + times + '}(?!#)(.*?)$', 'gm'); |
5 | 6 | } |
@@ -212,21 +213,33 @@ function isValidJSON(text) { |
212 | 213 | return false; |
213 | 214 | } |
214 | 215 | } |
| 216 | +function hasProjectInfo(text) { |
| 217 | + var validTitle = text.project.title.length > 0, validDescription = text.project.description.length > 0; |
| 218 | + return validTitle && validDescription; |
| 219 | +} |
| 220 | +function hasPage(text) { |
| 221 | + return (text.chapters[0].pages.length > 0 && !!text.chapters[0].pages[0].title); |
| 222 | +} |
215 | 223 | module.exports = function (filePath, output) { |
216 | 224 | if (output === void 0) { output = './coderoad.json'; } |
| 225 | + if (!filePath) { |
| 226 | + console.log(chalk.red("\n Pass in a path to your .md file\n For example: coderoad build ./src/tutorial.md\n ")); |
| 227 | + process.exit(1); |
| 228 | + } |
217 | 229 | var lines = fs.readFileSync(filePath, 'utf8').split('\n'); |
218 | 230 | var result = cleanup(build(lines)); |
219 | 231 | if (!isValidJSON(result)) { |
220 | | - console.log('Invalid JSON output'); |
221 | | - process.exit(0); |
| 232 | + console.log(chalk.red("\n Something went wrong. There seems to be an error in " + filePath + ".\n ")); |
| 233 | + process.exit(1); |
222 | 234 | } |
223 | | - else { |
224 | | - try { |
225 | | - fs.writeFileSync(output, result, 'utf8'); |
226 | | - } |
227 | | - catch (e) { |
228 | | - console.log(e); |
229 | | - process.exit(0); |
230 | | - } |
| 235 | + var jsonObject = JSON.parse(result); |
| 236 | + if (!hasProjectInfo(jsonObject)) { |
| 237 | + console.log(chalk.red("\n Your tutorial is missing basic project information. Check the project title & description.\n ")); |
| 238 | + process.exit(1); |
| 239 | + } |
| 240 | + else if (!hasPage(jsonObject)) { |
| 241 | + console.log(chalk.red("\n Your tutorial requires at least one page.\n ")); |
| 242 | + process.exit(1); |
231 | 243 | } |
| 244 | + fs.writeFileSync(output, result, 'utf8'); |
232 | 245 | }; |
0 commit comments