From 0415a05cc471544025ed7a60c0b3647d7c012448 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 3 May 2018 14:28:14 +0200
Subject: [PATCH] refactor: move package-rules to separate file

---
 lib/util/package-rules.js | 111 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 111 insertions(+)
 create mode 100644 lib/util/package-rules.js

diff --git a/lib/util/package-rules.js b/lib/util/package-rules.js
new file mode 100644
index 0000000000..2f02aa57e8
--- /dev/null
+++ b/lib/util/package-rules.js
@@ -0,0 +1,111 @@
+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;
+}
-- 
GitLab