Skip to content

Commit 5f7da5d

Browse files
committed
build array of actions
1 parent e756fb3 commit 5f7da5d

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

src/build/actions.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
"use strict";
22
var cleanup_1 = require('./cleanup');
3-
function addAction(result, line, index) {
3+
function addToTasks(result, line, index) {
44
var action = line.slice(1).split('(')[0];
5-
var target = cleanup_1.trimQuotes(/\((.*?)\)$/.exec(line)[1]);
5+
var value = cleanup_1.trimQuotes(/\((.*?)\)$/.exec(line)[1]);
66
var task = result.chapters[index.chapter].pages[index.page].tasks[index.task];
77
switch (action) {
88
case 'test':
9-
if (task.tests === undefined) {
9+
if (result.chapters[index.chapter].pages[index.page].tasks[index.task].tests === undefined) {
1010
result.chapters[index.chapter].pages[index.page].tasks[index.task].tests = [];
1111
}
12-
result.chapters[index.chapter].pages[index.page].tasks[index.task].tests.push(target);
12+
result.chapters[index.chapter].pages[index.page].tasks[index.task].tests.push(value);
1313
break;
1414
case 'action':
15-
if (task.actions === undefined) {
15+
var task_1 = result.chapters[index.chapter].pages[index.page].tasks[index.task];
16+
if (task_1.actions === undefined) {
1617
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions = [];
1718
}
18-
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(target);
19+
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(value);
20+
return result;
1921
break;
2022
case 'hint':
21-
if (task.hints === undefined) {
23+
if (task_1.hints === undefined) {
2224
result.chapters[index.chapter].pages[index.page].tasks[index.task].hints = [];
2325
}
24-
result.chapters[index.chapter].pages[index.page].tasks[index.task].hints.push(target);
26+
result.chapters[index.chapter].pages[index.page].tasks[index.task].hints.push(value);
2527
break;
2628
default:
2729
console.log('Invalid task action');
2830
}
2931
return result;
3032
}
31-
Object.defineProperty(exports, "__esModule", { value: true });
32-
exports.default = addAction;
33+
exports.addToTasks = addToTasks;

src/build/build.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"use strict";
22
var fs = require('fs');
3-
var process = require('process');
4-
var chalk = require('chalk');
53
var Match = require('./matchers');
6-
var validators_1 = require('./validators');
4+
var validate = require('./validators');
75
var actions_1 = require('./actions');
86
var cleanup_1 = require('./cleanup');
97
function build(lines) {
@@ -123,16 +121,17 @@ function task(result, lines, index) {
123121
inCodeBlock = !inCodeBlock;
124122
}
125123
if (!inCodeBlock) {
126-
if (!!Match.isAction(line)) {
127-
var isActionArray = Match.isArray(cleanup_1.trimQuotes(line));
124+
var isAction = Match.isAction(line);
125+
if (!!isAction) {
126+
var isActionArray = Match.isArray(cleanup_1.trimQuotes(isAction[2]));
128127
if (!!isActionArray) {
129-
arrayOfActions = JSON.parse(isActionArray);
130-
arrayOfActions.forEach(function (line) {
131-
result = actions_1.default(result, line, index);
128+
var arrayOfActions = JSON.parse(isActionArray);
129+
arrayOfActions.forEach(function (action) {
130+
result = actions_1.addToTasks(result, "@action(" + action + ")", index);
132131
});
133132
}
134133
else {
135-
result = actions_1.default(result, line, index);
134+
result = actions_1.addToTasks(result, line, index);
136135
}
137136
}
138137
else if (!!Match.task(line)) {
@@ -156,13 +155,10 @@ function task(result, lines, index) {
156155
}
157156
module.exports = function (filePath, output) {
158157
if (output === void 0) { output = './coderoad.json'; }
159-
if (!filePath) {
160-
console.log(chalk.red("\n Pass in a path to your .md file\n For example: coderoad build ./src/tutorial.md\n "));
161-
process.exit(1);
162-
}
158+
validate.filePath(filePath);
163159
var lines = fs.readFileSync(filePath, 'utf8').split('\n');
164160
var result = cleanup_1.cleanup(build(lines));
165-
if (validators_1.default(result)) {
161+
if (validate.result(result)) {
166162
fs.writeFileSync(output, result, 'utf8');
167163
}
168164
};

src/build/import.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
function loadImport(pathToFile) {
3+
}
4+
Object.defineProperty(exports, "__esModule", { value: true });
5+
exports.default = loadImport;

src/build/matchers.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ exports.page = parseWithCode('###');
3232
exports.task = parseWithCode('+');
3333
exports.codeBlock = parseWithCode('```');
3434
exports.isArray = function (line) {
35-
var isMatch = line.match(/^\[(.+)\]$/);
36-
return isMatch ? isMatch[1] : false;
35+
var isMatch = line.match(/^\[.+\]$/);
36+
if (isMatch) {
37+
return isMatch[0];
38+
}
39+
else {
40+
return false;
41+
}
3742
};
3843
exports.isAction = function (line) {
39-
var isMatch = line.match(/^@(action|test|hint)?/);
40-
return isMatch ? isMatch[1] : false;
44+
return line.match(/^@(action|test|hint)\((.+)\)$/);
4145
};
4246
exports.isImport = function (line) {
43-
return line.match(/^@import/);
47+
return line.match(/^@import\((.+)\)$/);
4448
};

src/build/validators.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
"use strict";
22
var chalk = require('chalk');
3-
function validate(text) {
3+
function filePath(filePath) {
4+
if (!filePath) {
5+
console.log(chalk.red("\n Pass in a path to your .md file\n For example: coderoad build ./src/tutorial.md\n "));
6+
process.exit(1);
7+
}
8+
}
9+
exports.filePath = filePath;
10+
function result(text) {
411
isValidJSON(text);
512
var jsonObject = JSON.parse(text);
613
hasProjectInfo(jsonObject);
714
hasPage(jsonObject);
815
return true;
916
}
10-
Object.defineProperty(exports, "__esModule", { value: true });
11-
exports.default = validate;
17+
exports.result = result;
1218
function isValidJSON(text) {
1319
if (!/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
1420
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').

0 commit comments

Comments
 (0)