Skip to content

Commit

Permalink
refactor: Use easier to understand functions and variable names
Browse files Browse the repository at this point in the history
The old naming led to me breaking the code when I wanted to refactor it.
"hasOnlyAllowedPackages" could be easily interpreted as "contains no
license not in the allow list", which is not what the function does.
  • Loading branch information
Roman Seidelsohn committed Oct 13, 2023
1 parent 69bd393 commit 7833444
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,17 +536,24 @@ exports.init = function init(args, callback) {
}
};

/**
* Check if the current license contains (eventually among others) at least one of the allowed licenses
*
* @param {string} currentLicense The current license
* @param {<type>} packageName The package name
*/
const checkForOnlyAllow = (currentLicense, packageName) => {
if (toCheckforOnlyAllow.length > 0) {
let hasOnlyAllowedPackages = false;
let containsOneOfAllowedPackages = false;

toCheckforOnlyAllow.forEach((allowedLicense) => {
if (currentLicense.indexOf(allowedLicense) >= 0) {
hasOnlyAllowedPackages = true;
for (const allowedLicense of toCheckforOnlyAllow) {
if (currentLicense.includes(allowedLicense)) {
containsOneOfAllowedPackages = true;
break;
}
});
}

if (!hasOnlyAllowedPackages) {
if (!containsOneOfAllowedPackages) {
console.error(
`Package "${packageName}" is licensed under "${currentLicense}" which is not permitted by the --onlyAllow flag. Exiting.`,
);
Expand Down

0 comments on commit 7833444

Please sign in to comment.