renovate/lib/config/massage.ts

75 lines
2.1 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 { RenovateConfig, UpdateType, PackageRule } from './common';
import { getOptions } from './definitions';
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' && 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].push(massageConfig(item));
} else {
massagedConfig[key].push(item);
}
});
2019-01-24 12:41:45 +00:00
} else if (is.object(val) && key !== 'encrypted') {
massagedConfig[key] = massageConfig(val);
}
}
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)) {
2019-03-11 22:43:30 +00:00
const newRule = clone(rule);
newRule.updateTypes = rule.updateTypes || [];
newRule.updateTypes.push(key);
Object.assign(newRule, val);
newRules.push(newRule);
}
}
}
for (const rule of newRules) {
updateTypes.forEach(updateType => {
delete rule[updateType];
});
}
massagedConfig.packageRules = newRules;
}
return massagedConfig;
}