Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
feat: config validater
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Jan 7, 2018
1 parent 28d350b commit cc587b2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 35 deletions.
87 changes: 71 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"prop-types": "^15.6.0"
}
}
6 changes: 3 additions & 3 deletions src/config/configMgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ let currConfig = {};
* @type {Object}
*/

const defaultOptions = {
const defaultConfig = {
model: {
jsonPath: '', // string
hHeadPos: 0.5, // horizontalHeadPos
vHeadPos: 0.618, // verticalHeadPos
myDefine: {},
myDefine: [],
},
display: {
AA: 2, // antialiasing Grade 抗锯齿等级
Expand Down Expand Up @@ -73,7 +73,7 @@ function configApplyer(userConfig){
});
}

currConfig = _.defaultsDeep(userConfig, defaultOptions);
currConfig = _.defaultsDeep(userConfig, defaultConfig);

}

Expand Down
56 changes: 41 additions & 15 deletions src/config/configValidater.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,51 @@
'use strict';


import _ from 'lodash';
let PropTypes = require('prop-types');

const configPropTypes = {
model: PropTypes.shape({
jsonPath: PropTypes.string,
hHeadPos: PropTypes.number,
vHeadPos: PropTypes.number,
myDefine: PropTypes.array,
}),
display: PropTypes.shape({
AA: PropTypes.number,
widght: PropTypes.number,
height: PropTypes.number,
scale: PropTypes.number,
position: PropTypes.oneOf(['left', 'right']),
hOffset: PropTypes.number, // horizontalOffset
vOffset: PropTypes.number, // verticalOffset
}),
mobile: PropTypes.shape({
show: PropTypes.bool,
scale: PropTypes.number,
motion: PropTypes.bool,
}),
name: PropTypes.shape({
canvas: PropTypes.string,
div: PropTypes.string,
}),
react: PropTypes.shape({
opacityDefault: PropTypes.number,
opacityOnHover: PropTypes.number,
myFunc: PropTypes.func,
}),
debug: PropTypes.shape({
log: PropTypes.bool,
mouseLog: PropTypes.bool,
mouseFunc: PropTypes.func,
}),
}

function configValidater(userConfig){

if (process.env.NODE_ENV === 'development') console.log('config: validating config...');

if(_.has(userConfig, 'model')){
if(_.has(userConfig.model, 'jsonPath')){
if(!_.isString(userConfig.model.jsonPath)){
throw 'userConfig.model.jsonPath should be a string.';
}
}
if(_.has(userConfig.model, 'hHeadPos')){

}
}
PropTypes.checkPropTypes(configPropTypes, userConfig, 'config', 'Live2D-widget');

}

export {
configValidater,
module.exports = {
configValidater: configValidater,
}
2 changes: 1 addition & 1 deletion src/wpPublicPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getCurrentPath(){
// IE 10 +, Safari and Opera 9

// console.log('Stage2');
// console.log(e);
console.log(e);

let stack = e.stack || e.sourceURL || e.stacktrace,
rExtractUri = /(file:\/{3}\w:)(\/[^\/]*){3}\//;
Expand Down

0 comments on commit cc587b2

Please sign in to comment.