renovate/bin/config-validator.js
Jamie Magee 6aef5d2bc0 feat: validate renovate config file (#3389)
Extend renovate-config-validator to validate `config.js` and `RENOVATE_CONFIG_FILE`. Also add the execute bit to `config-validator.js`

Fixed #3388
2019-03-16 07:03:32 +01:00

83 lines
2.3 KiB
JavaScript
Executable file

#!/usr/bin/env node
const fs = require('fs-extra');
const { validateConfig } = require('../lib/config/validation');
const { massageConfig } = require('../lib/config/massage');
const { getConfig } = require('../lib/config/file');
const { initLogger } = require('../lib/logger');
const cache = require('../lib/workers/global/cache');
const { configFileNames } = require('../lib/config/app-strings');
initLogger();
// istanbul ignore if
if (!global.renovateCache) {
cache.init();
}
/* eslint-disable no-console */
let returnVal = 0;
async function validate(desc, config, isPreset = false) {
const res = await validateConfig(massageConfig(config), isPreset);
if (res.errors.length) {
console.log(
`${desc} contains errors:\n\n${JSON.stringify(res.errors, null, 2)}`
);
returnVal = 1;
}
if (res.warnings.length) {
console.log(
`${desc} contains warnings:\n\n${JSON.stringify(res.warnings, null, 2)}`
);
returnVal = 1;
}
}
(async () => {
for (const file of configFileNames.filter(name => name !== 'package.json')) {
try {
const rawContent = fs.readFileSync(file, 'utf8');
console.log(`Validating ${file}`);
try {
const jsonContent = JSON.parse(rawContent);
await validate(file, jsonContent);
} catch (err) {
console.log(`${file} is not valid Renovate config`);
returnVal = 1;
}
} catch (err) {
// file does not exist
}
}
try {
const pkgJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
if (pkgJson.renovate) {
console.log(`Validating package.json > renovate`);
await validate('package.json > renovate', pkgJson.renovate);
}
if (pkgJson['renovate-config']) {
console.log(`Validating package.json > renovate-config`);
for (const presetConfig of Object.values(pkgJson['renovate-config'])) {
await validate('package.json > renovate-config', presetConfig, true);
}
}
} catch (err) {
// ignore
}
try {
const fileConfig = getConfig(process.env);
console.log(`Validating config.js`);
try {
await validate('config.js', fileConfig);
} catch (err) {
console.log(`config.js is not valid Renovate config`);
returnVal = 1;
}
} catch (err) {
// ignore
}
if (returnVal !== 0) {
process.exit(returnVal);
}
console.log('OK');
})();