Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e98e008

Browse files
committed
validate coderoad.json and package.json
1 parent f72c43f commit e98e008

19 files changed

+270
-86
lines changed

lib/get/coderoadJson.js

Whitespace-only changes.

lib/get/file.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var node_file_exists_1 = require('node-file-exists');
3+
var fs_1 = require('fs');
4+
var path_1 = require('path');
5+
function getPackageJson(dir, file) {
6+
var pathToFile = path_1.resolve(dir, file);
7+
if (!node_file_exists_1.default(pathToFile)) {
8+
return null;
9+
}
10+
return JSON.parse(fs_1.readFileSync(pathToFile, 'utf8'));
11+
}
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.default = getPackageJson;

lib/get/packageJson.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
var node_file_exists_1 = require('node-file-exists');
33
var fs_1 = require('fs');
44
var path_1 = require('path');
5-
function getPackageJson(dir) {
6-
var pathToPJ = path_1.resolve(dir, 'package.json');
7-
if (!node_file_exists_1.default(pathToPJ)) {
5+
function getPackageJson(dir, file) {
6+
var pathToFile = path_1.resolve(dir, file);
7+
if (!node_file_exists_1.default(pathToFile)) {
88
return null;
99
}
10-
var pj = fs_1.readFileSync(pathToPJ, 'utf8');
11-
return JSON.parse(pj);
10+
return JSON.parse(fs_1.readFileSync(pathToFile, 'utf8'));
1211
}
1312
Object.defineProperty(exports, "__esModule", { value: true });
1413
exports.default = getPackageJson;

lib/tutorials/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22
var chalk_1 = require('chalk');
33
var find_tutorials_1 = require('./find-tutorials');
4-
var packageJson_1 = require('../get/packageJson');
4+
var getJson_1 = require('../utils/getJson');
55
function tutorials(dir) {
6-
var pj = packageJson_1.default(dir);
6+
var pj = getJson_1.default(dir, 'package.json');
77
if (!pj) {
88
console.log(chalk_1.red("No package.json available"));
99
return null;

lib/utils/getJson.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var node_file_exists_1 = require('node-file-exists');
3+
var fs_1 = require('fs');
4+
var path_1 = require('path');
5+
function getJson(dir, file) {
6+
var pathToFile = path_1.resolve(dir, file);
7+
if (!node_file_exists_1.default(pathToFile)) {
8+
return null;
9+
}
10+
return JSON.parse(fs_1.readFileSync(pathToFile, 'utf8'));
11+
}
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.default = getJson;

lib/validate/coderoadJson.js

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,66 @@
11
"use strict";
2+
var validations_1 = require('./validations');
23
var validKeys = {
34
info: ['title', 'description'],
45
page: ['title', 'description', 'onPageComplete', 'tasks', 'video', 'link'],
56
task: ['description', 'tests', 'actions', 'hints'],
67
actions: ['open', 'set', 'insert'],
78
};
9+
var validateKeys = {
10+
info: [{
11+
name: 'title',
12+
validation: validations_1.isString,
13+
}, {
14+
name: 'description',
15+
validation: validations_1.isString,
16+
}],
17+
page: [{
18+
name: 'title',
19+
validation: validations_1.isString,
20+
}, {
21+
name: 'description',
22+
validation: validations_1.isString,
23+
}],
24+
task: [{
25+
name: 'description',
26+
validation: validations_1.isString,
27+
}, {
28+
name: 'tests',
29+
validation: function (tests) { return Array.isArray(tests) && tests.length && tests.every(function (test) { return typeof test === 'string' && test.length; }); },
30+
}]
31+
};
832
function validateCoderoadJson(json) {
933
var errors = [];
1034
var warnings = [];
1135
try {
12-
json.parse(json);
36+
JSON.parse(JSON.stringify(json));
1337
}
1438
catch (e) {
1539
errors.push({
16-
name: 'json',
17-
msg: 'is invalid'
40+
name: 'coderoad.json',
41+
msg: 'has an error.'
1842
});
1943
return {
2044
errors: errors, warnings: warnings
2145
};
2246
}
2347
var infoKeys = Object.keys(json.info);
2448
infoKeys.forEach(function (key) {
25-
if (validKeys.info.indexOf(key) < 0) {
49+
if (!validKeys.info.includes(key)) {
2650
errors.push({
27-
name: key,
28-
msg: 'is missing',
51+
name: "info." + key,
52+
msg: 'is not a valid key on info',
2953
location: "info." + key,
54+
example: "Did you mean: " + validKeys.info.join(', ')
55+
});
56+
}
57+
});
58+
validateKeys.info.forEach(function (key) {
59+
if (!key.validation(json.info[key.name])) {
60+
errors.push({
61+
name: "info." + key.name,
62+
msg: 'is not complete',
63+
example: "Expected a string, but got: " + json.info[key.name],
3064
});
3165
}
3266
});
@@ -42,6 +76,15 @@ function validateCoderoadJson(json) {
4276
});
4377
}
4478
});
79+
validateKeys.page.forEach(function (key) {
80+
if (!key.validation(page[key.name])) {
81+
errors.push({
82+
name: "pages[" + pIndex + "]." + key.name,
83+
msg: 'is not complete',
84+
example: "Expected a string, but got: " + page[key.name]
85+
});
86+
}
87+
});
4588
if (page.tasks && page.tasks.length > 0) {
4689
page.tasks.forEach(function (task, tIndex) {
4790
var taskKeys = Object.keys(task);
@@ -61,6 +104,15 @@ function validateCoderoadJson(json) {
61104
location: "pages[" + pIndex + "].tasks[" + tIndex + "]",
62105
});
63106
}
107+
validateKeys.task.forEach(function (key) {
108+
if (!key.validation(task[key.name])) {
109+
errors.push({
110+
name: "pages[" + pIndex + "].tasks[" + tIndex + "]." + key.name,
111+
msg: 'is not complete',
112+
example: "Expected a string, but got: " + page[key.name]
113+
});
114+
}
115+
});
64116
});
65117
}
66118
else {

lib/validate/index.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
"use strict";
22
var chalk_1 = require('chalk');
33
var packageJson_1 = require('./packageJson');
4-
var packageJson_2 = require('../get/packageJson');
4+
var coderoadJson_1 = require('./coderoadJson');
5+
var getJson_1 = require('../utils/getJson');
6+
var validation_messages_1 = require('./validation-messages');
57
function validate() {
6-
var pj = packageJson_2.default(process.cwd());
7-
if (!pj) {
8-
console.log(chalk_1.red('Error: No package.json.'));
8+
var pj = getJson_1.default(process.cwd(), 'package.json');
9+
var cj = getJson_1.default(process.cwd(), 'coderoad.json');
10+
if (!pj || !cj) {
11+
if (!pj) {
12+
console.log(chalk_1.red('Error: No package.json.'));
13+
}
14+
if (!cj) {
15+
console.log(chalk_1.red('Error: No coderoad.json.'));
16+
}
917
return false;
1018
}
11-
var validation = packageJson_1.default(pj);
12-
getValidationMessages('Warning', validation.warnings, chalk_1.yellow);
13-
return getValidationMessages('Error', validation.errors, chalk_1.red);
19+
var pjValidation = packageJson_1.default(pj);
20+
var cjValidation = coderoadJson_1.default(cj);
21+
if (pjValidation.errors.length || pjValidation.warnings.length) {
22+
console.log(" package.json issues:");
23+
validation_messages_1.default('Warning', pjValidation.warnings, chalk_1.yellow);
24+
validation_messages_1.default('Error', pjValidation.errors, chalk_1.red);
25+
}
26+
if (cjValidation.errors.length || cjValidation.warnings.length) {
27+
console.log(" coderoad.json issues:");
28+
validation_messages_1.default('Warning', cjValidation.warnings, chalk_1.yellow);
29+
validation_messages_1.default('Error', cjValidation.errors, chalk_1.red);
30+
}
31+
return pjValidation.errors.length === 0 && cjValidation.errors.length === 0;
1432
}
1533
Object.defineProperty(exports, "__esModule", { value: true });
1634
exports.default = validate;
17-
function getValidationMessages(title, validation, color) {
18-
if (validation && validation.length) {
19-
validation.forEach(function (e, index) {
20-
console.log(color((index + 1) + ". " + title + ": \"" + e.name + "\" " + e.msg + ".\n Example: " + e.example + "\n"));
21-
});
22-
return false;
23-
}
24-
return true;
25-
}

lib/validate/packageJson.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use strict";
2+
var validations_1 = require('./validations');
23
var pJKeys = [{
34
name: 'name',
45
validate: function (name) { return !!name.match(/^coderoad-[A-Za-z0-9\-]+$/); },
@@ -16,7 +17,7 @@ var pJKeys = [{
1617
example: '"coderoad.json"',
1718
}, {
1819
name: 'description',
19-
validate: function (desc) { return typeof desc === 'string' && desc.length > 3; },
20+
validate: validations_1.isString,
2021
msg: 'must be long enough to describe a package',
2122
example: '"CodeRoad tutorial on ES2015 new features."'
2223
}, {
@@ -26,7 +27,7 @@ var pJKeys = [{
2627
example: '[\n"coderoad",\n"tutorial",\n"js"\n]',
2728
}, {
2829
name: 'author',
29-
validate: function (author) { return typeof author === 'string' && author.length > 2; },
30+
validate: validations_1.isString,
3031
msg: 'must have an author name and optional email',
3132
example: '"Shawn McKay <[email protected]> (http://blog)"',
3233
}, {
@@ -47,20 +48,20 @@ var pJKeys = [{
4748
}, {
4849
name: 'language',
4950
config: true,
50-
validate: function (lang) { return typeof lang === 'string' && !!lang.length; },
51+
validate: validations_1.isString,
5152
msg: 'must specify a programming language',
5253
example: '"JS"',
5354
}, {
5455
name: 'runner',
5556
config: true,
56-
validate: function (runner) { return typeof runner === 'string' && !!runner.length; },
57+
validate: validations_1.isString,
5758
msg: 'must specify a test runner',
5859
example: '"mocha-coderoad"',
5960
}, {
6061
name: 'repository',
6162
optional: true,
6263
validate: function (repo) {
63-
return typeof repo === 'string' && !!repo.length ||
64+
return validations_1.isString ||
6465
typeof repo === 'object' && repo.hasOwnProperty('type')
6566
&& typeof repo.type === 'string' &&
6667
repo.hasOwnProperty('url') && typeof repo.url === 'string';
@@ -77,7 +78,7 @@ var pJKeys = [{
7778
}, {
7879
name: 'license',
7980
optional: true,
80-
validate: function (license) { return typeof license === 'string' && !!license.length; },
81+
validate: validations_1.isString,
8182
msg: 'should have a valid license (ex: MIT, ISC, etc.)',
8283
example: '"MIT"',
8384
}];

lib/validate/validation-messages.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
function getValidationMessages(title, validation, color) {
3+
if (validation && validation.length) {
4+
validation.forEach(function (e, index) {
5+
console.log(color((index + 1) + ". " + title + ": \"" + e.name + "\" " + e.msg + ".\n Example: " + e.example + "\n"));
6+
});
7+
return false;
8+
}
9+
return true;
10+
}
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.default = getValidationMessages;

lib/validate/validations.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
exports.isString = function (str) { return str && typeof str === 'string' && str.length; };

src/get/packageJson.ts

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

src/tutorials/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {red} from 'chalk';
22
import findTutorials from './find-tutorials';
3-
import getPackageJson from '../get/packageJson';
3+
import getJson from '../utils/getJson';
44

55
export default function tutorials(dir: string): string[] {
6-
const pj: PackageJson = getPackageJson(dir);
6+
const pj: PackageJson = getJson(dir, 'package.json');
77

88
if (!pj) {
99
console.log(red(`No package.json available`));

src/utils/getJson.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fileExists from 'node-file-exists';
2+
import {readFileSync} from 'fs';
3+
import {resolve} from 'path';
4+
5+
export default function getJson(dir: string, file: string): Object {
6+
const pathToFile = resolve(dir, file);
7+
if (!fileExists(pathToFile)) { return null; }
8+
return JSON.parse(readFileSync(pathToFile, 'utf8'));
9+
}

0 commit comments

Comments
 (0)