2016-06-21 00:17:59 +02:00
|
|
|
import _ from 'lodash';
|
2016-07-21 00:00:30 +02:00
|
|
|
import { fromNode } from 'bluebird';
|
2016-06-21 00:17:59 +02:00
|
|
|
import npmLicense from 'license-checker';
|
|
|
|
|
|
|
|
export default function licenses(grunt) {
|
|
|
|
grunt.registerTask('licenses', 'Checks dependency licenses', async function () {
|
|
|
|
const config = this.options();
|
|
|
|
const done = this.async();
|
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
const options = {
|
|
|
|
start: process.cwd(),
|
|
|
|
production: true,
|
|
|
|
json: true
|
|
|
|
};
|
2015-06-04 22:47:57 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
const packages = await fromNode(cb => {
|
2017-03-22 15:08:23 +01:00
|
|
|
npmLicense.init(options, result => {
|
2016-06-21 00:17:59 +02:00
|
|
|
cb(undefined, result);
|
|
|
|
});
|
|
|
|
});
|
2015-06-04 22:47:57 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
/**
|
|
|
|
* Licenses for a package by name with overrides
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
2015-06-04 22:47:57 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
function licensesForPackage(name) {
|
|
|
|
let licenses = packages[name].licenses;
|
2015-06-04 22:47:57 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
if (config.overrides.hasOwnProperty(name)) {
|
|
|
|
licenses = config.overrides[name];
|
|
|
|
}
|
2015-07-28 02:13:45 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
return typeof licenses === 'string' ? [licenses] : licenses;
|
|
|
|
}
|
2015-07-28 02:13:45 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
/**
|
|
|
|
* Determine if a package has a valid license
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2015-07-28 02:13:45 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
function isInvalidLicense(name) {
|
2016-12-13 18:44:27 +01:00
|
|
|
const licenses = licensesForPackage(name);
|
2015-07-28 02:13:45 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
// verify all licenses for the package are in the config
|
|
|
|
return _.intersection(licenses, config.licenses).length < licenses.length;
|
2016-06-21 00:17:59 +02:00
|
|
|
}
|
2015-06-04 22:47:57 +02:00
|
|
|
|
2016-07-21 00:00:30 +02:00
|
|
|
// Build object containing only invalid packages
|
|
|
|
const invalidPackages = _.pick(packages, (pkg, name) => {
|
|
|
|
return isInvalidLicense(name);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Object.keys(invalidPackages).length) {
|
|
|
|
const execSync = require('child_process').execSync;
|
|
|
|
const names = Object.keys(invalidPackages);
|
|
|
|
|
|
|
|
// Uses npm ls to create tree for package locations
|
|
|
|
const tree = execSync(`npm ls ${names.join(' ')}`);
|
|
|
|
|
|
|
|
grunt.log.debug(JSON.stringify(invalidPackages, null, 2));
|
2016-06-21 00:17:59 +02:00
|
|
|
grunt.fail.warn(
|
2016-07-21 00:00:30 +02:00
|
|
|
`Non-confirming licenses:\n ${names.join('\n ')}\n\n${tree}`,
|
|
|
|
invalidPackages.length
|
2016-06-21 00:17:59 +02:00
|
|
|
);
|
|
|
|
}
|
2015-06-04 22:47:57 +02:00
|
|
|
|
2016-06-21 00:17:59 +02:00
|
|
|
done();
|
2015-06-04 22:47:57 +02:00
|
|
|
});
|
2016-12-14 02:17:47 +01:00
|
|
|
}
|