forked from behance/license-to-fail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (90 loc) · 3.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var checker = require('license-checker');
function isAllowedPackage(allowedPackages, dependency) {
return allowedPackages.some(function(pkg) {
return pkg.name.indexOf(dependency.name.toLowerCase().split('@')[0]) !== -1;
});
}
module.exports = function checkLicenses(config) {
var currentPackage = config.__currentPackage;
var allowedLicenses = config.allowedLicenses || [];
var allowedPackages = config.allowedPackages || [];
var warnOnUnknown = config.warnOnUnknown || false;
var configPath = config.configPath;
var ignoreDevDependencies = config.ignoreDevDependencies || false;
function log(dep) {
var type = 'INDIRECT DEP';
if (currentPackage.dependencies[dep.name]) {
type = 'DEP';
}
else if (currentPackage.devDependencies[dep.name]) {
type = 'DEVDEP';
}
else if (currentPackage.peerDependencies[dep.name]) {
type = 'PEERDEP';
}
else if (currentPackage.optionalDependencies[dep.name]) {
type = 'OPTIONALDEP';
}
console.error(type + ' - ' + dep.name + ' ' + dep.licenses + ': ' + dep.repository);
}
function isAllowedDependency(dependency) {
var licenses = dependency.licenses;
if (Array.isArray(licenses)) {
return licenses.some(function(license) {
return isAllowedDependency({ name: dependency.name, licenses: license });
});
}
return isAllowedPackage(allowedPackages, dependency) ||
allowedLicenses.some(function(license) {
return licenses.toLowerCase().indexOf(license.toLowerCase()) !== -1;
});
}
checker.init({
start: process.cwd(),
production: ignoreDevDependencies
}, function(err, json) {
var prohibitedDeps = Object.keys(json)
.map(function(dep) {
return {
name: dep,
licenses: json[dep].licenses,
repository: json[dep].repository,
licenseFile: json[dep].licenseFile
};
})
.filter(function(dep) {
if (isAllowedDependency(dep)) return false;
// don't check the current package
if (dep.name.indexOf(currentPackage.name) !== -1) return false;
// weird unknown package?
if (dep.name === 'undefined@undefined') return false;
if (warnOnUnknown && dep.licenses === 'UNKNOWN') {
log(dep);
return false;
};
return true;
});
if (prohibitedDeps.length) {
console.log('');
console.log('Disallowed Licenses:');
prohibitedDeps.sort(function(a, b) {
var aLower = Array.isArray(a.licenses) ? a.licenses[0].toLowerCase() : a.licenses.toLowerCase();
var bLower = Array.isArray(b.licenses) ? b.licenses[0].toLowerCase() : b.licenses.toLowerCase();
return aLower < bLower ? -1 : aLower > bLower ? 1 : 0;
});
prohibitedDeps.map(function(dep) { log(dep); });
console.log('');
console.log('If you need to add an exception for the disallowed packages,');
console.log('You will want to modify the config file: ' + configPath);
console.log('by adding an new entry to the allowedPackages array.');
console.log('');
console.log('It takes in an object with a name key:');
console.log('{');
console.log(' "name": "allowed-package-name-here",');
console.log(' "reason": "reason for allowing" // optional');
console.log('}');
console.log('For more info: check out the repo https://github.com/behance/license-to-fail');
process.exit(1);
}
});
};