-
Notifications
You must be signed in to change notification settings - Fork 44
/
validateConfig.js
85 lines (74 loc) · 2.49 KB
/
validateConfig.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
const browserslist = require('browserslist');
const { yellow, red, bold, underline, white } = require('chalk');
const didYouMean = require('didyoumean2').default;
const { emojify } = require('node-emoji');
const configSchema = require('./configSchema');
const defaultSkuConfig = require('./defaultSkuConfig');
const defaultClientEntry = require('./defaultClientEntry');
const availableConfigKeys = Object.keys(defaultSkuConfig);
const exitWithErrors = errors => {
console.log(bold(underline(red('Errors in sku config:'))));
errors.forEach(error => {
console.log(yellow(emojify(error)));
});
process.exit(1);
};
module.exports = skuConfig => {
const errors = [];
// Validate extra keys
Object.keys(skuConfig)
.filter(key => !availableConfigKeys.includes(key))
.forEach(key => {
const unknownMessage = `Unknown key '${bold(key)}'.`;
const suggestedKey = didYouMean(key, availableConfigKeys);
const suggestedMessage = suggestedKey
? ` Did you mean '${bold(suggestedKey)}'?`
: '';
errors.push(`:question: ${unknownMessage}${suggestedMessage}`);
});
// Validate schema types
const schemaCheckResult = configSchema(skuConfig);
if (schemaCheckResult !== true) {
schemaCheckResult.forEach(({ message, field }) => {
const errorMessage = message
? `:no_entry_sign: ${message.replace(field, `${bold(field)}`)}`
: `:no_entry_sign: '${bold(field)}' is invalid`;
errors.push(errorMessage);
});
}
// Validate library entry has corresponding libraryName
if (skuConfig.libraryEntry && !skuConfig.libraryName) {
errors.push(
`:no_entry_sign: '${bold(
'libraryEntry'
)}' must have a corresponding '${bold(
'libraryName'
)}' option. More details: ${underline(
'https://github.com/seek-oss/sku#building-a-library'
)}`
);
}
// Ensure defaultClientEntry is not configured as a route name
skuConfig.routes.forEach(({ name }) => {
if (name === defaultClientEntry) {
errors.push(
`:no_entry_sign: Invalid route name: '${bold(
defaultClientEntry
)}', please use a different route name`
);
}
});
// Ensure supportedBrowsers is valid browserslist query
try {
browserslist(skuConfig.supportedBrowsers);
} catch (e) {
errors.push(
`:no_entry_sign: '${bold(
'supportedBrowsers'
)}' must be a valid browserslist query. ${white(e.message)}`
);
}
if (errors.length > 0) {
exitWithErrors(errors);
}
};