<commit_before>#!/usr/bin/env node
// istanbul ignore file
import { readFileSync } from 'fs-extra';
import JSON5 from 'json5';
import { configFileNames } from './config/app-strings';
import { RenovateConfig } from './config/common';
import { getConfig } from './config/file';
import { massageConfig } from './config/massage';
import { validateConfig } from './config/validation';

/* eslint-disable no-console */

let returnVal = 0;

async function validate(
  desc: string,
  config: RenovateConfig,
  isPreset = false
): Promise<void> {
  const res = await validateConfig(massageConfig(config), isPreset);
  if (res.errors.length) {
    console.log(
      `${desc} contains errors:\n\n${JSON5.stringify(res.errors, null, 2)}`
    );
    returnVal = 1;
  }
  if (res.warnings.length) {
    console.log(
      `${desc} contains warnings:\n\n${JSON5.stringify(res.warnings, null, 2)}`
    );
    returnVal = 1;
  }
}

type PackageJson = {
  renovate?: RenovateConfig;
  'renovate-config'?: Record<string, RenovateConfig>;
};

(async () => {
  for (const file of configFileNames.filter(
    (name) => name !== 'package.json'
  )) {
    try {
      const rawContent = readFileSync(file, 'utf8');
      console.log(`Validating ${file}`);
      try {
        const jsonContent = JSON5.parse(rawContent) as PackageJson;
        await validate(file, jsonContent);
      } catch (err) {
        console.log(`${file} is not valid Renovate config`, err);
        returnVal = 1;
      }
    } catch (err) {
      // file does not exist
    }
  }
  try {
    const pkgJson = JSON5.parse(
      readFileSync('package.json', 'utf8')
    ) as PackageJson;
    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);<commit_msg>fix(validator): use JSON.parse when non-json5 (#6824)<commit_after>#!/usr/bin/env node
// istanbul ignore file
import { readFileSync } from 'fs-extra';
import JSON5 from 'json5';
import { configFileNames } from './config/app-strings';
import { RenovateConfig } from './config/common';
import { getConfig } from './config/file';
import { massageConfig } from './config/massage';
import { validateConfig } from './config/validation';

/* eslint-disable no-console */

let returnVal = 0;

async function validate(
  desc: string,
  config: RenovateConfig,
  isPreset = false
): Promise<void> {
  const res = await validateConfig(massageConfig(config), isPreset);
  if (res.errors.length) {
    console.log(
      `${desc} contains errors:\n\n${JSON5.stringify(res.errors, null, 2)}`
    );
    returnVal = 1;
  }
  if (res.warnings.length) {
    console.log(
      `${desc} contains warnings:\n\n${JSON5.stringify(res.warnings, null, 2)}`
    );
    returnVal = 1;
  }
}

type PackageJson = {
  renovate?: RenovateConfig;
  'renovate-config'?: Record<string, RenovateConfig>;
};

(async () => {
  for (const file of configFileNames.filter(
    (name) => name !== 'package.json'
  )) {
    try {
      const rawContent = readFileSync(file, 'utf8');
      console.log(`Validating ${file}`);
      try {
        let jsonContent;
        if (file.endsWith('.json5)')) {
          jsonContent = JSON5.parse(rawContent) as PackageJson;
        } else {
          jsonContent = JSON.parse(rawContent) as PackageJson;
        }
        await validate(file, jsonContent);
      } catch (err) {
        console.log(`${file} is not valid Renovate config`, err);
        returnVal = 1;
      }
    } catch (err) {
      // file does not exist
    }
  }
  try {
    const pkgJson = JSON.parse(
      readFileSync('package.json', 'utf8')
    ) as PackageJson;
    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);
{'fraction_non_alphanumeric': 0.14898562975486052, 'fraction_numerical': 0.006973795435333897, 'mean_word_length': 3.0110169491525425, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2323685', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1522, 'n_words': 346}