Skip to content

Commit e756fb3

Browse files
committed
allow action arrays in build, trim output
1 parent 33cf62a commit e756fb3

File tree

5 files changed

+103
-38
lines changed

5 files changed

+103
-38
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Command line interface for CodeRoad. Coming soon.
1515
### Roadmap
1616

1717
* create
18-
* publish
19-
* find
18+
* publish [coderoad-name]
19+
* search [query]
20+
* list
2021
* run

src/build/actions.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
var cleanup_1 = require('./cleanup');
3+
function addAction(result, line, index) {
4+
var action = line.slice(1).split('(')[0];
5+
var target = cleanup_1.trimQuotes(/\((.*?)\)$/.exec(line)[1]);
6+
var task = result.chapters[index.chapter].pages[index.page].tasks[index.task];
7+
switch (action) {
8+
case 'test':
9+
if (task.tests === undefined) {
10+
result.chapters[index.chapter].pages[index.page].tasks[index.task].tests = [];
11+
}
12+
result.chapters[index.chapter].pages[index.page].tasks[index.task].tests.push(target);
13+
break;
14+
case 'action':
15+
if (task.actions === undefined) {
16+
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions = [];
17+
}
18+
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(target);
19+
break;
20+
case 'hint':
21+
if (task.hints === undefined) {
22+
result.chapters[index.chapter].pages[index.page].tasks[index.task].hints = [];
23+
}
24+
result.chapters[index.chapter].pages[index.page].tasks[index.task].hints.push(target);
25+
break;
26+
default:
27+
console.log('Invalid task action');
28+
}
29+
return result;
30+
}
31+
Object.defineProperty(exports, "__esModule", { value: true });
32+
exports.default = addAction;

src/build/build.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var process = require('process');
44
var chalk = require('chalk');
55
var Match = require('./matchers');
66
var validators_1 = require('./validators');
7+
var actions_1 = require('./actions');
78
var cleanup_1 = require('./cleanup');
89
function build(lines) {
910
var result = {
@@ -41,19 +42,23 @@ function chapter(result, lines, index) {
4142
index.page = -1;
4243
index.chapter += 1;
4344
result.chapters.push({
44-
title: Match.chapter(lines[0]).trim(),
45-
description: '',
46-
pages: []
45+
title: Match.chapter(lines[0]).trim()
4746
});
4847
for (var i = 0; i < lines.length; i++) {
4948
var line = lines[i];
5049
if (Match.page(line)) {
50+
if (result.chapters[index.chapter].pages === undefined) {
51+
result.chapters[index.chapter].pages = [];
52+
}
5153
return page(result, lines.slice(i), index);
5254
}
5355
else if (Match.chapter(line) && i > 0) {
5456
return chapter(result, lines.slice(i), index);
5557
}
5658
else {
59+
if (result.chapters[index.chapter].description === undefined) {
60+
result.chapters[index.chapter].description = '';
61+
}
5762
result.chapters[index.chapter].description += line + '\n';
5863
}
5964
}
@@ -64,10 +69,7 @@ function page(result, lines, index) {
6469
index.page += 1;
6570
index.task = -1;
6671
result.chapters[index.chapter].pages.push({
67-
title: Match.page(lines[0]).trim(),
68-
description: '',
69-
explanation: '',
70-
tasks: []
72+
title: Match.page(lines[0]).trim()
7173
});
7274
var inCodeBlock = false;
7375
for (var i = 1; i < lines.length; i++) {
@@ -86,13 +88,22 @@ function page(result, lines, index) {
8688
return page(result, lines.slice(i), index);
8789
}
8890
else if (!!Match.task(line)) {
91+
if (result.chapters[index.chapter].pages[index.page].tasks === undefined) {
92+
result.chapters[index.chapter].pages[index.page].tasks = [];
93+
}
8994
return task(result, lines.slice(i), index);
9095
}
9196
else {
9297
if (!hasBreak) {
98+
if (result.chapters[index.chapter].pages[index.page].description === undefined) {
99+
result.chapters[index.chapter].pages[index.page].description = '';
100+
}
93101
result.chapters[index.chapter].pages[index.page].description += line + '\n';
94102
}
95103
else {
104+
if (result.chapters[index.chapter].pages[index.page].explanation === undefined) {
105+
result.chapters[index.chapter].pages[index.page].explanation = '';
106+
}
96107
result.chapters[index.chapter].pages[index.page].explanation += line + '\n';
97108
}
98109
}
@@ -102,10 +113,7 @@ function page(result, lines, index) {
102113
}
103114
function task(result, lines, index) {
104115
result.chapters[index.chapter].pages[index.page].tasks.push({
105-
title: Match.task(lines[0]),
106-
description: '',
107-
tests: [],
108-
actions: []
116+
title: Match.task(lines[0])
109117
});
110118
index.task += 1;
111119
var inCodeBlock = false;
@@ -115,18 +123,16 @@ function task(result, lines, index) {
115123
inCodeBlock = !inCodeBlock;
116124
}
117125
if (!inCodeBlock) {
118-
if (!!Match.taskAction(line)) {
119-
var action = line.slice(1).split('(')[0];
120-
var target = cleanup_1.trimQuotes(/\((.*?)\)$/.exec(line)[1]);
121-
switch (action) {
122-
case 'test':
123-
result.chapters[index.chapter].pages[index.page].tasks[index.task].tests.push(target);
124-
break;
125-
case 'action':
126-
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(target);
127-
break;
128-
default:
129-
console.log('Invalid task action');
126+
if (!!Match.isAction(line)) {
127+
var isActionArray = Match.isArray(cleanup_1.trimQuotes(line));
128+
if (!!isActionArray) {
129+
arrayOfActions = JSON.parse(isActionArray);
130+
arrayOfActions.forEach(function (line) {
131+
result = actions_1.default(result, line, index);
132+
});
133+
}
134+
else {
135+
result = actions_1.default(result, line, index);
130136
}
131137
}
132138
else if (!!Match.task(line)) {
@@ -139,6 +145,9 @@ function task(result, lines, index) {
139145
return chapter(result, lines.slice(i), index);
140146
}
141147
else {
148+
if (result.chapters[index.chapter].pages[index.page].tasks[index.task].description === undefined) {
149+
result.chapters[index.chapter].pages[index.page].tasks[index.task].description = '';
150+
}
142151
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += line + '\n';
143152
}
144153
}

src/build/cleanup.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,33 @@ function trimQuotes(text) {
2323
}
2424
exports.trimQuotes = trimQuotes;
2525
function cleanup(result) {
26-
result.project.description = removeLineBreaks(result.project.description);
27-
result.chapters.map(function (chapter) {
28-
chapter.description = removeLineBreaks(chapter.description);
29-
chapter.pages.map(function (page) {
30-
page.description = removeLineBreaks(page.description);
31-
page.explanation = removeLineBreaks(page.explanation);
32-
page.tasks.map(function (task) {
33-
task.description = removeLineBreaks(task.description);
34-
});
26+
if (result.project.description) {
27+
result.project.description = removeLineBreaks(result.project.description);
28+
}
29+
if (result.chapters) {
30+
result.chapters.map(function (chapter) {
31+
if (chapter.description) {
32+
chapter.description = removeLineBreaks(chapter.description);
33+
}
34+
if (chapter.pages) {
35+
chapter.pages.map(function (page) {
36+
if (page.description) {
37+
page.description = removeLineBreaks(page.description);
38+
}
39+
if (page.explanation) {
40+
page.explanation = removeLineBreaks(page.explanation);
41+
}
42+
if (page.tasks) {
43+
page.tasks.map(function (task) {
44+
if (task.description) {
45+
task.description = removeLineBreaks(task.description);
46+
}
47+
});
48+
}
49+
});
50+
}
3551
});
36-
});
52+
}
3753
return JSON.stringify(result, null, 2);
3854
}
3955
exports.cleanup = cleanup;

src/build/matchers.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var regex = {
77
'##': match('#', 2),
88
'###': match('#', 3),
99
'+': match('\\+', 1),
10-
'@': match('@', 1),
1110
'```': match('`', 3)
1211
};
1312
function parseWithCode(code) {
@@ -32,6 +31,14 @@ exports.chapter = parseWithCode('##');
3231
exports.page = parseWithCode('###');
3332
exports.task = parseWithCode('+');
3433
exports.codeBlock = parseWithCode('```');
35-
exports.taskAction = function (line) {
36-
line.match(/^@action|test|hint/);
34+
exports.isArray = function (line) {
35+
var isMatch = line.match(/^\[(.+)\]$/);
36+
return isMatch ? isMatch[1] : false;
37+
};
38+
exports.isAction = function (line) {
39+
var isMatch = line.match(/^@(action|test|hint)?/);
40+
return isMatch ? isMatch[1] : false;
41+
};
42+
exports.isImport = function (line) {
43+
return line.match(/^@import/);
3744
};

0 commit comments

Comments
 (0)