Add 'licenses' task to grunt to check for incompatible licenses

This commit is contained in:
Rashid Khan 2015-06-04 13:47:57 -07:00
parent 029360a2ee
commit fc28784e4d
3 changed files with 122 additions and 0 deletions

View file

@ -64,6 +64,7 @@
"through": "^2.3.6"
},
"devDependencies": {
"bower-license": "^0.2.6",
"connect": "~2.19.5",
"event-stream": "~3.1.5",
"expect.js": "~0.3.1",
@ -89,6 +90,7 @@
"http-proxy": "~1.8.1",
"husky": "~0.6.0",
"istanbul": "~0.2.4",
"license-checker": "^3.0.3",
"load-grunt-config": "~0.7.0",
"lodash": "~2.4.1",
"marked": "^0.3.2",
@ -96,6 +98,7 @@
"mkdirp": "^0.5.0",
"mocha": "^2.2.5",
"mocha-screencast-reporter": "~0.1.4",
"npm": "^2.11.0",
"opn": "~1.0.0",
"path-browserify": "0.0.0",
"progress": "^1.1.8",

39
tasks/config/licenses.js Normal file
View file

@ -0,0 +1,39 @@
module.exports = function (grunt) {
return {
options: {
licenses: [
'MIT',
'MIT*',
'MIT/X11',
'new BSD, and MIT',
'BSD',
'BSD*',
'BSD New',
'BSD-like',
'BSD-2-Clause',
'BSD-3-Clause',
'Apache',
'Apache*',
'Apache v2',
'Apache 2.0',
'Apache2',
'Apache-2.0',
'Apache, Version 2.0',
'ISC',
'WTFPL',
'Public-Domain'
],
overrides: {
'angular-bootstrap@0.10.0': ['MIT'],
'angular-ui-ace@0.2.3': ['MIT'],
'leaflet@0.7.2': ['BSD-2-Clause'],
'moment-timezone@0.0.6': ['MIT'],
'zeroclipboard@2.2.0': ['MIT'],
'FileSaver@undefined': ['MIT'],
'cycle@1.0.3': ['Public-Domain'],
'pkginfo@0.2.3': ['MIT'],
'uglify-js@2.2.5': ['BSD']
}
}
};
};

80
tasks/licenses.js Normal file
View file

@ -0,0 +1,80 @@
var _ = require('lodash');
var npm = require('npm');
var bowerLicense = require('bower-license');
var npmLicense = require('license-checker');
module.exports = function (grunt) {
grunt.registerTask('licenses', 'Checks dependency licenses', function () {
var config = this.options();
var done = this.async();
var result = {};
var options = {start: process.cwd(), json: true };
var checkQueueLength = 2;
function processPackage(info, dependency) {
var pkgInfo = {};
pkgInfo.name = dependency;
pkgInfo.licenses = config.overrides[dependency] || info.licenses;
pkgInfo.licenses = _.isArray(pkgInfo.licenses) ? pkgInfo.licenses : [pkgInfo.licenses];
pkgInfo.valid = (function () {
if (_.intersection(pkgInfo.licenses, config.licenses).length > 0) {
return true;
}
return false;
})();
return pkgInfo;
}
function dequeue(output) {
checkQueueLength--;
_.extend(result, output);
if (!checkQueueLength) {
var licenseStats = _.map(result, processPackage);
if (grunt.option('check-validity')) {
var invalidLicenses = _.filter(licenseStats, function (pkg) { return !pkg.valid;});
if (invalidLicenses.length) {
console.log(invalidLicenses);
grunt.fail.warn('Dependencies with non-conforming licenses found', invalidLicenses.length);
}
} else {
console.log(_.indexBy(licenseStats, 'name'));
}
done();
}
}
bowerLicense.init(options, dequeue);
npmLicense.init(options, function (allDependencies) {
// Only check production NPM dependencies, not dev
npm.load({production: true}, function () {
npm.commands.list([], true, function (a, b, npmList) {
// Recurse npm --production --json ls output, create array of package@version
var getDependencies = function (dependencies, list) {
list = list || [];
_.each(dependencies, function (info, dependency) {
list.push(dependency + '@' + info.version);
if (info.dependencies) {
getDependencies(info.dependencies, list);
}
});
return list;
};
var productionDependencies = {};
_.each(getDependencies(npmList.dependencies), function (packageAndVersion) {
productionDependencies[packageAndVersion] = allDependencies[packageAndVersion];
});
dequeue(productionDependencies);
});
});
});
});
};