Skip to content

Commit 092f542

Browse files
committed
refactor validators out of build
1 parent 264dd7e commit 092f542

File tree

4 files changed

+58
-70
lines changed

4 files changed

+58
-70
lines changed

src/build/build.js

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@ var fs = require('fs');
33
var process = require('process');
44
var chalk = require('chalk');
55
var Match = require('./matchers');
6-
var Validator = require('./validators');
6+
var validators_1 = require('./validators');
77
function build(lines) {
88
var result = {
9-
project: {
10-
title: '',
11-
description: ''
12-
},
9+
project: {},
1310
chapters: []
14-
}, index = {
11+
};
12+
var index = {
1513
chapter: -1,
1614
page: -1,
1715
task: -1
1816
};
1917
return project(result, lines, index);
2018
}
2119
function project(result, lines, index) {
22-
var matchedAt = null;
20+
result.project = {
21+
title: '',
22+
description: ''
23+
};
2324
for (var i = 0; i < lines.length; i++) {
2425
var line = lines[i];
2526
var projectTitleMatch = Match.project(line);
26-
var chapterStart = Match.chapter(line);
27-
if (projectTitleMatch) {
28-
matchedAt = i;
27+
if (!!projectTitleMatch) {
2928
result.project.title = projectTitleMatch.trim();
3029
}
31-
else if (chapterStart) {
30+
else if (!!Match.chapter(line)) {
3231
return chapter(result, lines.slice(i), index);
3332
}
3433
else {
@@ -38,25 +37,19 @@ function project(result, lines, index) {
3837
return result;
3938
}
4039
function chapter(result, lines, index) {
41-
var matchedAt = null;
40+
index.page = -1;
41+
index.chapter += 1;
42+
result.chapters.push({
43+
title: Match.chapter(lines[0]).trim(),
44+
description: '',
45+
pages: []
46+
});
4247
for (var i = 0; i < lines.length; i++) {
4348
var line = lines[i];
44-
var chapterStart = Match.chapter(line);
45-
var pageStart = Match.page(line);
46-
if (chapterStart && !matchedAt) {
47-
matchedAt = i;
48-
index.page = -1;
49-
index.chapter += 1;
50-
result.chapters.push({
51-
title: chapterStart.trim(),
52-
description: '',
53-
pages: []
54-
});
55-
}
56-
else if (pageStart) {
49+
if (Match.page(line)) {
5750
return page(result, lines.slice(i), index);
5851
}
59-
else if (chapterStart) {
52+
else if (Match.chapter(line) && i > 0) {
6053
return chapter(result, lines.slice(i), index);
6154
}
6255
else {
@@ -78,32 +71,28 @@ function page(result, lines, index) {
7871
var inCodeBlock = false;
7972
for (var i = 1; i < lines.length; i++) {
8073
var line = lines[i];
81-
var pageStart = Match.page(line);
82-
var chapterStart = Match.chapter(line);
83-
var taskStart = Match.task(line);
84-
var codeBlock = Match.codeBlock(line);
85-
if (!!codeBlock) {
74+
if (!!Match.codeBlock(line)) {
8675
inCodeBlock = !inCodeBlock;
8776
}
8877
if (!inCodeBlock) {
89-
if (!hasBreak && Match.isEmpty(lines[i])) {
78+
if (!hasBreak && Match.isEmpty(line)) {
9079
hasBreak = i;
9180
}
92-
else if (!!chapterStart) {
81+
else if (!!Match.chapter(line)) {
9382
return chapter(result, lines.slice(i), index);
9483
}
95-
else if (!!pageStart) {
84+
else if (!!Match.page(line)) {
9685
return page(result, lines.slice(i), index);
9786
}
98-
else if (!!taskStart) {
87+
else if (!!Match.task(line)) {
9988
return task(result, lines.slice(i), index);
10089
}
10190
else {
10291
if (!hasBreak) {
103-
result.chapters[index.chapter].pages[index.page].description += lines[i] + '\n';
92+
result.chapters[index.chapter].pages[index.page].description += line + '\n';
10493
}
10594
else {
106-
result.chapters[index.chapter].pages[index.page].explanation += lines[i] + '\n';
95+
result.chapters[index.chapter].pages[index.page].explanation += line + '\n';
10796
}
10897
}
10998
}
@@ -121,16 +110,11 @@ function task(result, lines, index) {
121110
var inCodeBlock = false;
122111
for (var i = 1; i < lines.length; i++) {
123112
var line = lines[i];
124-
var pageStart = Match.page(line);
125-
var chapterStart = Match.chapter(line);
126-
var taskStart = Match.task(line);
127-
var isAction = Match.taskAction(line);
128-
var codeBlock = Match.codeBlock(line);
129-
if (!!codeBlock) {
113+
if (!!Match.codeBlock(line)) {
130114
inCodeBlock = !inCodeBlock;
131115
}
132116
if (!inCodeBlock) {
133-
if (!!isAction) {
117+
if (!!Match.taskAction(line)) {
134118
var action = line.slice(1).split('(')[0];
135119
var target = /\((.*?)\)$/.exec(line)[1];
136120
switch (action) {
@@ -144,13 +128,13 @@ function task(result, lines, index) {
144128
console.log('Invalid task action');
145129
}
146130
}
147-
else if (!!taskStart) {
131+
else if (!!Match.task(line)) {
148132
return task(result, lines.slice(i), index);
149133
}
150-
else if (!!pageStart) {
134+
else if (!!Match.page(line)) {
151135
return page(result, lines.slice(i), index);
152136
}
153-
else if (!!chapterStart) {
137+
else if (!!Match.chapter(line)) {
154138
return chapter(result, lines.slice(i), index);
155139
}
156140
else {
@@ -193,18 +177,7 @@ module.exports = function (filePath, output) {
193177
}
194178
var lines = fs.readFileSync(filePath, 'utf8').split('\n');
195179
var result = cleanup(build(lines));
196-
if (!Validator.isValidJSON(result)) {
197-
console.log(chalk.red("\n Something went wrong. There seems to be an error in " + filePath + ".\n "));
198-
process.exit(1);
199-
}
200-
var jsonObject = JSON.parse(result);
201-
if (!Validator.hasProjectInfo(jsonObject)) {
202-
console.log(chalk.red("\n Your tutorial is missing basic project information. Check the project title & description.\n "));
203-
process.exit(1);
204-
}
205-
else if (!Validator.hasPage(jsonObject)) {
206-
console.log(chalk.red("\n Your tutorial requires at least one page.\n "));
207-
process.exit(1);
180+
if (validators_1.default(result)) {
181+
fs.writeFileSync(output, result, 'utf8');
208182
}
209-
fs.writeFileSync(output, result, 'utf8');
210183
};

src/build/text-tools.js

Whitespace-only changes.

src/build/validators.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
"use strict";
2+
var chalk = require('chalk');
3+
function validate(text) {
4+
isValidJSON(text);
5+
var jsonObject = JSON.parse(text);
6+
hasProjectInfo(jsonObject);
7+
hasPage(jsonObject);
8+
return true;
9+
}
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
exports.default = validate;
212
function isValidJSON(text) {
3-
if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
13+
if (!/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
414
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
515
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
6-
return true;
7-
}
8-
else {
9-
return false;
16+
console.log(chalk.red("\n Something went wrong. There seems to be an error in " + filePath + ".\n "));
17+
process.exit(1);
1018
}
1119
}
12-
exports.isValidJSON = isValidJSON;
1320
function hasProjectInfo(json) {
1421
var validTitle = json.project.title.length > 0, validDescription = json.project.description.length > 0;
15-
return validTitle && validDescription;
22+
if (!(validTitle && validDescription)) {
23+
console.log(chalk.red("\n Your tutorial is missing basic project information. Check the project title & description.\n "));
24+
process.exit(1);
25+
}
1626
}
17-
exports.hasProjectInfo = hasProjectInfo;
1827
function hasPage(json) {
19-
return (json.chapters[0].pages.length > 0 && !!json.chapters[0].pages[0].title);
28+
if (!(json.chapters[0].pages.length > 0 && !!json.chapters[0].pages[0].title)) {
29+
console.log(chalk.red("\n Your tutorial requires at least one page.\n "));
30+
process.exit(1);
31+
}
2032
}
21-
exports.hasPage = hasPage;

src/tscompiler-fix.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// fix build issue with cli.js requiring a break after node env
2+
var cli = fs.readFileSync('cli.js', 'utf8').split('\n');
3+
var fix = cli.slice(0, 1) + '\n\n' + cli.slice(1);
4+
fs.writeFileSync('cli.js'), fix, 'utf8');

0 commit comments

Comments
 (0)