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;

lib/update/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
function checkForUpdate() {
3+
}
4+
Object.defineProperty(exports, "__esModule", { value: true });
5+
exports.default = checkForUpdate;

lib/write.js

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

src/build/build.ts renamed to src/build/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ function parseAndBuild(lines: string[]): CR.Output {
1919
return info(result, lines, index);
2020
}
2121

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

2628
// Read
2729
let lines: string[] = readFileSync(filePath, 'utf8').split('\n');
@@ -33,4 +35,5 @@ export default function build(filePath: string, output = './coderoad.json'): voi
3335
writeFileSync(output, result, 'utf8');
3436
}
3537
createReadme();
38+
return true;
3639
}

src/build/validators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as chalk from 'chalk';
22
import {isValidJSON, hasTutorialInfo, hasPage, lintOutput} from './lint';
33

4-
export function filePath(filePath: string): void {
4+
export function filePath(filePath: string): boolean {
55
if (!filePath) {
66
console.log(chalk.red(`
77
Pass in a path to your .md file
88
> coderoad build "./src/tutorial.md"
99
`));
10-
process.exit(1); // fail
10+
return false; // fail
1111
}
1212
// regex .md
1313
}

src/cli.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#! /usr/bin/env node
22

33
import * as program from 'commander';
4-
import {grey, green} from 'chalk';
4+
import {grey} from 'chalk';
5+
import {success, fail} from './result';
56

6-
import build from './build/build';
7-
import create from './create/create';
8-
import search from './search/search';
9-
import tutorials from './tutorials/tutorials';
10-
import publish from './publish/publish';
11-
import {checkForUpdate} from './update/update';
7+
import build from './build';
8+
import create from './create';
9+
import search from './search';
10+
import tutorials from './tutorials';
11+
import publish from './publish';
12+
import checkForUpdate from './update';
1213

1314
program
1415
.version('0.6.0')
@@ -29,11 +30,17 @@ if (program.build) {
2930
const tutorial = program.args[0] || 'tutorial/tutorial.md';
3031
const output = 'coderoad.json';
3132
process.stdout.write(grey(`building coderoad.json for ${tutorial}...`));
32-
build(tutorial, output);
33+
const built = build(tutorial, output);
34+
if (!built) {
35+
fail();
36+
}
3337

3438
} else if (program.create) {
3539
const packageName = program.args[0];
36-
create(packageName);
40+
const created = create(packageName);
41+
if (!created) {
42+
fail();
43+
}
3744

3845
} else if (program.search) {
3946
const query = program.args[0];
@@ -51,5 +58,4 @@ if (program.build) {
5158
}
5259

5360
// success! exit
54-
process.stdout.write(green(' ✓\n'));
55-
process.exit(0);
61+
success();

src/create/create.ts renamed to src/create/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ import {validatePackageName} from './validate';
22
import {createPackageJson, createTutorialMd} from './write-demo';
33
import build from '../build/build';
44

5-
export default function create(name: string, dir: string): void {
5+
export default function create(name: string): boolean | Promise<boolean> {
66
// check
7-
validatePackageName(name);
7+
if (!validatePackageName(name)) {
8+
return false;
9+
}
810

911
// continue
1012
process.stdout.write(`Creating demo tutorial "coderoad-${name}"...`);
1113

1214
// npm package
13-
Promise.all([
15+
return Promise.all([
1416
createPackageJson(name),
1517
createTutorialMd()
1618
]).then(() => {
1719
build('tutorial/tutorial.md', 'coderoad.json');
20+
return true;
1821
});
19-
2022
}

src/create/validate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {red, yellow} from 'chalk';
22
import * as validateNpm from 'validate-npm-package-name';
33
import kebabCase from 'lodash.kebabcase';
44

5-
export function validatePackageName(name: string): void {
5+
export function validatePackageName(name: string): boolean {
66
let validated: Validated = validateNpm(name);
77
if (!validated.validForNewPackages || !validated.validForOldPackages) {
88
if (validated.errors) {
@@ -21,6 +21,7 @@ export function validatePackageName(name: string): void {
2121
> coderoad create ${kebabCase(name) }
2222
`));
2323
}
24-
process.exit(1);
24+
return false;
2525
}
26+
return true;
2627
}

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export {default as build} from './build';
2+
export {default as create} from './create';
3+
export {default as publish} from './publish';
4+
export {default as search} from './search';
5+
export {default as tutorials} from './tutorials';
6+
export {default as update} from './update';
File renamed without changes.

src/result.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {green, red} from 'chalk';
2+
3+
export const success = () => {
4+
process.stdout.write(green(' ✓\n'));
5+
process.exit(0);
6+
};
7+
8+
export const fail = () => {
9+
process.stdout.write(red(' ✗\n'));
10+
process.exit(1); // fail
11+
};
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)