Skip to content

Commit 93ea7ac

Browse files
committed
improved typings
1 parent ea38acb commit 93ea7ac

40 files changed

+196
-196
lines changed

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
src
22
test
3-
typings
43
tsd.json
54
tsconfig.json
65
tslint.json

lib/build/build.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ var readme_1 = require('./readme');
66
var cleanup_1 = require('./parser/cleanup');
77
function build(lines) {
88
var result = {
9-
project: {},
9+
project: {
10+
title: '',
11+
description: ''
12+
},
1013
chapters: []
1114
};
1215
var index = {

lib/build/parser/actions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ function doAction(type, isArray, actionValue, result, line, index) {
2727
return result;
2828
}
2929
function addToTasks(result, line, index) {
30-
var match = Match.isAction(line);
31-
var action = match.action;
30+
var action = Match.isAction(line);
3231
var task = result.chapters[index.chapter].pages[index.page].tasks[index.task];
3332
var trimmedContent = line.slice(action.length + 2, line.length - 1);
3433
var actionValue = cleanup_1.trimQuotes(trimmedContent);
@@ -40,6 +39,8 @@ function addToTasks(result, line, index) {
4039
case 'hint':
4140
result = doAction('hints', isActionArray, actionValue, result, line, index);
4241
break;
42+
case 'continue':
43+
result = doAction('continue', isActionArray, actionValue, result, line, index);
4344
case 'action':
4445
if (task.actions === undefined) {
4546
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions = [];

lib/build/parser/chapter.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function chapter(result, lines, index) {
77
index.chapter += 1;
88
result.chapters.push({
99
title: Match.chapter(lines[0]).trim(),
10+
description: '',
1011
pages: []
1112
});
1213
var inCodeBlock = false;
@@ -42,9 +43,6 @@ function chapter(result, lines, index) {
4243
}
4344
exports.chapter = chapter;
4445
function addToDescription(i, result, line, index) {
45-
if (result.chapters[index.chapter].description === undefined) {
46-
result.chapters[index.chapter].description = '';
47-
}
4846
if (i > 1) {
4947
result.chapters[index.chapter].description += '\n';
5048
}

lib/build/parser/match.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var regex = {
1212
function parseWithCode(code) {
1313
return function (line) {
1414
if (!line) {
15-
return false;
15+
return null;
1616
}
1717
if (line.match(regex[code])) {
1818
return regex[code].exec(line)[1];
1919
}
2020
else {
21-
return false;
21+
return null;
2222
}
2323
};
2424
}
@@ -33,15 +33,13 @@ exports.task = parseWithCode('+');
3333
exports.codeBlock = parseWithCode('```');
3434
exports.isArray = function (line) {
3535
var isMatch = line.match(/^\[.+\]$/);
36-
return isMatch ? isMatch[0] : false;
36+
return isMatch ? isMatch[0] : null;
3737
};
3838
exports.isAction = function (line) {
39-
var match = line.match(/^@(action|test|hint)/);
40-
return !!match ? {
41-
action: match[1]
42-
} : false;
39+
var match = line.match(/^@(action|test|hint|continue)/);
40+
return !!match ? match[1] : null;
4341
};
4442
exports.isImport = function (line) {
4543
var isMatch = line.match(/^@import\((.+)\)$/);
46-
return isMatch ? isMatch[1] : false;
44+
return isMatch ? isMatch[1] : null;
4745
};

lib/build/parser/page.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function page(result, lines, index) {
88
index.page += 1;
99
index.task = -1;
1010
result.chapters[index.chapter].pages.push({
11-
title: Match.page(lines[0]).trim()
11+
title: Match.page(lines[0]).trim(),
12+
description: ''
1213
});
1314
var inCodeBlock = false;
1415
var i = 0;
@@ -53,9 +54,6 @@ function page(result, lines, index) {
5354
exports.page = page;
5455
function addToDescriptionOrExplanation(hasBreak, i, result, line, index) {
5556
if (!hasBreak) {
56-
if (result.chapters[index.chapter].pages[index.page].description === undefined) {
57-
result.chapters[index.chapter].pages[index.page].description = '';
58-
}
5957
if (i > 1) {
6058
result.chapters[index.chapter].pages[index.page].description += '\n';
6159
}

lib/build/parser/project.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ var Match = require('./match');
33
var chapter_1 = require('./chapter');
44
var import_1 = require('./import');
55
function project(result, lines, index) {
6-
result.project = {
7-
title: '',
8-
description: ''
9-
};
106
var inCodeBlock = false;
117
var i = -1;
128
while (i < lines.length - 1) {

lib/build/readme.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ function createReadme() {
88
console.log('No coderoad.json file found');
99
return;
1010
}
11-
;
1211
if (!file_1.fileExists('./package.json')) {
1312
console.log('No package.json file found');
1413
return;
1514
}
16-
;
1715
var data = JSON.parse(fs.readFileSync('coderoad.json', 'utf8'));
1816
var packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
1917
var content = generateReadme(data, packageJson);

lib/cli.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var create_1 = require('./create/create');
88
var search_1 = require('./search/search');
99
var tutorials_1 = require('./tutorials/tutorials');
1010
var publish_1 = require('./publish/publish');
11-
var docs_1 = require('./docs/docs');
1211
program
1312
.version('0.3.12')
1413
.usage('[options] <keywords>')
@@ -18,10 +17,9 @@ program
1817
.option('-t, --tutorials', 'list of tutorial packages')
1918
.option('-s, --search [query]', 'search for tutorial package')
2019
.option('-r, --run', 'run tutorial')
21-
.option('-d, --docs', 'development documentation')
2220
.parse(process.argv);
2321
if (!program.args.length &&
24-
!program.build && !program.tutorials && !program.run && !program.docs) {
22+
!program.build && !program.tutorials && !program.run) {
2523
program.help();
2624
}
2725
else {
@@ -47,8 +45,5 @@ else {
4745
var version = program.args[0];
4846
publish_1.default(version);
4947
}
50-
if (program.docs) {
51-
docs_1.default();
52-
}
5348
process.exit(0);
5449
}

lib/create/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
var chalk = require('chalk');
3-
var validateNpm = require("validate-npm-package-name");
3+
var validateNpm = require('validate-npm-package-name');
44
var _ = require('lodash');
55
function validatePackageName(name) {
66
var validated = validateNpm(name);

lib/publish/validate.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
var fs = require('fs');
33
var chalk = require('chalk');
44
var file_1 = require('../tools/file');
5+
function incrementVersion(version) {
6+
var finalDot = version.lastIndexOf('.');
7+
var start = version.substring(0, finalDot + 1);
8+
var patch = parseInt(version.substring(finalDot + 1, version.length)) + 1;
9+
return start + patch;
10+
}
511
function versionIsGreaterThanCurrent(version) {
612
if (!file_1.fileExists('package.json')) {
713
console.log(chalk.yellow("\n No available package.json file.Create one.\n > npm init\n "));
814
process.exit(1);
915
}
10-
var currentVersion = fs.readFileSync('package.json', 'utf8').version;
16+
var currentVersion = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
1117
if (parseInt(version) <= parseInt(currentVersion)) {
12-
var incrementedVersion = parseInt(currentVersion) + 0.0;
13-
.1;
14-
console.log(chalk.yellow("\n Published version is not larger than current version.\n Current: \"" + currentVersion + "\"\n > coderoad publish \"" + incrementedVersion + "\"\n "));
18+
console.log(chalk.yellow("\n Published version is not larger than current version.\n Current: \"" + currentVersion + "\"\n > coderoad publish \"" + incrementVersion(currentVersion) + "\"\n "));
1519
process.exit(1);
1620
}
1721
}

src/build/build.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import {project} from './parser/project';
44
import {createReadme} from './readme';
55
import {cleanup} from './parser/cleanup';
66

7-
function build(lines: string[]) {
7+
function build(lines: string[]): CR.Output {
88
let result = {
9-
project: {},
9+
project: {
10+
title: '',
11+
description: ''
12+
},
1013
chapters: []
1114
}
1215
let index = {
@@ -17,7 +20,7 @@ function build(lines: string[]) {
1720
return project(result, lines, index);
1821
}
1922

20-
export default function(filePath: string, output = './coderoad.json') {
23+
export default function(filePath: string, output = './coderoad.json'): void {
2124
// VALIDATE: path name
2225
validate.filePath(filePath);
2326

src/build/parser/actions.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function trimCommandValue(text: string): string {
1010
return command.action + '(\'' + command.value + '\')';
1111
}
1212

13-
function doAction(type: 'tests' | 'hints', isArray, actionValue, result, line, index) {
13+
function doAction(type: CR.OutputAction, isArray, actionValue, result, line, index) {
1414
// set to array
1515
if (result.chapters[index.chapter].pages[index.page].tasks[index.task][type] === undefined) {
1616
result.chapters[index.chapter].pages[index.page].tasks[index.task][type] = [];
@@ -28,10 +28,9 @@ function doAction(type: 'tests' | 'hints', isArray, actionValue, result, line, i
2828
}
2929

3030
export function addToTasks(result, line, index) {
31-
let match = Match.isAction(line);
32-
let action = match.action; //: 'action'|'test'|'hint'
33-
let task = result.chapters[index.chapter].pages[index.page].tasks[index.task];
34-
let trimmedContent = line.slice(action.length + 2, line.length - 1); // content between brackets
31+
let action: CR.TaskAction|string = Match.isAction(line); //: 'action'|'test'|'hint'|'continue'
32+
let task: CR.Task = result.chapters[index.chapter].pages[index.page].tasks[index.task];
33+
let trimmedContent: string = line.slice(action.length + 2, line.length - 1); // content between brackets
3534
let actionValue: string = trimQuotes(trimmedContent);
3635
let isActionArray = Match.isArray(trimQuotes(actionValue));
3736
switch (action) {
@@ -41,19 +40,21 @@ export function addToTasks(result, line, index) {
4140
case 'hint':
4241
result = doAction('hints', isActionArray, actionValue, result, line, index);
4342
break;
43+
case 'continue':
44+
result = doAction('continue', isActionArray, actionValue, result, line, index);
4445
case 'action':
4546
if (task.actions === undefined) {
4647
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions = [];
4748
}
4849
if (!!isActionArray) {
49-
var arrayOfActions = JSON.parse(isActionArray);
50+
var arrayOfActions: string[] = JSON.parse(isActionArray);
5051
arrayOfActions.forEach(function(value) {
5152
value = trimCommandValue(trimQuotes(value.trim()));
5253
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(value);
5354
});
5455
}
5556
else {
56-
let value = trimCommandValue(actionValue);
57+
let value: string = trimCommandValue(actionValue);
5758
result.chapters[index.chapter].pages[index.page].tasks[index.task].actions.push(value);
5859
}
5960
return result;

src/build/parser/chapter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {page} from './page';
33
import {loadImport} from './import';
44
import {trimLineBreaks} from './cleanup';
55

6-
export function chapter(result: Result, lines: string[], index: Index): Result {
6+
export function chapter(result: CR.Output, lines: string[], index: CR.Index): CR.Output {
77
index.page = -1;
88
index.chapter += 1;
99
result.chapters.push({
1010
title: Match.chapter(lines[0]).trim(),
11+
description: '',
1112
pages: []
1213
});
1314
let inCodeBlock = false;
@@ -45,9 +46,6 @@ export function chapter(result: Result, lines: string[], index: Index): Result {
4546
}
4647

4748
function addToDescription(i, result, line, index) {
48-
if (result.chapters[index.chapter].description === undefined) {
49-
result.chapters[index.chapter].description = '';
50-
}
5149
if (i > 1) {
5250
result.chapters[index.chapter].description += '\n';
5351
}

src/build/parser/cleanup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function trimLineBreaks(text: string) {
77

88
let quotes = ['\'', '"'];
99

10-
export function trimQuotes(text: string, quotesOnly?: boolean) {
10+
export function trimQuotes(text: string, quotesOnly?: boolean): string {
1111
if (!!text.match(/^[\r\n]/)) {
1212
return text;
1313
} else if (!!text.match(/^\s/)) {
@@ -33,15 +33,15 @@ export function trimQuotes(text: string, quotesOnly?: boolean) {
3333
}
3434
}
3535

36-
export function trimLeadingSpaces(text: string) {
36+
export function trimLeadingSpaces(text: string): string {
3737
if (!!text.match(/^\s/)) {
3838
return text.slice(1);
3939
} else {
4040
return text;
4141
}
4242
}
4343

44-
export function cleanup(result: Result) {
44+
export function cleanup(result: CR.Output): string {
4545
result = JSON.parse(JSON.stringify(result));
4646
if (result.project.description) {
4747
result.project.description = trimLineBreaks(result.project.description);

src/build/parser/import.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ export function loadImport(lines: string[], pathToMd: string): string[] {
88
if (!pathToMd.match(/\.md$/)) {
99
pathToMd = pathToMd.concat('.md');
1010
}
11-
let realPath = path.join(process.cwd(), pathToMd);
11+
let realPath: string = path.join(process.cwd(), pathToMd);
1212
if (!fileExists(pathToMd)) {
1313
console.log('Invalid path to markdown file', realPath);
1414
return;
1515
}
1616
let importLines: string = fs.readFileSync(realPath, 'utf8');
17-
let splitLines = importLines.split('\n');
17+
let splitLines: string[] = importLines.split('\n');
1818
return lines.concat(splitLines);
1919
}

src/build/parser/match.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ var regex = {
1111
};
1212

1313
function parseWithCode(code: string) {
14-
return function(line: string) {
14+
return function(line: string): string {
1515
if (!line) {
16-
return false;
16+
return null;
1717
}
1818
if (line.match(regex[code])) {
1919
return regex[code].exec(line)[1];
2020
} else {
21-
return false;
21+
return null;
2222
}
2323
};
2424
}
@@ -32,19 +32,16 @@ export const chapter = parseWithCode('##');
3232
export const page = parseWithCode('###');
3333
export const task = parseWithCode('+');
3434
export const codeBlock = parseWithCode('```');
35-
export const isArray = function(line: string) {
35+
export const isArray = function(line: string): string {
3636
let isMatch = line.match(/^\[.+\]$/);
37-
return isMatch ? isMatch[0] : false;
37+
return isMatch ? isMatch[0] : null;
3838
}
39-
export const isAction = function(line: string) {
39+
export const isAction = function(line: string): string {
4040
// returns [0]: full, [1]: action, [2]: target
41-
let match = line.match(/^@(action|test|hint)/);
42-
return !!match ? {
43-
action: match[1]
44-
// content: match[2]
45-
} : false;
41+
let match: string[] = line.match(/^@(action|test|hint|continue)/);
42+
return !!match ? match[1] : null;
4643
}
47-
export const isImport = function(line: string): string|boolean {
44+
export const isImport = function(line: string): string {
4845
let isMatch = line.match(/^@import\((.+)\)$/);
49-
return isMatch ? isMatch[1] : false;
46+
return isMatch ? isMatch[1] : null;
5047
}

0 commit comments

Comments
 (0)