feat: throw error if there is wrong field on siteConfig.js

This commit is contained in:
endiliey 2018-08-29 23:57:26 +08:00
parent ef17da741a
commit 9965eec798
4 changed files with 41 additions and 4 deletions

View file

@ -3,7 +3,7 @@ const chalk = require('chalk');
const escapeHtml = require('escape-html');
export default (str, rawLang) => {
if (rawLang === 'text') {
if (rawLang === 'text' || !rawLang) {
return escapeHtml(str);
}
const lang = rawLang.toLowerCase();

View file

@ -18,9 +18,27 @@ module.exports = function loadConfig(siteDir, deleteCache = true) {
'projectName',
'baseUrl'
];
const optionalFields = [
'customDocsPath',
'themePath',
'highlight',
'markdownPlugins'
];
const missingFields = requiredFields.filter(field => !config[field]);
if (missingFields && missingFields.length > 0) {
throw new Error(`${missingFields.join(', ')} are missing in siteConfig.js`);
throw new Error(
`${missingFields.join(', ')} fields are missing in siteConfig.js`
);
}
const uselessFields = Object.keys(config).filter(
field => ![...requiredFields, ...optionalFields].includes(field)
);
if (uselessFields && uselessFields.length > 0) {
throw new Error(
`${uselessFields.join(', ')} fields are useless in siteConfig.js`
);
}
return config;
};

View file

@ -0,0 +1,10 @@
module.exports = {
title: 'Hello',
tagline: 'Hello World',
organizationName: 'endiliey',
projectName: 'hello',
baseUrl: '/',
useLessField: 'what',
superman: 'lol',
admin: 'endi'
};

View file

@ -20,7 +20,16 @@ describe('loadConfig', () => {
expect(() => {
loadConfig(siteDir);
}).toThrowErrorMatchingInlineSnapshot(
`"tagline, organizationName, projectName are missing in siteConfig.js"`
`"tagline, organizationName, projectName fields are missing in siteConfig.js"`
);
});
test('website with useless field (wrong field) in siteConfig', () => {
const siteDir = path.join(__dirname, '__fixtures__', 'wrong-site');
expect(() => {
loadConfig(siteDir);
}).toThrowErrorMatchingInlineSnapshot(
`"useLessField, superman, admin fields are useless in siteConfig.js"`
);
});
@ -29,7 +38,7 @@ describe('loadConfig', () => {
expect(() => {
loadConfig(siteDir);
}).toThrowErrorMatchingInlineSnapshot(
`"title, tagline, organizationName, projectName, baseUrl are missing in siteConfig.js"`
`"title, tagline, organizationName, projectName, baseUrl fields are missing in siteConfig.js"`
);
});
});