2021-06-11 17:23:25 +03:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
/* Script that validates the conf.yml file against Dashy's schema, and outputs any issues */
|
|
|
|
const Ajv = require('ajv');
|
|
|
|
const yaml = require('js-yaml');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
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 */
|
|
|
|
const successMsg = () => '\x1b[1m\x1b[32mNo issues found, your configuration is valid :)\x1b[0m\n';
|
|
|
|
|
2021-10-24 15:31:48 +03:00
|
|
|
/* Just a wrapper to system's console.log */
|
|
|
|
const logToConsole = (msg) => { console.log(msg); };
|
|
|
|
|
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`;
|
|
|
|
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) => {
|
|
|
|
msg += `${'\x1b[36m'}${index + 1}. ${details.keyword} ${details.message} `
|
|
|
|
+ `in ${details.instancePath}\x1b[0m\n`;
|
|
|
|
});
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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}\n`;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
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());
|
|
|
|
logToConsole('Please ensure that your config file is present, '
|
2021-06-11 17:23:25 +03:00
|
|
|
+ 'has the correct access rights and is parsable. '
|
|
|
|
+ 'If this warning persists, it may be an issue with the '
|
|
|
|
+ 'validator function. Please raise an issue, and include the following stack trace:\n');
|
2021-07-25 19:42:01 +03:00
|
|
|
console.warn('\x1b[33mStack Trace for config-validator.js:\x1b[0m\n', e);
|
2021-10-24 15:31:48 +03:00
|
|
|
logToConsole('\n\n');
|
2021-06-11 17:23:25 +03:00
|
|
|
}
|