renovate/lib/config/massage.ts

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-07-17 08:14:56 +00:00
import is from '@sindresorhus/is';
2019-09-06 04:48:25 +00:00
import { clone } from '../util/clone';
2019-08-23 13:46:31 +00:00
import { getOptions } from './definitions';
2021-03-02 20:44:55 +00:00
import type { PackageRule, RenovateConfig, UpdateType } from './types';
2019-07-17 08:14:56 +00:00
2019-08-23 13:46:31 +00:00
const options = getOptions();
2019-08-23 13:46:31 +00:00
let allowedStrings: string[];
// Returns a massaged config
2019-08-23 13:46:31 +00:00
export function massageConfig(config: RenovateConfig): RenovateConfig {
if (!allowedStrings) {
allowedStrings = [];
options.forEach((option) => {
if (option.allowString) {
allowedStrings.push(option.name);
}
});
}
2019-03-11 22:43:30 +00:00
const massagedConfig = clone(config);
for (const [key, val] of Object.entries(config)) {
if (allowedStrings.includes(key) && is.string(val)) {
massagedConfig[key] = [val];
} else if (key === 'npmToken' && is.string(val) && val.length < 50) {
massagedConfig.npmrc = `//registry.npmjs.org/:_authToken=${val}\n`;
delete massagedConfig.npmToken;
} else if (is.array(val)) {
massagedConfig[key] = [];
val.forEach((item) => {
if (is.object(item)) {
(massagedConfig[key] as RenovateConfig[]).push(
massageConfig(item as RenovateConfig)
);
} else {
(massagedConfig[key] as unknown[]).push(item);
}
});
2019-01-24 12:41:45 +00:00
} else if (is.object(val) && key !== 'encrypted') {
massagedConfig[key] = massageConfig(val as RenovateConfig);
}
}
if (is.nonEmptyArray(massagedConfig.packageRules)) {
2019-08-23 13:46:31 +00:00
const newRules: PackageRule[] = [];
const updateTypes: UpdateType[] = [
'major',
'minor',
'patch',
'pin',
'digest',
'lockFileMaintenance',
'rollback',
];
for (const rule of massagedConfig.packageRules) {
newRules.push(rule);
2019-08-23 13:46:31 +00:00
for (const [key, val] of Object.entries(rule) as [
UpdateType,
PackageRule
][]) {
if (updateTypes.includes(key)) {
let newRule = clone(rule);
newRule.matchUpdateTypes = rule.matchUpdateTypes || [];
newRule.matchUpdateTypes.push(key);
newRule = { ...newRule, ...val };
newRules.push(newRule);
}
}
}
for (const rule of newRules) {
updateTypes.forEach((updateType) => {
delete rule[updateType];
});
}
massagedConfig.packageRules = newRules;
}
return massagedConfig;
}