Skip to content

Commit 787ab4b

Browse files
committed
refactor
1 parent c7139ba commit 787ab4b

File tree

9 files changed

+53
-72
lines changed

9 files changed

+53
-72
lines changed

lib/build/lint.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ function lintOutput(json) {
7575
});
7676
if (warningKeys.length > 0) {
7777
warningKeys.forEach(function (w) {
78-
console.log(chalk_1.yellow("Warning: " + w.warning + ": ", w.location));
78+
console.log(chalk_1.yellow("\nWarning: " + w.warning + ": ", w.location));
7979
});
8080
}
8181
if (invalidKeys.length > 0) {
8282
invalidKeys.forEach(function (e) {
83-
console.log(chalk_1.red("Error: " + e.error + ": ", e.location));
83+
console.log(chalk_1.red("\nError: " + e.error + ": ", e.location));
8484
});
8585
process.exit(1);
8686
}
@@ -90,22 +90,22 @@ function isValidJSON(text) {
9090
if (!/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
9191
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
9292
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
93-
console.log(chalk_1.red("\n Something went wrong. Build did not output valid JSON.\n "));
93+
console.log(chalk_1.red('\nSomething went wrong. Build did not output valid JSON.'));
9494
process.exit(1);
9595
}
9696
}
9797
exports.isValidJSON = isValidJSON;
9898
function hasTutorialInfo(json) {
9999
var validTitle = json.info.title.length > 0, validDescription = json.info.description.length > 0;
100100
if (!(validTitle && validDescription)) {
101-
console.log(chalk_1.red("\n Your tutorial is missing basic project information. Check the project title & description.\n "));
101+
console.log(chalk_1.red('\nYour tutorial is missing basic project information. Check the project title & description.'));
102102
process.exit(1);
103103
}
104104
}
105105
exports.hasTutorialInfo = hasTutorialInfo;
106106
function hasPage(json) {
107107
if (!(json.chapters[0].pages.length > 0 && !!json.chapters[0].pages[0].title)) {
108-
console.log(chalk_1.red("\n Your tutorial requires at least one page.\n "));
108+
console.log(chalk_1.red('\nYour tutorial requires at least one page.'));
109109
process.exit(1);
110110
}
111111
}

lib/cli.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,30 @@ program
1919
.option('-r, --run', 'run tutorial')
2020
.parse(process.argv);
2121
update_1.checkForUpdate();
22-
if (!program.args.length &&
23-
!program.build && !program.tutorials && !program.run) {
24-
program.help();
25-
}
26-
else {
27-
if (program.build) {
22+
switch (true) {
23+
case program.build:
2824
var tutorial = program.args[0] || 'tutorial/tutorial.md';
2925
var output = 'coderoad.json';
3026
process.stdout.write(chalk_1.grey("building coderoad.json for " + tutorial + "..."));
3127
build_1.default(tutorial, output);
32-
}
33-
if (program.create) {
28+
break;
29+
case program.create:
3430
var packageName = program.args[0];
3531
create_1.default(packageName);
36-
}
37-
if (program.search) {
32+
break;
33+
case program.search:
3834
var query = program.args[0];
3935
search_1.default(query);
40-
}
41-
if (program.tutorials) {
36+
break;
37+
case program.tutorials:
4238
tutorials_1.default();
43-
}
44-
if (program.publish) {
39+
break;
40+
case program.publish:
4541
var version = program.args[0];
4642
publish_1.default(version);
47-
}
48-
process.stdout.write(chalk_1.green(' ✓\n'));
49-
process.exit(0);
43+
break;
44+
default:
45+
program.help();
5046
}
47+
process.stdout.write(chalk_1.green(' ✓\n'));
48+
process.exit(0);

lib/create/validate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ function validatePackageName(name) {
77
if (!validated.validForNewPackages || !validated.validForOldPackages) {
88
if (validated.errors) {
99
validated.errors.forEach(function (error) {
10-
console.log(chalk_1.red('Package ' + error));
10+
console.log(chalk_1.red('\nPackage ' + error));
1111
});
1212
}
1313
if (validated.warnings) {
1414
validated.warnings.forEach(function (warning) {
15-
console.log(chalk_1.yellow('Package ' + warning));
15+
console.log(chalk_1.yellow('\nPackage ' + warning));
1616
});
1717
}
1818
if (!validated.errors && !validated.warnings) {

lib/publish/publish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function publish(version) {
77
console.log(chalk_1.yellow('Publish feature not implemented yet.\n'));
88
console.log('To publish your tutorial package follow these instructions: \n');
99
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("Publish your package to npm:\n > npm publish\n ");
10+
console.log("\n Publish your package to npm:\n > npm publish\n ");
1111
}
1212
Object.defineProperty(exports, "__esModule", { value: true });
1313
exports.default = publish;

src/build/lint.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ export function lintOutput(json: CR.Output): void {
8585

8686
if (warningKeys.length > 0) {
8787
warningKeys.forEach((w) => {
88-
console.log(yellow(`Warning: ${w.warning}: `, w.location));
88+
console.log(yellow(`\nWarning: ${w.warning}: `, w.location));
8989
});
9090
}
9191

9292
if (invalidKeys.length > 0) {
9393
invalidKeys.forEach((e) => {
94-
console.log(red(`Error: ${e.error}: `, e.location));
94+
console.log(red(`\nError: ${e.error}: `, e.location));
9595
});
9696

9797
process.exit(1); // fail
@@ -102,9 +102,7 @@ export function isValidJSON(text: string): void {
102102
if (!/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
103103
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
104104
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
105-
console.log(red(`
106-
Something went wrong. Build did not output valid JSON.
107-
`));
105+
console.log(red('\nSomething went wrong. Build did not output valid JSON.'));
108106
process.exit(1); // fail
109107
}
110108
}
@@ -113,18 +111,14 @@ export function hasTutorialInfo(json: CR.Output): void {
113111
let validTitle = json.info.title.length > 0,
114112
validDescription = json.info.description.length > 0;
115113
if (!(validTitle && validDescription)) {
116-
console.log(red(`
117-
Your tutorial is missing basic project information. Check the project title & description.
118-
`));
114+
console.log(red('\nYour tutorial is missing basic project information. Check the project title & description.'));
119115
process.exit(1); // fail
120116
}
121117
}
122118

123119
export function hasPage(json: CR.Output): void {
124120
if (!(json.chapters[0].pages.length > 0 && !!json.chapters[0].pages[0].title)) {
125-
console.log(red(`
126-
Your tutorial requires at least one page.
127-
`));
121+
console.log(red('\nYour tutorial requires at least one page.'));
128122
process.exit(1); // fail
129123
}
130124
}

src/cli.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,49 @@ import {checkForUpdate} from './update/update';
1212
program
1313
.version('0.3.27')
1414
.usage('[options] <keywords>')
15-
.option('-b, --build [path/to/tutorial.md]', 'tutorial markdown file', /^.+\.md$/i)
15+
.option('-b, --build [path/to/tutorial.md]',
16+
'tutorial markdown file', /^.+\.md$/i)
1617
.option('-c, --create [name]', 'tutorial name')
17-
.option('-p, --publish [version]', 'publish tutorial to npm with new version number')
18+
.option('-p, --publish [version]',
19+
'publish tutorial to npm with new version number')
1820
.option('-t, --tutorials', 'list of tutorial packages')
1921
.option('-s, --search [query]', 'search for tutorial package')
2022
.option('-r, --run', 'run tutorial')
21-
2223
.parse(process.argv);
2324

2425
checkForUpdate();
2526

26-
if (!program.args.length &&
27-
!program.build && !program.tutorials && !program.run) {
28-
program.help();
29-
} else {
30-
31-
// build
32-
if (program.build) {
27+
switch (true) {
28+
case program.build:
3329
const tutorial = program.args[0] || 'tutorial/tutorial.md';
3430
const output = 'coderoad.json';
3531
process.stdout.write(grey(`building coderoad.json for ${tutorial}...`));
3632
build(tutorial, output);
37-
}
33+
break;
3834

39-
// create
40-
if (program.create) {
35+
case program.create:
4136
const packageName = program.args[0];
4237
create(packageName);
43-
}
38+
break;
4439

45-
if (program.search) {
40+
case program.search:
4641
const query = program.args[0];
4742
search(query);
48-
}
43+
break;
4944

50-
if (program.tutorials) {
45+
case program.tutorials:
5146
tutorials();
52-
}
47+
break;
5348

54-
if (program.publish) {
49+
case program.publish:
5550
const version = program.args[0];
5651
publish(version);
57-
}
52+
break;
5853

59-
process.stdout.write(green(' ✓\n'));
60-
61-
process.exit(0);
54+
default:
55+
program.help();
6256
}
57+
58+
// exit
59+
process.stdout.write(green(' ✓\n'));
60+
process.exit(0);

src/create/validate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export function validatePackageName(name: string): void {
77
if (!validated.validForNewPackages || !validated.validForOldPackages) {
88
if (validated.errors) {
99
validated.errors.forEach((error) => {
10-
console.log(red('Package ' + error));
10+
console.log(red('\nPackage ' + error));
1111
});
1212
}
1313
if (validated.warnings) {
1414
validated.warnings.forEach((warning) => {
15-
console.log(yellow('Package ' + warning));
15+
console.log(yellow('\nPackage ' + warning));
1616
});
1717
}
1818
if (!validated.errors && !validated.warnings) {

src/publish/publish.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export default function publish(version: string): void {
1414
> git add remote origin http://github.com/$your-github-id$/$your-package-name$
1515
> git push -u --tags
1616
`);
17-
console.log(`Publish your package to npm:
17+
console.log(`
18+
Publish your package to npm:
1819
> npm publish
1920
`);
2021
}

src/typings/globals.d.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
declare var program: {
2-
build: any;
3-
create: any;
4-
docs: any;
5-
list: any;
6-
publish: any;
7-
run: any;
8-
search: any;
9-
};
10-
111
interface PackageJson {
122
name: string;
133
version: string;

0 commit comments

Comments
 (0)