Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite98e008

Browse files
committed
validate coderoad.json and package.json
1 parentf72c43f commite98e008

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+
varnode_file_exists_1=require('node-file-exists');
3+
varfs_1=require('fs');
4+
varpath_1=require('path');
5+
functiongetPackageJson(dir,file){
6+
varpathToFile=path_1.resolve(dir,file);
7+
if(!node_file_exists_1.default(pathToFile)){
8+
returnnull;
9+
}
10+
returnJSON.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
varnode_file_exists_1=require('node-file-exists');
33
varfs_1=require('fs');
44
varpath_1=require('path');
5-
functiongetPackageJson(dir){
6-
varpathToPJ=path_1.resolve(dir,'package.json');
7-
if(!node_file_exists_1.default(pathToPJ)){
5+
functiongetPackageJson(dir,file){
6+
varpathToFile=path_1.resolve(dir,file);
7+
if(!node_file_exists_1.default(pathToFile)){
88
returnnull;
99
}
10-
varpj=fs_1.readFileSync(pathToPJ,'utf8');
11-
returnJSON.parse(pj);
10+
returnJSON.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
varchalk_1=require('chalk');
33
varfind_tutorials_1=require('./find-tutorials');
4-
varpackageJson_1=require('../get/packageJson');
4+
vargetJson_1=require('../utils/getJson');
55
functiontutorials(dir){
6-
varpj=packageJson_1.default(dir);
6+
varpj=getJson_1.default(dir,'package.json');
77
if(!pj){
88
console.log(chalk_1.red("No package.json available"));
99
returnnull;

‎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+
varnode_file_exists_1=require('node-file-exists');
3+
varfs_1=require('fs');
4+
varpath_1=require('path');
5+
functiongetJson(dir,file){
6+
varpathToFile=path_1.resolve(dir,file);
7+
if(!node_file_exists_1.default(pathToFile)){
8+
returnnull;
9+
}
10+
returnJSON.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+
varvalidations_1=require('./validations');
23
varvalidKeys={
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+
varvalidateKeys={
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){returnArray.isArray(tests)&&tests.length&&tests.every(function(test){returntypeoftest==='string'&&test.length;});},
30+
}]
31+
};
832
functionvalidateCoderoadJson(json){
933
varerrors=[];
1034
varwarnings=[];
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
varinfoKeys=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:'ismissing',
51+
name:"info."+key,
52+
msg:'isnot 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
vartaskKeys=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
varchalk_1=require('chalk');
33
varpackageJson_1=require('./packageJson');
4-
varpackageJson_2=require('../get/packageJson');
4+
varcoderoadJson_1=require('./coderoadJson');
5+
vargetJson_1=require('../utils/getJson');
6+
varvalidation_messages_1=require('./validation-messages');
57
functionvalidate(){
6-
varpj=packageJson_2.default(process.cwd());
7-
if(!pj){
8-
console.log(chalk_1.red('Error: No package.json.'));
8+
varpj=getJson_1.default(process.cwd(),'package.json');
9+
varcj=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
returnfalse;
1018
}
11-
varvalidation=packageJson_1.default(pj);
12-
getValidationMessages('Warning',validation.warnings,chalk_1.yellow);
13-
returngetValidationMessages('Error',validation.errors,chalk_1.red);
19+
varpjValidation=packageJson_1.default(pj);
20+
varcjValidation=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+
returnpjValidation.errors.length===0&&cjValidation.errors.length===0;
1432
}
1533
Object.defineProperty(exports,"__esModule",{value:true});
1634
exports.default=validate;
17-
functiongetValidationMessages(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-
returnfalse;
23-
}
24-
returntrue;
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+
varvalidations_1=require('./validations');
23
varpJKeys=[{
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){returntypeofdesc==='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){returntypeofauthor==='string'&&author.length>2;},
30+
validate:validations_1.isString,
3031
msg:'must have an author name and optional email',
3132
example:'"Shawn McKay <me@email.com> (http://blog)"',
3233
},{
@@ -47,20 +48,20 @@ var pJKeys = [{
4748
},{
4849
name:'language',
4950
config:true,
50-
validate:function(lang){returntypeoflang==='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){returntypeofrunner==='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-
returntypeofrepo==='string'&&!!repo.length||
64+
returnvalidations_1.isString||
6465
typeofrepo==='object'&&repo.hasOwnProperty('type')
6566
&&typeofrepo.type==='string'&&
6667
repo.hasOwnProperty('url')&&typeofrepo.url==='string';
@@ -77,7 +78,7 @@ var pJKeys = [{
7778
},{
7879
name:'license',
7980
optional:true,
80-
validate:function(license){returntypeoflicense==='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+
functiongetValidationMessages(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+
returnfalse;
8+
}
9+
returntrue;
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){returnstr&&typeofstr==='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
importfindTutorialsfrom'./find-tutorials';
3-
importgetPackageJsonfrom'../get/packageJson';
3+
importgetJsonfrom'../utils/getJson';
44

55
exportdefaultfunctiontutorials(dir:string):string[]{
6-
constpj:PackageJson=getPackageJson(dir);
6+
constpj: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+
importfileExistsfrom'node-file-exists';
2+
import{readFileSync}from'fs';
3+
import{resolve}from'path';
4+
5+
exportdefaultfunctiongetJson(dir:string,file:string):Object{
6+
constpathToFile=resolve(dir,file);
7+
if(!fileExists(pathToFile)){returnnull;}
8+
returnJSON.parse(readFileSync(pathToFile,'utf8'));
9+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp