2022-02-09 23:05:56 +03:00
|
|
|
|
/**
|
|
|
|
|
* Checks that conf.yml is present + parsable, then validates it against the schema
|
|
|
|
|
* Prints detailed info about any errors or warnings to help the user fix the issue
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const fs = require('fs'); // For opening + reading files
|
|
|
|
|
const yaml = require('js-yaml'); // For parsing YAML
|
|
|
|
|
const Ajv = require('ajv'); // For validating with schema
|
2021-06-11 17:23:25 +03:00
|
|
|
|
|
2021-07-25 19:42:01 +03:00
|
|
|
|
const schema = require('../src/utils/ConfigSchema.json');
|
2021-06-11 17:23:25 +03:00
|
|
|
|
|
2021-07-25 19:42:01 +03:00
|
|
|
|
/* Tell AJV to use strict mode, and report all errors */
|
2021-06-11 17:23:25 +03:00
|
|
|
|
const validatorOptions = {
|
|
|
|
|
strict: true,
|
|
|
|
|
allowUnionTypes: true,
|
|
|
|
|
allErrors: true,
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-25 19:42:01 +03:00
|
|
|
|
/* Initiate AJV validator */
|
2021-06-11 17:23:25 +03:00
|
|
|
|
const ajv = new Ajv(validatorOptions);
|
|
|
|
|
|
|
|
|
|
/* Message printed when validation was successful */
|
2022-02-09 23:05:56 +03:00
|
|
|
|
const successMsg = () => '\x1b[1m\x1b[32m✔️ Config file is valid, no issues found\x1b[0m\n';
|
2021-06-11 17:23:25 +03:00
|
|
|
|
|
2021-10-24 15:31:48 +03:00
|
|
|
|
/* Just a wrapper to system's console.log */
|
2022-02-09 23:05:56 +03:00
|
|
|
|
const logToConsole = (msg) => { console.log(msg || '\n'); }; // eslint-disable-line no-console
|
2021-10-24 15:31:48 +03:00
|
|
|
|
|
2021-06-11 17:23:25 +03:00
|
|
|
|
/* Formats error message. ready for printing to the console */
|
|
|
|
|
const errorMsg = (output) => {
|
|
|
|
|
const warningFont = '\x1b[103m\x1b[34m';
|
|
|
|
|
const line = `${warningFont}${new Array(42).fill('━').join('')}\x1b[0m`;
|
2022-02-09 23:05:56 +03:00
|
|
|
|
const formatParams = (params) => {
|
|
|
|
|
if (params.additionalProperty) return `(${params.additionalProperty})`;
|
|
|
|
|
return '';
|
|
|
|
|
};
|
2021-06-11 17:23:25 +03:00
|
|
|
|
let msg = `\n${line}\n${warningFont} Warning: ${output.length} `
|
|
|
|
|
+ `issue${output.length > 1 ? 's' : ''} found in config file \x1b[0m\n${line}\n`;
|
|
|
|
|
output.forEach((details, index) => {
|
2022-02-09 23:05:56 +03:00
|
|
|
|
msg += `${'\x1b[36m'}${index + 1}. \x1b[4m${details.instancePath}\x1b[0m\x1b[36m `
|
|
|
|
|
+ `${details.message} ${formatParams(details.params)}\x1b[0m\n`;
|
2021-06-11 17:23:25 +03:00
|
|
|
|
});
|
|
|
|
|
return msg;
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-09 23:05:56 +03:00
|
|
|
|
/* Sets valid status as environmental variable */
|
2021-06-23 00:30:46 +03:00
|
|
|
|
const setIsValidVariable = (isValid) => {
|
|
|
|
|
process.env.VUE_APP_CONFIG_VALID = isValid;
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-11 17:23:25 +03:00
|
|
|
|
/* Start the validation */
|
|
|
|
|
const validate = (config) => {
|
2021-10-24 15:31:48 +03:00
|
|
|
|
logToConsole('\nChecking config file against schema...');
|
2021-06-11 17:23:25 +03:00
|
|
|
|
const valid = ajv.validate(schema, config);
|
|
|
|
|
if (valid) {
|
2021-06-23 00:30:46 +03:00
|
|
|
|
setIsValidVariable(true);
|
2021-10-24 15:31:48 +03:00
|
|
|
|
logToConsole(successMsg());
|
2021-06-11 17:23:25 +03:00
|
|
|
|
} else {
|
2021-06-23 00:30:46 +03:00
|
|
|
|
setIsValidVariable(false);
|
2021-10-24 15:31:48 +03:00
|
|
|
|
logToConsole(errorMsg(ajv.errors));
|
2021-06-11 17:23:25 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-09 23:05:56 +03:00
|
|
|
|
/* Error message printed when the file could not be opened */
|
|
|
|
|
const bigError = () => {
|
|
|
|
|
const formatting = '\x1b[30m\x1b[43m';
|
|
|
|
|
const line = `${formatting}${new Array(38).fill('━').join('')}\x1b[0m\n`;
|
|
|
|
|
const msg = `${formatting} Error, unable to validate 'conf.yml' \x1b[0m\n`;
|
|
|
|
|
return `\n${line}${msg}${line}`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Given an error object, prints helpful info to the user */
|
|
|
|
|
const printFileReadError = (e) => {
|
|
|
|
|
let customError = '';
|
|
|
|
|
if (e.mark) { // YAML syntax error
|
|
|
|
|
customError = `\x1b[33m\x1b[4m⚠️ Error on line ${e.mark.line}, column ${e.mark.column}: `
|
|
|
|
|
+ `${e.reason}\x1b[0m\n\n${e.mark.snippet}\n\x1b[0m`
|
|
|
|
|
+ '\n\x1b[36m ℹ️ You might find it helpful to use a YAML validator'
|
|
|
|
|
+ ', like: \x1b[4mhttps://yamlchecker.com/\x1b[0m\n';
|
|
|
|
|
}
|
|
|
|
|
if (e.code === 'ENOENT') { // File not found error
|
|
|
|
|
customError = `\x1b[33m⚠️ Config file could not be found at ${e.path}\x1b[0m\n`;
|
|
|
|
|
}
|
|
|
|
|
if (e.code === 'EISDIR') { // Not a file
|
|
|
|
|
customError = '\x1b[33m⚠️ Config needs to be a file, but found a directory instead \x1b[0m\n';
|
|
|
|
|
}
|
|
|
|
|
if (e.code === 'EACCES' || e.code === 'EPERM') { // File permissions error
|
|
|
|
|
customError = '\x1b[33m⚠️ Permission denied \x1b[0m\n';
|
|
|
|
|
}
|
|
|
|
|
logToConsole(customError);
|
|
|
|
|
if (customError === '') { // Unknown error, print stack trace
|
|
|
|
|
const moreInfo = 'Ensure that your config file is present, readable, and valid YAML. '
|
|
|
|
|
+ 'If this issue persists, you can get support by raising a ticket on GitHub. '
|
|
|
|
|
+ 'Please include the following stack trace';
|
|
|
|
|
logToConsole(moreInfo);
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
|
console.warn('\x1b[33mStack Trace for config-validator.js:\x1b[0m\n', e);
|
|
|
|
|
logToConsole();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try { // Try to open and parse the YAML file
|
2021-06-11 17:23:25 +03:00
|
|
|
|
const config = yaml.load(fs.readFileSync('./public/conf.yml', 'utf8'));
|
|
|
|
|
validate(config);
|
2021-07-25 19:42:01 +03:00
|
|
|
|
} catch (e) { // Something went very wrong...
|
2021-06-23 00:30:46 +03:00
|
|
|
|
setIsValidVariable(false);
|
2021-10-24 15:31:48 +03:00
|
|
|
|
logToConsole(bigError());
|
2022-02-09 23:05:56 +03:00
|
|
|
|
printFileReadError(e);
|
2021-06-11 17:23:25 +03:00
|
|
|
|
}
|