mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-31 17:51:49 +00:00
b2d3b64c48
* refactor(lint): enforce err key for errors in logger metadata * refactor(lint): flag error message and stack strings in logger-err-key
105 lines
3.0 KiB
TypeScript
105 lines
3.0 KiB
TypeScript
import {
|
|
isEmptyStringOrWhitespace,
|
|
isNonEmptyObject,
|
|
isNonEmptyString,
|
|
isUndefined,
|
|
} from '@sindresorhus/is';
|
|
import fs from 'fs-extra';
|
|
import type { AllConfig } from '../../../../config/types.ts';
|
|
import { logger } from '../../../../logger/index.ts';
|
|
import { getParsedContent, migrateAndValidateConfig } from './util.ts';
|
|
|
|
export async function getConfig(env: NodeJS.ProcessEnv): Promise<AllConfig> {
|
|
let config: AllConfig = {};
|
|
const configFile = env.RENOVATE_ADDITIONAL_CONFIG_FILE;
|
|
if (!configFile) {
|
|
logger.debug('No additional config file found specified - skipping');
|
|
return config;
|
|
}
|
|
|
|
const configFileExists = await fs.pathExists(configFile);
|
|
if (!configFileExists) {
|
|
// Unsure what behavior is expected here
|
|
logger.fatal(
|
|
{ configFile },
|
|
`Custom additional config file specified in RENOVATE_ADDITIONAL_CONFIG_FILE must exist`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
logger.debug(`Checking for additional config file in ${configFile}`);
|
|
try {
|
|
config = await getParsedContent(configFile);
|
|
} catch (err) {
|
|
if (err instanceof SyntaxError || err instanceof TypeError) {
|
|
// Unsure what behavior is expected here
|
|
logger.fatal({ err }, 'Could not parse additional config file');
|
|
process.exit(1);
|
|
} else if (err instanceof ReferenceError) {
|
|
// Unsure what behavior is expected here
|
|
logger.fatal(
|
|
{ err },
|
|
'Error parsing additional config file due to unresolved variable(s)',
|
|
);
|
|
process.exit(1);
|
|
} else if (err.message === 'Unsupported file type') {
|
|
logger.fatal(err.message);
|
|
process.exit(1);
|
|
} else if (env.RENOVATE_ADDITIONAL_CONFIG_FILE) {
|
|
logger.debug({ err }, 'Parse error');
|
|
logger.fatal('Error parsing additional config file');
|
|
process.exit(1);
|
|
}
|
|
logger.debug('Error reading or parsing additional config file - skipping');
|
|
}
|
|
|
|
if (isNonEmptyObject(config.processEnv)) {
|
|
const exportedKeys = [];
|
|
for (const [key, value] of Object.entries(config.processEnv)) {
|
|
if (!isNonEmptyString(value)) {
|
|
logger.error({ key }, 'processEnv value is not a string.');
|
|
continue;
|
|
}
|
|
|
|
exportedKeys.push(key);
|
|
process.env[key] = value;
|
|
}
|
|
logger.debug(
|
|
{ keys: exportedKeys },
|
|
'processEnv keys were exported to env',
|
|
);
|
|
delete config.processEnv;
|
|
}
|
|
|
|
return migrateAndValidateConfig(config, configFile);
|
|
}
|
|
|
|
export async function deleteNonDefaultConfig(
|
|
env: NodeJS.ProcessEnv,
|
|
deleteConfigFile: boolean,
|
|
): Promise<void> {
|
|
const configFile = env.RENOVATE_ADDITIONAL_CONFIG_FILE;
|
|
|
|
if (isUndefined(configFile) || isEmptyStringOrWhitespace(configFile)) {
|
|
return;
|
|
}
|
|
|
|
if (!deleteConfigFile) {
|
|
return;
|
|
}
|
|
|
|
if (!(await fs.pathExists(configFile))) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await fs.remove(configFile);
|
|
logger.trace(
|
|
{ path: configFile },
|
|
'Additional config file successfully deleted',
|
|
);
|
|
} catch (err) {
|
|
logger.warn({ err }, 'Error deleting additional config file');
|
|
}
|
|
}
|