Skip to content

Commit d8d542a

Browse files
committed
refactor build for programmatic use
1 parent e70d158 commit d8d542a

File tree

18 files changed

+79
-136
lines changed

18 files changed

+79
-136
lines changed

lib/build/build.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/build/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ function build(filePath, output) {
2323
if (!validate.filePath(filePath)) {
2424
return false;
2525
}
26-
;
2726
var lines = fs_1.readFileSync(filePath, 'utf8').split('\n');
2827
var result = cleanup_1.cleanup(parseAndBuild(lines));
28+
if (!result) {
29+
return false;
30+
}
2931
if (validate.result(result)) {
3032
fs_1.writeFileSync(output, result, 'utf8');
3133
}
32-
readme_1.createReadme();
34+
if (!readme_1.createReadme()) {
35+
return false;
36+
}
3337
return true;
3438
}
3539
Object.defineProperty(exports, "__esModule", { value: true });

lib/build/lint.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,35 @@ function lintOutput(json) {
6262
invalidKeys.forEach(function (e) {
6363
console.log(chalk_1.red("\nError: " + e.error + ": ", e.location));
6464
});
65-
process.exit(1);
65+
return false;
6666
}
67+
return true;
6768
}
6869
exports.lintOutput = lintOutput;
6970
function isValidJSON(text) {
7071
if (!/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
7172
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
7273
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
7374
console.log(chalk_1.red('\nSomething went wrong. Build did not output valid JSON.'));
74-
process.exit(1);
75+
return false;
7576
}
77+
return true;
7678
}
7779
exports.isValidJSON = isValidJSON;
7880
function hasTutorialInfo(json) {
7981
var validTitle = json.info.title.length > 0, validDescription = json.info.description.length > 0;
8082
if (!(validTitle && validDescription)) {
8183
console.log(chalk_1.red('\nYour tutorial is missing basic project information. Check the project title & description.'));
82-
process.exit(1);
84+
return false;
8385
}
86+
return true;
8487
}
8588
exports.hasTutorialInfo = hasTutorialInfo;
8689
function hasPage(json) {
8790
if (!(json.pages.length > 0 && !!json.pages[0].title)) {
8891
console.log(chalk_1.red('\nYour tutorial requires at least one page.'));
89-
process.exit(1);
92+
return false;
9093
}
94+
return true;
9195
}
9296
exports.hasPage = hasPage;

lib/build/readme.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
"use strict";
22
var fs_1 = require('fs');
33
var file_1 = require('../tools/file');
4+
var chalk_1 = require('chalk');
45
function createReadme() {
56
if (!file_1.fileExists('./README.md')) {
67
}
78
if (!file_1.fileExists('./coderoad.json')) {
8-
console.log('No coderoad.json file found');
9-
return;
9+
console.log(chalk_1.red('No coderoad.json file found'));
10+
return false;
1011
}
1112
if (!file_1.fileExists('./package.json')) {
12-
console.log('No package.json file found');
13-
return;
13+
console.log(chalk_1.red('No package.json file found'));
14+
return false;
1415
}
1516
var data = JSON.parse(fs_1.readFileSync('coderoad.json', 'utf8'));
1617
var packageJson = JSON.parse(fs_1.readFileSync('package.json', 'utf8'));
1718
var content = generateReadme(data, packageJson);
1819
fs_1.writeFileSync('README.md', content, 'utf8');
20+
return true;
1921
}
2022
exports.createReadme = createReadme;
2123
function generateReadme(data, packageJson) {

lib/build/validators.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ function filePath(filePath) {
66
console.log(chalk.red("\n Pass in a path to your .md file\n > coderoad build \"./src/tutorial.md\"\n "));
77
return false;
88
}
9+
return true;
910
}
1011
exports.filePath = filePath;
1112
function result(text) {
12-
lint_1.isValidJSON(text);
13+
if (!lint_1.isValidJSON(text)) {
14+
return false;
15+
}
1316
var jsonObject = JSON.parse(text);
14-
lint_1.hasTutorialInfo(jsonObject);
15-
lint_1.hasPage(jsonObject);
16-
lint_1.lintOutput(jsonObject);
17-
return true;
17+
return [
18+
lint_1.hasTutorialInfo(jsonObject),
19+
lint_1.hasPage(jsonObject),
20+
lint_1.lintOutput(jsonObject)
21+
].every(function (x) { return !!x; });
1822
}
1923
exports.result = result;

lib/cli.js

100755100644
File mode changed.

lib/create/create.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/publish/publish.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/search/search.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/tutorials/tutorials.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/update/update.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/write.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
},
3333
"devDependencies": {
3434
"chai": "3.5.0",
35-
"mocha": "2.4.5"
35+
"mocha": "2.5.3"
3636
}
3737
}

src/build/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,42 @@ import {createReadme} from './readme';
55
import {cleanup} from './parser/cleanup';
66

77
function parseAndBuild(lines: string[]): CR.Output {
8-
let result: CR.Output = {
8+
// coderoad.json outline
9+
const result: CR.Output = {
910
info: {
1011
title: '',
1112
description: '',
1213
},
1314
pages: []
1415
};
15-
let index: CR.Index = {
16+
const index: CR.Index = {
1617
page: -1,
1718
task: -1,
1819
};
1920
return info(result, lines, index);
2021
}
2122

2223
export default function build(filePath: string, output = './coderoad.json'): boolean {
23-
// VALIDATE: path name
24-
if (!validate.filePath(filePath)) {
25-
return false;
26-
};
2724

28-
// Read
29-
let lines: string[] = readFileSync(filePath, 'utf8').split('\n');
30-
// Build
31-
let result = cleanup(parseAndBuild(lines));
25+
// validate path name
26+
if (!validate.filePath(filePath)) { return false; }
27+
28+
// read tutorial.md
29+
const lines: string[] = readFileSync(filePath, 'utf8').split('\n');
30+
31+
// build coeroad.json
32+
const result = cleanup(parseAndBuild(lines));
33+
34+
// error parsing or building coderoad.json
35+
if (!result) { return false; }
3236

3337
if (validate.result(result)) {
3438
// Safe to Write coderoad.json
3539
writeFileSync(output, result, 'utf8');
3640
}
37-
createReadme();
41+
42+
// check error creating readme
43+
if (!createReadme()) { return false; }
44+
3845
return true;
3946
}

src/build/lint.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const validKeys = {
66
task: ['description', 'tests', 'actions', 'hints']
77
};
88

9-
export function lintOutput(json: CR.Output): void {
9+
export function lintOutput(json: CR.Output): boolean {
1010
let invalidKeys = [];
1111
let warningKeys = [];
1212

@@ -73,32 +73,35 @@ export function lintOutput(json: CR.Output): void {
7373
invalidKeys.forEach((e) => {
7474
console.log(red(`\nError: ${e.error}: `, e.location));
7575
});
76-
77-
process.exit(1); // fail
76+
return false;
7877
}
78+
return true;
7979
}
8080

81-
export function isValidJSON(text: string): void {
81+
export function isValidJSON(text: string): boolean {
8282
if (!/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
8383
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
8484
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
8585
console.log(red('\nSomething went wrong. Build did not output valid JSON.'));
86-
process.exit(1); // fail
86+
return false;
8787
}
88+
return true;
8889
}
8990

90-
export function hasTutorialInfo(json: CR.Output): void {
91+
export function hasTutorialInfo(json: CR.Output): boolean {
9192
let validTitle = json.info.title.length > 0,
9293
validDescription = json.info.description.length > 0;
9394
if (!(validTitle && validDescription)) {
9495
console.log(red('\nYour tutorial is missing basic project information. Check the project title & description.'));
95-
process.exit(1); // fail
96+
return false;
9697
}
98+
return true;
9799
}
98100

99-
export function hasPage(json: CR.Output): void {
101+
export function hasPage(json: CR.Output): boolean {
100102
if (!(json.pages.length > 0 && !!json.pages[0].title)) {
101103
console.log(red('\nYour tutorial requires at least one page.'));
102-
process.exit(1); // fail
104+
return false;
103105
}
106+
return true;
104107
}

src/build/readme.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import {readFileSync, writeFileSync} from 'fs';
22
import {fileExists} from '../tools/file';
3+
import {red} from 'chalk';
34

4-
export function createReadme(): void {
5+
export function createReadme(): boolean {
56
if (!fileExists('./README.md')) {
67
// prompt.start();
78
// prompt.get(['overwriteReadme'], function (err, result) {
89
// console.log(result);
910
// });
1011
}
1112
if (!fileExists('./coderoad.json')) {
12-
console.log('No coderoad.json file found');
13-
return;
13+
console.log(red('No coderoad.json file found'));
14+
return false;
1415
}
1516
if (!fileExists('./package.json')) {
16-
console.log('No package.json file found');
17-
return;
17+
console.log(red('No package.json file found'));
18+
return false;
1819
}
1920
let data: CR.Output = JSON.parse(readFileSync('coderoad.json', 'utf8'));
2021
let packageJson: PackageJson = JSON.parse(readFileSync('package.json', 'utf8'));
2122
let content: string = generateReadme(data, packageJson);
2223
writeFileSync('README.md', content, 'utf8');
24+
return true;
2325
}
2426

2527
function generateReadme(data: CR.Output, packageJson: PackageJson): string {

src/build/validators.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ export function filePath(filePath: string): boolean {
1010
return false; // fail
1111
}
1212
// regex .md
13+
return true;
1314
}
1415

1516
export function result(text: string): boolean {
16-
isValidJSON(text);
1717

18-
let jsonObject = JSON.parse(text);
19-
hasTutorialInfo(jsonObject);
20-
hasPage(jsonObject);
21-
lintOutput(jsonObject);
22-
return true;
18+
if (!isValidJSON(text)) { return false; }
19+
20+
const jsonObject = JSON.parse(text);
21+
22+
// run tests
23+
return [
24+
hasTutorialInfo(jsonObject),
25+
hasPage(jsonObject),
26+
lintOutput(jsonObject)
27+
].every(x => !!x);
2328
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"src/typings/chalk/chalk.d.ts",
4242
"src/typings/commander/commander.d.ts",
4343
"src/typings/cr.d.ts",
44+
"src/typings/es6-promise/es6-promise.d.ts",
4445
"src/typings/globals.d.ts",
4546
"src/typings/node/node.d.ts",
4647
"src/typings/prompt/prompt.d.ts",

0 commit comments

Comments
 (0)