Skip to content

Commit

Permalink
feat(config-validator): read filenames from CLI arguments (#16475)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbrunet committed Aug 4, 2022
1 parent e02967e commit cbe8bfc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 41 deletions.
3 changes: 3 additions & 0 deletions docs/usage/getting-started/installing-onboarding.md
Expand Up @@ -135,6 +135,9 @@ If you want to make config edits directly, follow these steps:
1. Validate your config by running `renovate-config-validator`
1. If the improved config passes the validation, merge the branch into your mainline branch

The validator program checks files passed as CLI arguments.
If no argument is given, all [default locations](../configuration-options.md) (if files exist) and the `RENOVATE_CONFIG_FILE` environment variable are checked.

You can configure a [pre-commit](https://pre-commit.com) hook to validate your configuration automatically.
Please check out the [`renovatebot/pre-commit-hooks` repository](https://github.com/renovatebot/pre-commit-hooks) for more information.

Expand Down
105 changes: 64 additions & 41 deletions lib/config-validator.ts
Expand Up @@ -50,57 +50,80 @@ type PackageJson = {
};

(async () => {
for (const file of configFileNames.filter(
(name) => name !== 'package.json'
)) {
try {
if (!(await pathExists(file))) {
continue;
}
const parsedContent = await getParsedContent(file);
if (process.argv.length > 2) {
for (const file of process.argv.slice(2)) {
try {
logger.info(`Validating ${file}`);
await validate(file, parsedContent);
if (!(await pathExists(file))) {
returnVal = 1;
logger.error(`${file} does not exist`);
break;
}
const parsedContent = await getParsedContent(file);
try {
logger.info(`Validating ${file}`);
await validate(file, parsedContent);
} catch (err) {
logger.warn({ err }, `${file} is not valid Renovate config`);
returnVal = 1;
}
} catch (err) {
logger.warn({ err }, `${file} is not valid Renovate config`);
logger.warn({ err }, `${file} could not be parsed`);
returnVal = 1;
}
} catch (err) {
logger.warn({ err }, `${file} could not be parsed`);
returnVal = 1;
}
}
try {
const pkgJson = JSON.parse(
await readFile('package.json', 'utf8')
) as PackageJson;
if (pkgJson.renovate) {
logger.info(`Validating package.json > renovate`);
await validate('package.json > renovate', pkgJson.renovate);
}
if (pkgJson['renovate-config']) {
logger.info(`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 = await getFileConfig(process.env);
if (!dequal(fileConfig, {})) {
const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js';
logger.info(`Validating ${file}`);
} else {
for (const file of configFileNames.filter(
(name) => name !== 'package.json'
)) {
try {
await validate(file, fileConfig);
if (!(await pathExists(file))) {
continue;
}
const parsedContent = await getParsedContent(file);
try {
logger.info(`Validating ${file}`);
await validate(file, parsedContent);
} catch (err) {
logger.warn({ err }, `${file} is not valid Renovate config`);
returnVal = 1;
}
} catch (err) {
logger.error({ err }, `${file} is not valid Renovate config`);
logger.warn({ err }, `${file} could not be parsed`);
returnVal = 1;
}
}
} catch (err) {
// ignore
try {
const pkgJson = JSON.parse(
await readFile('package.json', 'utf8')
) as PackageJson;
if (pkgJson.renovate) {
logger.info(`Validating package.json > renovate`);
await validate('package.json > renovate', pkgJson.renovate);
}
if (pkgJson['renovate-config']) {
logger.info(`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 = await getFileConfig(process.env);
if (!dequal(fileConfig, {})) {
const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js';
logger.info(`Validating ${file}`);
try {
await validate(file, fileConfig);
} catch (err) {
logger.error({ err }, `${file} is not valid Renovate config`);
returnVal = 1;
}
}
} catch (err) {
// ignore
}
}
if (returnVal !== 0) {
process.exit(returnVal);
Expand Down

0 comments on commit cbe8bfc

Please sign in to comment.