-
Notifications
You must be signed in to change notification settings - Fork 3
/
licenseUsability.js
156 lines (133 loc) · 5.43 KB
/
licenseUsability.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
let packageJson = require('../package.json');
module.exports = function() {
let depsTitle = 'dependencies';
let devDepsTitle = 'devDependencies';
let dependencies = [];
let devDependencies = [];
// *** Mapping ***
dependencies = mapDependenciesLicenses(packageJson, depsTitle);
devDependencies = mapDependenciesLicenses(packageJson, devDepsTitle);
// *** Validators ***
let isProjectPackageJSON = true;
validateLicenseByPackage(packageJson, isProjectPackageJSON);
validateDependenciesLicenses(dependencies, depsTitle);
validateDependenciesLicenses(devDependencies, devDepsTitle);
};
let mapDependenciesLicenses = function(pkg, title) {
let mappedDependencies = [];
if (pkg) {
if (title === 'dependencies' || title === 'devDependencies') {
// Map Dependencies Names
for (let dependency in pkg[title]) {
// Only props on the obj itself & not the prototype
if (pkg[title].hasOwnProperty(dependency)) {
mappedDependencies.push({
'name': dependency
});
}
}
// Map Dependencies Licenses
for (let i = 0; i < mappedDependencies.length; i ) {
let depPackage = require('../node_modules/' mappedDependencies[i].name '/package');
mappedDependencies[i]['license'] = depPackage.license;
}
return mappedDependencies;
}
}
};
let validateLicenseByPackage = function(pkg, isProjectPackageJSON) {
let redText = '\x1b[31m%s\x1b[0m';
if (!pkg) {
return;
}
// Check License is defined
if (!pkg.license || pkg.license.length === 0) {
if (isProjectPackageJSON) {
console.log(redText, ' LICENSE is undefined for ' pkg.name ', It is recommended to set a license for your program');
} else {
console.log(redText, ' LICENSE is undefined for ' pkg.name ', Please visit the Package \n'
' website and verify it is a license you can use');
}
} else {
// Handle white spaces before / after & more text after first word pattern is trimmed
if (pkg.license.indexOf(' ') !== -1) {
pkg.license = handleSpaceBeforeText(pkg.license);
pkg.license = handleMoreTextAfterFirstWord(pkg.license);
}
pkg.license.toUpperCase();
// Friendly console print for each License
printLicenseUsability(pkg);
}
};
let validateDependenciesLicenses = function(dependencies, title) {
console.log('\n * * * ' title.toUpperCase() ' License Usability * * *');
dependencies.forEach((pkg) => {
validateLicenseByPackage(pkg);
});
};
let printLicenseUsability = function(pkg) {
let greenText = '\x1b[32m%s\x1b[0m'; // Approved for use
let redText = '\x1b[31m%s\x1b[0m'; // Warning to use / verify usability
let yellowText = '\x1b[33m%s\x1b[0m'; // Not Classified by me yet
switch (pkg.license) {
case 'MIT':
console.log(greenText, '\n ' pkg.name ' --> You are using MIT License, this is a permissive free software license, \n'
' it permits reuse within proprietary software provided that all copies of the \n'
' licensed software include a copy of MIT License terms & copyright notice \n');
break;
case 'Apache-2.0':
console.log(greenText, ' ' pkg.name ' --> You are using Apache-2.0 License, this is a permissive free software license \n'
' written by the Apache Software Foundation. version 2.0 requires preservation of the \n'
' copyright notice and disclaimer. Like other free software licenses, the license allows \n'
' the user of the software the freedom to use the software for any purpose, to \n'
' distribute it, to modify it, and to distribute modified versions of the software, \n'
' under the terms of the license, without concern for royalties.\n');
break;
case 'ISC':
console.log(yellowText, ' ' pkg.name ' --> Your using ISC License \n');
break;
case 'GPL':
console.log(yellowText, ' ' pkg.name ' --> Your using GPL License \n');
break;
case 'LGPL':
console.log(yellowText, ' ' pkg.name ' --> Your using LGPL License \n');
break;
case 'Proprietary':
console.log(yellowText, ' ' pkg.name ' --> Your using Proprietary License \n');
break;
case 'BSD-3-Clause':
console.log(greenText, ' ' pkg.name ' --> You are using BSD-3-CLAUSE License, you can use it for \n '
' commercial use, modify and distribute it, need to give copyright \n'
' credit and include the original license in source code \n');
break;
case 'Apache':
console.log(yellowText, ' ' pkg.name ' --> Your using Apache License \n');
break;
default:
console.log(yellowText, ' In ' pkg.name ' --> Your ' pkg.license
' License was not recognized, please contact the creator \n'
' of this library if you think this message is wrong \n');
}
};
// *** Handlers ***
let handleMoreTextAfterFirstWord = function(pkgLicense) {
let packageLicense;
let noSpacesAfterPkgLicense;
if (pkgLicense.indexOf(' ')) {
packageLicense = pkgLicense.substring(0, pkgLicense.indexOf(' '));
noSpacesAfterPkgLicense = packageLicense;
return noSpacesAfterPkgLicense;
}
return pkgLicense;
};
let handleSpaceBeforeText = function(pkgLicense) {
let packageLicense;
let noSpacesBeforePkgLicense;
// Handle case of all spaces before any other First character
if (pkgLicense.indexOf(' ') === 0) {
packageLicense = pkgLicense.replace(/[\s]*/, '');
noSpacesBeforePkgLicense = packageLicense;
return noSpacesBeforePkgLicense;
}
return pkgLicense;
};