-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate-config.js
89 lines (82 loc) · 3 KB
/
validate-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @summary Validates the Node.js Extensions configuration object.
* @description This script validates a configuration object against the format provided in @example.
* @example "nodejsExtensions": {
* "assetsToCopy": [
* {
* "sources": [ "Assets/Images" ],
* "target": "wwwroot/images"
* },
* {
* "sources": [
* "node_modules/marvelous/dist"
* "node_modules/wonderful/bin"
* ],
* "pattern": "*",
* "target": "wwwroot"
* }
* // more groups, if needed
* ],
* "scripts": {
* "source": "CustomJsFolder",
* "target": "wwwCustomRoot/jay-es"
* },
* "styles": {
* "source": "NonDefaultScssFolder",
* "target": "wwwwNonDefaultCssFolder"
* }
* }
*/
function logError(message) { process.stderr.write(`ERROR: ${message}\n`); }
function validateAssetGroupsAndLogErrors(assetConfig) {
if (!Array.isArray(assetConfig)) {
logError('The configuration must be an array of asset groups of the form: ' +
'{ sources: Array<string>, pattern: string [optional], target: string }.');
return false;
}
const errors = [];
assetConfig.forEach((assetGroup) => {
if (!Array.isArray(assetGroup.sources)) {
errors.push('sources must be an array of strings');
}
if (assetGroup.pattern && typeof assetGroup.pattern !== 'string') {
errors.push('pattern must be a glob string');
}
if (typeof assetGroup.target !== 'string') {
errors.push('target must be a string');
}
if (errors.length > 0) {
logError(`Invalid asset group: ${JSON.stringify(assetGroup)}: ${errors.join(', ')}.`);
}
});
return errors.length === 0;
}
function validateSimpleGroupAndLogErrors(group) {
const errors = [];
if (typeof group.source !== 'string') {
errors.push('source must be a string');
}
if (typeof group.target !== 'string') {
errors.push('target must be a string');
}
if (errors.length > 0) {
logError(`Invalid group: ${JSON.stringify(group)}: ${errors.join(', ')}.`);
}
return errors.length === 0;
}
function validateAndLogErrors(nodejsExtensionsConfig) {
return Object.entries(nodejsExtensionsConfig).every(([key, group]) => {
switch (key) {
case 'assetsToCopy':
return validateAssetGroupsAndLogErrors(group);
case 'scripts':
case 'styles':
return validateSimpleGroupAndLogErrors(group);
default:
throw new Error(
`Found unexpected key "${key}" in Node.js Extensions configuration! Valid keys are: ` +
'"assetsToCopy", "scripts", "styles".');
}
});
}
module.exports = validateAndLogErrors;