2018-03-07 05:28:09 +00:00
|
|
|
#!/usr/bin/env node
|
2020-05-16 10:11:56 +00:00
|
|
|
// istanbul ignore file
|
2022-12-23 12:12:32 +00:00
|
|
|
import 'source-map-support/register';
|
2021-03-10 22:04:12 +00:00
|
|
|
import { dequal } from 'dequal';
|
2022-04-29 08:34:55 +00:00
|
|
|
import { pathExists, readFile } from 'fs-extra';
|
2020-05-16 10:11:56 +00:00
|
|
|
import { configFileNames } from './config/app-strings';
|
|
|
|
import { massageConfig } from './config/massage';
|
2021-02-03 09:35:30 +00:00
|
|
|
import { migrateConfig } from './config/migration';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { RenovateConfig } from './config/types';
|
2020-05-16 10:11:56 +00:00
|
|
|
import { validateConfig } from './config/validation';
|
2021-02-03 09:35:30 +00:00
|
|
|
import { logger } from './logger';
|
2021-12-09 12:45:48 +00:00
|
|
|
import {
|
|
|
|
getConfig as getFileConfig,
|
|
|
|
getParsedContent,
|
|
|
|
} from './workers/global/config/parse/file';
|
2018-04-12 09:52:52 +00:00
|
|
|
|
2018-03-07 05:10:17 +00:00
|
|
|
let returnVal = 0;
|
|
|
|
|
2020-05-16 10:11:56 +00:00
|
|
|
async function validate(
|
|
|
|
desc: string,
|
|
|
|
config: RenovateConfig,
|
|
|
|
isPreset = false
|
|
|
|
): Promise<void> {
|
2021-02-03 09:35:30 +00:00
|
|
|
const { isMigrated, migratedConfig } = migrateConfig(config);
|
|
|
|
if (isMigrated) {
|
|
|
|
logger.warn(
|
|
|
|
{
|
|
|
|
oldConfig: config,
|
|
|
|
newConfig: migratedConfig,
|
|
|
|
},
|
|
|
|
'Config migration necessary'
|
2018-03-07 05:10:17 +00:00
|
|
|
);
|
2021-02-03 09:35:30 +00:00
|
|
|
}
|
|
|
|
const massagedConfig = massageConfig(migratedConfig);
|
|
|
|
const res = await validateConfig(massagedConfig, isPreset);
|
|
|
|
if (res.errors.length) {
|
|
|
|
logger.error({ errors: res.errors }, `${desc} contains errors`);
|
2018-03-07 05:10:17 +00:00
|
|
|
returnVal = 1;
|
|
|
|
}
|
|
|
|
if (res.warnings.length) {
|
2021-02-03 09:35:30 +00:00
|
|
|
logger.warn({ warnings: res.warnings }, `${desc} contains warnings`);
|
2018-03-07 05:10:17 +00:00
|
|
|
returnVal = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 10:11:56 +00:00
|
|
|
type PackageJson = {
|
|
|
|
renovate?: RenovateConfig;
|
|
|
|
'renovate-config'?: Record<string, RenovateConfig>;
|
|
|
|
};
|
|
|
|
|
2018-03-29 03:47:49 +00:00
|
|
|
(async () => {
|
2022-08-04 06:25:18 +00:00
|
|
|
if (process.argv.length > 2) {
|
|
|
|
for (const file of process.argv.slice(2)) {
|
2018-03-29 03:47:49 +00:00
|
|
|
try {
|
2022-08-04 06:25:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-03-29 03:47:49 +00:00
|
|
|
} catch (err) {
|
2022-08-04 06:25:18 +00:00
|
|
|
logger.warn({ err }, `${file} could not be parsed`);
|
2018-03-29 03:47:49 +00:00
|
|
|
returnVal = 1;
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 06:25:18 +00:00
|
|
|
} else {
|
|
|
|
for (const file of configFileNames.filter(
|
|
|
|
(name) => name !== 'package.json'
|
|
|
|
)) {
|
2021-02-03 09:35:30 +00:00
|
|
|
try {
|
2022-08-04 06:25:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-02-03 09:35:30 +00:00
|
|
|
} catch (err) {
|
2022-08-04 06:25:18 +00:00
|
|
|
logger.warn({ err }, `${file} could not be parsed`);
|
2021-02-03 09:35:30 +00:00
|
|
|
returnVal = 1;
|
|
|
|
}
|
2019-03-16 06:03:32 +00:00
|
|
|
}
|
2022-08-04 06:25:18 +00:00
|
|
|
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
|
|
|
|
}
|
2019-03-16 06:03:32 +00:00
|
|
|
}
|
2018-03-29 03:47:49 +00:00
|
|
|
if (returnVal !== 0) {
|
|
|
|
process.exit(returnVal);
|
2018-03-07 05:10:17 +00:00
|
|
|
}
|
2021-02-03 09:35:30 +00:00
|
|
|
logger.info('Config validated successfully');
|
2020-05-16 10:11:56 +00:00
|
|
|
})().catch((e) => {
|
2023-04-28 12:17:48 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2020-05-16 10:11:56 +00:00
|
|
|
console.error(e);
|
|
|
|
process.exit(99);
|
|
|
|
});
|