Skip to content

Commit e70d158

Browse files
committed
refactor process out of cli, allow for programmatic use
1 parent 2ab3537 commit e70d158

File tree

25 files changed

+283
-41
lines changed

25 files changed

+283
-41
lines changed

lib/build/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
var fs_1 = require('fs');
3+
var validate = require('./validators');
4+
var info_1 = require('./parser/info');
5+
var readme_1 = require('./readme');
6+
var cleanup_1 = require('./parser/cleanup');
7+
function parseAndBuild(lines) {
8+
var result = {
9+
info: {
10+
title: '',
11+
description: '',
12+
},
13+
pages: []
14+
};
15+
var index = {
16+
page: -1,
17+
task: -1,
18+
};
19+
return info_1.info(result, lines, index);
20+
}
21+
function build(filePath, output) {
22+
if (output === void 0) { output = './coderoad.json'; }
23+
if (!validate.filePath(filePath)) {
24+
return false;
25+
}
26+
;
27+
var lines = fs_1.readFileSync(filePath, 'utf8').split('\n');
28+
var result = cleanup_1.cleanup(parseAndBuild(lines));
29+
if (validate.result(result)) {
30+
fs_1.writeFileSync(output, result, 'utf8');
31+
}
32+
readme_1.createReadme();
33+
return true;
34+
}
35+
Object.defineProperty(exports, "__esModule", { value: true });
36+
exports.default = build;

lib/build/validators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var lint_1 = require('./lint');
44
function filePath(filePath) {
55
if (!filePath) {
66
console.log(chalk.red("\n Pass in a path to your .md file\n > coderoad build \"./src/tutorial.md\"\n "));
7-
process.exit(1);
7+
return false;
88
}
99
}
1010
exports.filePath = filePath;

lib/cli.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"use strict";
33
var program = require('commander');
44
var chalk_1 = require('chalk');
5-
var build_1 = require('./build/build');
6-
var create_1 = require('./create/create');
7-
var search_1 = require('./search/search');
8-
var tutorials_1 = require('./tutorials/tutorials');
9-
var publish_1 = require('./publish/publish');
10-
var update_1 = require('./update/update');
5+
var result_1 = require('./result');
6+
var build_1 = require('./build');
7+
var create_1 = require('./create');
8+
var search_1 = require('./search');
9+
var tutorials_1 = require('./tutorials');
10+
var publish_1 = require('./publish');
11+
var update_1 = require('./update');
1112
program
1213
.version('0.6.0')
1314
.usage('[options] <keywords>')
@@ -18,16 +19,22 @@ program
1819
.option('-s, --search [query]', 'search for tutorial package')
1920
.option('-r, --run', 'run tutorial')
2021
.parse(process.argv);
21-
update_1.checkForUpdate();
22+
update_1.default();
2223
if (program.build) {
2324
var tutorial = program.args[0] || 'tutorial/tutorial.md';
2425
var output = 'coderoad.json';
2526
process.stdout.write(chalk_1.grey("building coderoad.json for " + tutorial + "..."));
26-
build_1.default(tutorial, output);
27+
var built = build_1.default(tutorial, output);
28+
if (!built) {
29+
result_1.fail();
30+
}
2731
}
2832
else if (program.create) {
2933
var packageName = program.args[0];
30-
create_1.default(packageName);
34+
var created = create_1.default(packageName);
35+
if (!created) {
36+
result_1.fail();
37+
}
3138
}
3239
else if (program.search) {
3340
var query = program.args[0];
@@ -43,5 +50,4 @@ else if (program.publish) {
4350
else {
4451
program.help();
4552
}
46-
process.stdout.write(chalk_1.green(' ✓\n'));
47-
process.exit(0);
53+
result_1.success();

lib/create/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
var validate_1 = require('./validate');
3+
var write_demo_1 = require('./write-demo');
4+
var build_1 = require('../build/build');
5+
function create(name) {
6+
if (!validate_1.validatePackageName(name)) {
7+
return false;
8+
}
9+
process.stdout.write("Creating demo tutorial \"coderoad-" + name + "\"...");
10+
return Promise.all([
11+
write_demo_1.createPackageJson(name),
12+
write_demo_1.createTutorialMd()
13+
]).then(function () {
14+
build_1.default('tutorial/tutorial.md', 'coderoad.json');
15+
return true;
16+
});
17+
}
18+
Object.defineProperty(exports, "__esModule", { value: true });
19+
exports.default = create;

lib/create/validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function validatePackageName(name) {
1818
if (!validated.errors && !validated.warnings) {
1919
console.log(chalk_1.red("\n Invalid package name. Try using kebab-case.\n > coderoad create " + lodash_kebabcase_1.default(name) + "\n "));
2020
}
21-
process.exit(1);
21+
return false;
2222
}
23+
return true;
2324
}
2425
exports.validatePackageName = validatePackageName;

lib/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var build_1 = require('./build');
3+
exports.build = build_1.default;
4+
var create_1 = require('./create');
5+
exports.create = create_1.default;
6+
var publish_1 = require('./publish');
7+
exports.publish = publish_1.default;
8+
var search_1 = require('./search');
9+
exports.search = search_1.default;
10+
var tutorials_1 = require('./tutorials');
11+
exports.tutorials = tutorials_1.default;
12+
var update_1 = require('./update');
13+
exports.update = update_1.default;

lib/publish/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var chalk_1 = require('chalk');
3+
var validate_1 = require('./validate');
4+
function publish(version) {
5+
validate_1.default(version);
6+
process.stdout.write("Publishing package version \"" + version + "\"... \n");
7+
console.log(chalk_1.yellow('Publish feature not implemented yet.\n'));
8+
console.log('To publish your tutorial package follow these instructions: \n');
9+
console.log("Setup a git repo and tag your version:\n > git init\n > git add -A\n > git commit -m \"$your-commit-message$\"\n > git tag v" + version + "\n > git add remote origin http://github.com/$your-github-id$/$your-package-name$\n > git push -u --tags\n ");
10+
console.log("\n Publish your package to npm:\n > npm publish\n ");
11+
}
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.default = publish;

lib/result.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
var chalk_1 = require('chalk');
3+
exports.success = function () {
4+
process.stdout.write(chalk_1.green(' ✓\n'));
5+
process.exit(0);
6+
};
7+
exports.fail = function () {
8+
process.stdout.write(chalk_1.red(' ✗\n'));
9+
process.exit(1);
10+
};

lib/search/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
var chalk_1 = require('chalk');
3+
var validate_1 = require('./validate');
4+
function search(query) {
5+
validate_1.validateQuery(query);
6+
console.log("Searching for \"coderoad-" + query + "\"...");
7+
console.log(chalk_1.yellow('Search feature not fully implemented yet.\n'));
8+
console.log('To search for tutorials follow the instructions below: \n');
9+
console.log("Search tutorials on npm:\n > npm search coderoad tutorial\n ");
10+
}
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.default = search;

lib/tutorials/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
var chalk_1 = require('chalk');
3+
function tutorials() {
4+
console.log("List of tutorial packages in this directory...\n");
5+
console.log(chalk_1.yellow('This feature is not yet implemented'));
6+
}
7+
Object.defineProperty(exports, "__esModule", { value: true });
8+
exports.default = tutorials;

0 commit comments

Comments
 (0)