Skip to content

Commit 72d6245

Browse files
committed
pass dir into build for programmatic use
1 parent 9533056 commit 72d6245

File tree

16 files changed

+52
-47
lines changed

16 files changed

+52
-47
lines changed

lib/build/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var validate = require('./validators');
44
var info_1 = require('./parser/info');
55
var readme_1 = require('./readme');
66
var cleanup_1 = require('./parser/cleanup');
7-
function parseAndBuild(lines) {
7+
var path_1 = require('path');
8+
function parseAndBuild(dir, lines) {
89
var result = {
910
info: {
1011
title: '',
@@ -16,15 +17,18 @@ function parseAndBuild(lines) {
1617
page: -1,
1718
task: -1,
1819
};
19-
return info_1.info(result, lines, index);
20+
return info_1.info(dir, result, lines, index);
2021
}
21-
function build(filePath, output) {
22+
function build(dir, filePath, output) {
2223
if (output === void 0) { output = './coderoad.json'; }
24+
filePath = path_1.join(dir, filePath);
25+
output = path_1.join(dir, output);
2326
if (!validate.filePath(filePath)) {
2427
return false;
2528
}
2629
var lines = fs_1.readFileSync(filePath, 'utf8').split('\n');
27-
var result = cleanup_1.cleanup(parseAndBuild(lines));
30+
var result = cleanup_1.cleanup(parseAndBuild(dir, lines));
31+
console.log('result', result);
2832
if (!result) {
2933
return false;
3034
}

lib/build/parser/import.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ var path_1 = require('path');
44
var node_file_exists_1 = require('node-file-exists');
55
var cleanup_1 = require('./cleanup');
66
var settings_1 = require('./settings');
7-
function loadImport(lines, pathToMd) {
7+
function loadImport(dir, lines, pathToMd) {
88
pathToMd = cleanup_1.trimQuotes(pathToMd);
99
if (!pathToMd.match(/\.md$/)) {
1010
pathToMd = pathToMd.concat('.md');
1111
}
12-
var realPath = path_1.join(process.cwd(), settings_1.tutorialDir, pathToMd);
12+
var realPath = path_1.join(dir, settings_1.tutorialDir, pathToMd);
1313
if (!node_file_exists_1.default(realPath)) {
1414
console.log('Invalid path to markdown file', realPath);
1515
return;

lib/build/parser/info.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
var Match = require('./match');
33
var page_1 = require('./page');
44
var import_1 = require('./import');
5-
function info(result, lines, index) {
5+
function info(dir, result, lines, index) {
66
var inCodeBlock = false;
77
var i = -1;
88
while (i < lines.length - 1) {
99
i += 1;
1010
var line = lines[i];
1111
switch (true) {
1212
case !!Match.isImport(line):
13-
lines = import_1.loadImport(lines, Match.isImport(line));
13+
lines = import_1.loadImport(dir, lines, Match.isImport(line));
1414
continue;
1515
case !!Match.codeBlock(line):
1616
if (line.length > 3) {
@@ -24,7 +24,7 @@ function info(result, lines, index) {
2424
result.info.title = Match.info(line).trim();
2525
continue;
2626
case !!Match.page(line):
27-
return page_1.page(result, lines.slice(i), index);
27+
return page_1.page(dir, result, lines.slice(i), index);
2828
default:
2929
if (i > 1) {
3030
result.info.description += '\n';

lib/build/parser/page.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Match = require('./match');
33
var task_1 = require('./task');
44
var import_1 = require('./import');
55
var cleanup_1 = require('./cleanup');
6-
function page(result, lines, index) {
6+
function page(dir, result, lines, index) {
77
index.page += 1;
88
index.task = -1;
99
result.pages.push({
@@ -19,7 +19,7 @@ function page(result, lines, index) {
1919
var line = lines[i];
2020
switch (true) {
2121
case !!Match.isImport(line):
22-
lines = import_1.loadImport(lines, Match.isImport(line));
22+
lines = import_1.loadImport(dir, lines, Match.isImport(line));
2323
continue;
2424
case (!!Match.isPageComplete(line) || !!currentPageComplete):
2525
currentPageComplete = !!currentPageComplete
@@ -42,12 +42,12 @@ function page(result, lines, index) {
4242
case inCodeBlock:
4343
continue;
4444
case !!Match.page(line):
45-
return page(result, lines.slice(i), index);
45+
return page(dir, result, lines.slice(i), index);
4646
case !!Match.task(line):
4747
if (result.pages[index.page].tasks === undefined) {
4848
result.pages[index.page].tasks = [];
4949
}
50-
return task_1.task(result, lines.slice(i), index);
50+
return task_1.task(dir, result, lines.slice(i), index);
5151
default:
5252
if (i > 1) {
5353
result.pages[index.page].description += '\n';

lib/build/parser/task.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var page_1 = require('./page');
44
var actions_1 = require('./actions');
55
var cleanup_1 = require('./cleanup');
66
var import_1 = require('./import');
7-
function task(result, lines, index) {
7+
function task(dir, result, lines, index) {
88
result.pages[index.page].tasks.push({
99
description: cleanup_1.trimLeadingSpaces(Match.task(lines[0]))
1010
});
@@ -19,7 +19,7 @@ function task(result, lines, index) {
1919
var line = lines[i];
2020
switch (true) {
2121
case !!Match.isImport(line):
22-
lines = import_1.loadImport(lines, Match.isImport(line));
22+
lines = import_1.loadImport(dir, lines, Match.isImport(line));
2323
continue;
2424
case (!!Match.isPageComplete(line) || !!currentPageComplete):
2525
currentPageComplete = !!currentPageComplete
@@ -66,9 +66,9 @@ function task(result, lines, index) {
6666
}
6767
continue;
6868
case !!Match.task(line):
69-
return task(result, lines.slice(i), index);
69+
return task(dir, result, lines.slice(i), index);
7070
case !!Match.page(line):
71-
return page_1.page(result, lines.slice(i), index);
71+
return page_1.page(dir, result, lines.slice(i), index);
7272
default:
7373
if (i > 0) {
7474
result.pages[index.page].tasks[index.task].description += '\n';

lib/cli.js

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (program.build) {
2424
var tutorial = program.args[0] || 'tutorial/tutorial.md';
2525
var output = 'coderoad.json';
2626
process.stdout.write(chalk_1.grey("building coderoad.json for " + tutorial + "..."));
27-
if (!build_1.default(tutorial, output)) {
27+
if (!build_1.default(process.cwd(), tutorial, output)) {
2828
result_1.fail();
2929
}
3030
}

lib/create/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
"use strict";
22
var validate_1 = require('./validate');
33
var write_demo_1 = require('./write-demo');
4-
var path_1 = require('path');
54
var chalk_1 = require('chalk');
6-
var build_1 = require('../build');
75
function create(dir, name) {
86
return Promise.all([
97
validate_1.validatePackageName(name),
108
write_demo_1.createPackageJson(dir, name),
119
write_demo_1.createTutorialMd(dir)
1210
]).then(function () {
13-
build_1.default(path_1.join(dir, 'tutorial/tutorial.md'), path_1.join(dir, 'coderoad.json'));
1411
return true;
1512
}).catch(function (e) {
1613
switch (e.type) {

lib/create/write-demo.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ var files = [
4141
path_1.join('tutorial', '02', '02.spec.js')
4242
];
4343
function createTutorialMd(dir) {
44-
console.log('dir', dir);
4544
return new Promise(function (resolve, reject) {
4645
folders(dir).forEach(function (folder) { return createFolder(folder); });
4746
files.forEach(function (file) { return createFile(dir, file); });

src/build/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import * as validate from './validators';
33
import {info} from './parser/info';
44
import {createReadme} from './readme';
55
import {cleanup} from './parser/cleanup';
6+
import {join} from 'path';
67

7-
function parseAndBuild(lines: string[]): CR.Output {
8+
function parseAndBuild(dir: string, lines: string[]): CR.Output {
89
// coderoad.json outline
910
const result: CR.Output = {
1011
info: {
@@ -17,10 +18,13 @@ function parseAndBuild(lines: string[]): CR.Output {
1718
page: -1,
1819
task: -1,
1920
};
20-
return info(result, lines, index);
21+
return info(dir, result, lines, index);
2122
}
2223

23-
export default function build(filePath: string, output = './coderoad.json'): boolean {
24+
export default function build(dir: string, filePath: string, output = './coderoad.json'): boolean {
25+
26+
filePath = join(dir, filePath);
27+
output = join(dir, output);
2428

2529
// validate path name
2630
if (!validate.filePath(filePath)) { return false; }
@@ -29,7 +33,9 @@ export default function build(filePath: string, output = './coderoad.json'): boo
2933
const lines: string[] = readFileSync(filePath, 'utf8').split('\n');
3034

3135
// build coeroad.json
32-
const result = cleanup(parseAndBuild(lines));
36+
const result = cleanup(parseAndBuild(dir, lines));
37+
38+
console.log('result', result);
3339

3440
// error parsing or building coderoad.json
3541
if (!result) { return false; }

src/build/parser/import.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import fileExists from 'node-file-exists';
44
import {trimQuotes} from './cleanup';
55
import {tutorialDir} from './settings';
66

7-
export function loadImport(lines: string[], pathToMd: string): string[] {
7+
export function loadImport(dir: string, lines: string[], pathToMd: string): string[] {
88
// add .md suffix
99
pathToMd = trimQuotes(pathToMd);
1010
if (!pathToMd.match(/\.md$/)) {
1111
pathToMd = pathToMd.concat('.md');
1212
}
1313
// get path to imports
14-
let realPath: string = join(process.cwd(), tutorialDir, pathToMd);
14+
let realPath: string = join(dir, tutorialDir, pathToMd);
1515
if (!fileExists(realPath)) {
1616
console.log('Invalid path to markdown file', realPath);
1717
return;

src/build/parser/info.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Match from './match';
22
import {page} from './page';
33
import {loadImport} from './import';
44

5-
export function info(result: CR.Output, lines: string[], index: CR.Index): CR.Output {
5+
export function info(dir: string, result: CR.Output, lines: string[], index: CR.Index): CR.Output {
66
let inCodeBlock = false;
77
let i = -1;
88

@@ -14,7 +14,7 @@ export function info(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
1414

1515
// @import
1616
case !!Match.isImport(line):
17-
lines = loadImport(lines, Match.isImport(line));
17+
lines = loadImport(dir, lines, Match.isImport(line));
1818
continue;
1919

2020
// ``` `
@@ -33,7 +33,7 @@ export function info(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
3333

3434
// ##
3535
case !!Match.page(line):
36-
return page(result, lines.slice(i), index);
36+
return page(dir, result, lines.slice(i), index);
3737

3838
// description
3939
default:

src/build/parser/page.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {task} from './task';
33
import {loadImport} from './import';
44
import {bracketTracker, trimValue} from './cleanup';
55

6-
export function page(result: CR.Output, lines: string[], index: CR.Index): CR.Output {
6+
export function page(dir: string, result: CR.Output, lines: string[], index: CR.Index): CR.Output {
77
index.page += 1;
88
index.task = -1;
99
result.pages.push({
@@ -23,7 +23,7 @@ export function page(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
2323

2424
// @import
2525
case !!Match.isImport(line):
26-
lines = loadImport(lines, Match.isImport(line));
26+
lines = loadImport(dir, lines, Match.isImport(line));
2727
continue;
2828

2929
// @onComplete
@@ -53,14 +53,14 @@ export function page(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
5353

5454
// ##
5555
case !!Match.page(line):
56-
return page(result, lines.slice(i), index);
56+
return page(dir, result, lines.slice(i), index);
5757

5858
// +
5959
case !!Match.task(line):
6060
if (result.pages[index.page].tasks === undefined) {
6161
result.pages[index.page].tasks = [];
6262
}
63-
return task(result, lines.slice(i), index);
63+
return task(dir, result, lines.slice(i), index);
6464

6565
// description
6666
default:

src/build/parser/task.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {addToTasks} from './actions';
44
import {trimLeadingSpaces, bracketTracker, trimValue} from './cleanup';
55
import {loadImport} from './import';
66

7-
export function task(result: CR.Output, lines: string[], index: CR.Index): CR.Output {
7+
export function task(dir: string, result: CR.Output, lines: string[], index: CR.Index): CR.Output {
88
result.pages[index.page].tasks.push({
99
description: trimLeadingSpaces(Match.task(lines[0]))
1010
});
@@ -23,7 +23,7 @@ export function task(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
2323

2424
// @import
2525
case !!Match.isImport(line):
26-
lines = loadImport(lines, Match.isImport(line));
26+
lines = loadImport(dir, lines, Match.isImport(line));
2727
continue;
2828

2929
// @onComplete
@@ -83,11 +83,11 @@ export function task(result: CR.Output, lines: string[], index: CR.Index): CR.Ou
8383

8484
// +
8585
case !!Match.task(line):
86-
return task(result, lines.slice(i), index);
86+
return task(dir, result, lines.slice(i), index);
8787

8888
// ##
8989
case !!Match.page(line):
90-
return page(result, lines.slice(i), index);
90+
return page(dir, result, lines.slice(i), index);
9191

9292

9393
// description

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if (program.build) {
3131
const output = 'coderoad.json';
3232
process.stdout.write(grey(`building coderoad.json for ${tutorial}...`));
3333
// run build
34-
if (!build(tutorial, output)) {
34+
if (!build(process.cwd(), tutorial, output)) {
3535
fail();
3636
}
3737

src/create/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {validatePackageName} from './validate';
22
import {createPackageJson, createTutorialMd} from './write-demo';
3-
import {join} from 'path';
3+
// import {join} from 'path';
44
import {red, yellow} from 'chalk';
5-
import build from '../build';
5+
// import build from '../build';
66

77
export default function create(dir: string, name: string): boolean | Promise<boolean> {
88

@@ -12,10 +12,10 @@ export default function create(dir: string, name: string): boolean | Promise<boo
1212
createPackageJson(dir, name),
1313
createTutorialMd(dir)
1414
]).then(() => {
15-
build(
16-
join(dir, 'tutorial/tutorial.md'),
17-
join(dir, 'coderoad.json')
18-
);
15+
// build(
16+
// join(dir, 'tutorial/tutorial.md'),
17+
// join(dir, 'coderoad.json')
18+
// );
1919
return true;
2020
}).catch((e) => {
2121

src/create/write-demo.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const files = [
4343
];
4444

4545
export function createTutorialMd(dir: string): Promise<boolean> {
46-
console.log('dir', dir);
4746
return new Promise((resolve, reject) => {
4847
folders(dir).forEach((folder) => createFolder(folder));
4948
files.forEach((file) => createFile(dir, file));

0 commit comments

Comments
 (0)