Skip to content

Commit c90ecf9

Browse files
committed
cli setup & build
1 parent b2a9e99 commit c90ecf9

File tree

5 files changed

+85
-56
lines changed

5 files changed

+85
-56
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ src/typings.d.ts
77
src/README.md
88
dist/cr.json
99
coderoad.json
10+
notes.md
11+
cli.ts

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# CodeRoad Builder
1+
# CodeRoad CLI
22

3-
Transform markdown tutorials into `coderoad.json` format.
3+
Coming soon.

cli.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#! /usr/bin/env node
2+
3+
var program = require('commander'),
4+
chalk = require('chalk');
5+
program
6+
.version('0.0.1')
7+
.usage('[options] <keywords>')
8+
.option('-b, --build [tutorial.md]', 'Tutorial Markdown file', /^.+\.md$/i)
9+
.parse(process.argv);
10+
if (!program.args.length) {
11+
program.help();
12+
} else {
13+
if (program.build) {
14+
(function() {
15+
var tutorial = program.args[0];
16+
var output = 'coderoad.json';
17+
if (!tutorial) {
18+
console.log(chalk.red("\n Pass in a path to your .md file, otherwise it defaults to README.md\n For example: npm start ./src/source.md\n "));
19+
process.exit(1);
20+
}
21+
var build = require('./src/build');
22+
console.log(chalk.grey("building from " + tutorial + "..."));
23+
build(tutorial, output);
24+
})();
25+
process.exit(0);
26+
}
27+
}

package.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
{
2-
"name": "coderoad-builder",
2+
"name": "coderoad-cli",
33
"version": "0.1.0",
4-
"description": "Generates tutorial project files for the coderoad-viewer",
5-
"main": "src/build.js",
4+
"description": "Command line interface for CodeRoad. Build project files.",
65
"directories": {
76
"test": "tests"
87
},
98
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1",
11-
"start": "node ./src/build.js coderoad.json"
9+
"test": "echo \"Error: no test specified\" && exit 1"
1210
},
1311
"author": "Shawn McKay <shawn.j.mckay@gmail.com>",
14-
"license": "ISC"
12+
"license": "ISC",
13+
"preferGlobal": true,
14+
"bin": {
15+
"coderoad": "cli.js"
16+
},
17+
"dependencies": {
18+
"chalk": "^1.1.1",
19+
"commander": "^2.9.0"
20+
}
1521
}

src/build.js

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function parseWithCode(code, content) {
2525
return false;
2626
}
2727
}
28-
function build(filePath) {
28+
function build(lines) {
2929
var result = {
3030
project: {
3131
title: '',
@@ -37,13 +37,11 @@ function build(filePath) {
3737
page: -1,
3838
task: -1
3939
};
40-
var input = fs.readFileSync(filePath, 'utf8');
41-
var lines = input.split('\n');
4240
return project(result, lines, index);
4341
}
4442
function project(result, lines, index) {
45-
let matchedAt = null;
46-
for (let i = 0; i < lines.length; i++) {
43+
var matchedAt = null;
44+
for (var i = 0; i < lines.length; i++) {
4745
var projectTitleMatch = parseWithCode('#', lines[i]);
4846
var chapterStart = parseWithCode('##', lines[i]);
4947
if (projectTitleMatch) {
@@ -61,9 +59,9 @@ function project(result, lines, index) {
6159
}
6260
function chapter(result, lines, index) {
6361
var matchedAt = null;
64-
for (let i = 0; i < lines.length; i++) {
65-
let chapterTitleMatch = parseWithCode('##', lines[i]);
66-
let pageStart = parseWithCode('###', lines[i]);
62+
for (var i = 0; i < lines.length; i++) {
63+
var chapterTitleMatch = parseWithCode('##', lines[i]);
64+
var pageStart = parseWithCode('###', lines[i]);
6765
if (chapterTitleMatch && !matchedAt) {
6866
matchedAt = i;
6967
index.page = -1;
@@ -87,7 +85,7 @@ function chapter(result, lines, index) {
8785
return result;
8886
}
8987
function page(result, lines, index) {
90-
let hasBreak = null;
88+
var hasBreak = null;
9189
index.page += 1;
9290
index.task = -1;
9391
result.chapters[index.chapter].pages.push({
@@ -96,12 +94,12 @@ function page(result, lines, index) {
9694
explanation: '',
9795
tasks: []
9896
});
99-
let inCodeBlock = false;
100-
for (let i = 1; i < lines.length; i++) {
101-
let pageTitleMatch = parseWithCode('###', lines[i]);
102-
let nextChapter = parseWithCode('##', lines[i]);
103-
let nextTask = parseWithCode('+', lines[i]);
104-
let codeBlock = parseWithCode('```', lines[i]);
97+
var inCodeBlock = false;
98+
for (var i = 1; i < lines.length; i++) {
99+
var pageTitleMatch = parseWithCode('###', lines[i]);
100+
var nextChapter = parseWithCode('##', lines[i]);
101+
var nextTask = parseWithCode('+', lines[i]);
102+
var codeBlock = parseWithCode('```', lines[i]);
105103
if (!!codeBlock) {
106104
inCodeBlock = !inCodeBlock;
107105
}
@@ -138,20 +136,20 @@ function task(result, lines, index) {
138136
actions: []
139137
});
140138
index.task += 1;
141-
let inCodeBlock = false;
142-
for (let i = 1; i < lines.length; i++) {
143-
let nextPage = parseWithCode('###', lines[i]);
144-
let nextChapter = parseWithCode('##', lines[i]);
145-
let nextTask = parseWithCode('+', lines[i]);
146-
let isPossibleAction = lines[i].match(/^@action|test|hint/);
147-
let codeBlock = parseWithCode('```', lines[i]);
139+
var inCodeBlock = false;
140+
for (var i = 1; i < lines.length; i++) {
141+
var nextPage = parseWithCode('###', lines[i]);
142+
var nextChapter = parseWithCode('##', lines[i]);
143+
var nextTask = parseWithCode('+', lines[i]);
144+
var isPossibleAction = lines[i].match(/^@action|test|hint/);
145+
var codeBlock = parseWithCode('```', lines[i]);
148146
if (!!codeBlock) {
149147
inCodeBlock = !inCodeBlock;
150148
}
151149
if (!inCodeBlock) {
152150
if (!!isPossibleAction) {
153-
let action = lines[i].slice(1).split('(')[0];
154-
let target = /\((.*?)\)$/.exec(lines[i])[1];
151+
var action = lines[i].slice(1).split('(')[0];
152+
var target = /\((.*?)\)$/.exec(lines[i])[1];
155153
switch (action) {
156154
case 'test':
157155
result.chapters[index.chapter].pages[index.page].tasks[index.task].tests.push(target);
@@ -192,12 +190,12 @@ function removeLineBreaks(text) {
192190
}
193191
function cleanup(result) {
194192
result.project.description = removeLineBreaks(result.project.description);
195-
result.chapters.map((chapter) => {
193+
result.chapters.map(function (chapter) {
196194
chapter.description = removeLineBreaks(chapter.description);
197-
chapter.pages.map((page) => {
195+
chapter.pages.map(function (page) {
198196
page.description = removeLineBreaks(page.description);
199197
page.explanation = removeLineBreaks(page.explanation);
200-
page.tasks.map((task) => {
198+
page.tasks.map(function (task) {
201199
task.description = removeLineBreaks(task.description);
202200
});
203201
});
@@ -214,25 +212,21 @@ function isValidJSON(text) {
214212
return false;
215213
}
216214
}
217-
var output = process.argv[2];
218-
if (!output) {
219-
throw ('Pass in path to output cr.json file');
220-
}
221-
var input = process.argv[3];
222-
if (!input) {
223-
input = './README.md';
224-
console.log(`
225-
Pass in a path to your .md file, otherwise it defaults to README.md
226-
For example: npm start ./src/source.md
227-
`);
228-
}
229-
var result = cleanup(build(input));
230-
if (!isValidJSON(result)) {
231-
throw ('Invalid JSON output');
232-
}
233-
fs.writeFile(output, result), 'utf8', function (err) {
234-
if (err)
235-
return console.log(err);
236-
console.log(input + ' > ' + output);
215+
module.exports = function (filePath, output) {
216+
if (output === void 0) { output = './coderoad.json'; }
217+
var lines = fs.readFileSync(filePath, 'utf8').split('\n');
218+
var result = cleanup(build(lines));
219+
if (!isValidJSON(result)) {
220+
console.log('Invalid JSON output');
221+
process.exit(0);
222+
}
223+
else {
224+
try {
225+
fs.writeFileSync(output, result, 'utf8');
226+
}
227+
catch (e) {
228+
console.log(e);
229+
process.exit(0);
230+
}
231+
}
237232
};
238-
;

0 commit comments

Comments
 (0)