Skip to content
Snippets Groups Projects
Commit 0415a05c authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor: move package-rules to separate file

parent 80ebeadf
No related branches found
No related tags found
No related merge requests found
const minimatch = require('minimatch');
const { intersectsSemver } = require('./semver');
const { mergeChildConfig } = require('../config');
module.exports = {
applyPackageRules,
};
function applyPackageRules(config) {
const { packageFile, depType, depName, currentVersion } = config;
let res = { ...config };
config.packageRules.forEach(packageRule => {
let {
paths,
depTypeList,
packageNames,
packagePatterns,
excludePackageNames,
excludePackagePatterns,
matchCurrentVersion,
} = packageRule;
// Setting empty arrays simplifies our logic later
paths = paths || [];
depTypeList = depTypeList || [];
packageNames = packageNames || [];
packagePatterns = packagePatterns || [];
excludePackageNames = excludePackageNames || [];
excludePackagePatterns = excludePackagePatterns || [];
matchCurrentVersion = matchCurrentVersion || null;
let positiveMatch = false;
let negativeMatch = false;
// Massage a positive patterns patch if an exclude one is present
if (
(excludePackageNames.length || excludePackagePatterns.length) &&
!(packageNames.length || packagePatterns.length)
) {
packagePatterns = ['.*'];
}
if (paths.length) {
const isMatch = paths.some(
rulePath =>
packageFile.includes(rulePath) || minimatch(packageFile, rulePath)
);
positiveMatch = positiveMatch || isMatch;
negativeMatch = negativeMatch || !isMatch;
}
if (depTypeList.length) {
const isMatch = depTypeList.includes(depType);
positiveMatch = positiveMatch || isMatch;
negativeMatch = negativeMatch || !isMatch;
}
if (packageNames.length || packagePatterns.length) {
let isMatch = packageNames.includes(depName);
// name match is "or" so we check patterns if we didn't match names
if (!isMatch) {
for (const packagePattern of packagePatterns) {
const packageRegex = new RegExp(
packagePattern === '^*$' || packagePattern === '*'
? '.*'
: packagePattern
);
if (depName.match(packageRegex)) {
logger.trace(`${depName} matches against ${packageRegex}`);
isMatch = true;
}
}
}
positiveMatch = positiveMatch || isMatch;
negativeMatch = negativeMatch || !isMatch;
}
if (excludePackageNames.length) {
const isMatch = excludePackageNames.includes(depName);
negativeMatch = negativeMatch || isMatch;
positiveMatch = positiveMatch || !isMatch;
}
if (excludePackagePatterns.length) {
let isMatch = false;
for (const pattern of excludePackagePatterns) {
const packageRegex = new RegExp(
pattern === '^*$' || pattern === '*' ? '.*' : pattern
);
if (depName.match(packageRegex)) {
logger.trace(`${depName} matches against ${packageRegex}`);
isMatch = true;
}
}
negativeMatch = negativeMatch || isMatch;
positiveMatch = positiveMatch || !isMatch;
}
if (matchCurrentVersion) {
const isMatch = intersectsSemver(currentVersion, matchCurrentVersion);
positiveMatch = positiveMatch || isMatch;
negativeMatch = negativeMatch || !isMatch;
}
// This rule is considered matched if there was at least one positive match and no negative matches
if (positiveMatch && !negativeMatch) {
// Package rule config overrides any existing config
res = mergeChildConfig(res, packageRule);
delete res.paths;
delete res.depTypeList;
delete res.packageNames;
delete res.packagePatterns;
delete res.excludePackageNames;
delete res.excludePackagePatterns;
delete res.matchCurrentVersion;
}
});
delete res.packageRules;
return res;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment