Skip to content

Commit

Permalink
Merge pull request elastic#7527 from cjcenizal/improvement/licenses-t…
Browse files Browse the repository at this point in the history
…ask-log-path

Log dependency path info in licenses grunt task.
  • Loading branch information
cjcenizal authored Jun 22, 2016
2 parents 5caa6c0 + 6d6ab4a commit ded0ee0
Showing 1 changed file with 72 additions and 57 deletions.
129 changes: 72 additions & 57 deletions tasks/licenses.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
var _ = require('lodash');
var npm = require('npm');
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 getLicenses(info, dependency) {
if (config.overrides[dependency]) return config.overrides[dependency];
if (info && info.licenses) return _.flatten([info.licenses]);
import _ from 'lodash';
import {
fromNode,
} from 'bluebird';
import npm from 'npm';
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();

const result = {};
const options = { start: process.cwd(), json: true };
const checkQueueLength = 2;

function getLicenses(dependency) {
if (config.overrides[dependency.name]) {
return config.overrides[dependency.name];
}

if (dependency && dependency.licenses) {
return _.flatten([dependency.licenses]);
}
}

function processPackage(info, dependency) {
var pkgInfo = {};
pkgInfo.name = dependency;
pkgInfo.licenses = getLicenses(info, dependency);
function processPackage(dependency) {
const pkgInfo = Object.assign({}, dependency);
pkgInfo.licenses = getLicenses(dependency);
pkgInfo.valid = (function () {
if (_.intersection(pkgInfo.licenses, config.licenses).length > 0) {
return true;
Expand All @@ -31,49 +36,59 @@ module.exports = function (grunt) {
return pkgInfo;
}

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) {
const allDependencies = await fromNode(cb => {
npmLicense.init(options, result => {
cb(undefined, result);
});
});

// 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;
};
// Only check production NPM dependencies, not dev
await fromNode(cb => npm.load({production: true}, cb));

var productionDependencies = {};
_.each(getDependencies(npmList.dependencies), function (packageAndVersion) {
productionDependencies[packageAndVersion] = allDependencies[packageAndVersion];
});
const npmList = await fromNode(cb => {
npm.commands.list([], true, (a, b, npmList) => cb(undefined, npmList));
});

var licenseStats = _.map(productionDependencies, processPackage);
var invalidLicenses = _.filter(licenseStats, function (pkg) { return !pkg.valid; });
// Recurse npm --production --json ls output, create array of dependency
// objects, each with a name prop formatted as 'package@version' and a
// path prop leading back to the root dependency.
function getDependencies(dependencies, path, list = []) {
Object.keys(dependencies).forEach(name => {
const dependency = dependencies[name];
const newPath = path ? `${path} -> ${dependency.from}` : dependency.from;
list.push({
path: newPath,
name: name + '@' + dependency.version
});

if (!grunt.option('only-invalid')) {
grunt.log.debug(JSON.stringify(licenseStats, null, 2));
}
if (dependency.dependencies) {
getDependencies(dependency.dependencies, newPath, list);
}
});
return list;
};

const productionDependencies = {};
getDependencies(npmList.dependencies).forEach(dependency => {
productionDependencies[dependency.name] =
Object.assign({}, allDependencies[dependency.name], dependency);
});

if (invalidLicenses.length) {
grunt.log.debug(JSON.stringify(invalidLicenses, null, 2));
grunt.fail.warn(
'Non-confirming licenses: ' + _.pluck(invalidLicenses, 'name').join(', '),
invalidLicenses.length
);
}
const licenseStats = _.map(productionDependencies, processPackage);
const invalidLicenses = licenseStats.filter(pkg => !pkg.valid);

done();
});
});
});
if (!grunt.option('only-invalid')) {
grunt.log.debug(JSON.stringify(licenseStats, null, 2));
}

if (invalidLicenses.length) {
grunt.log.debug(JSON.stringify(invalidLicenses, null, 2));
grunt.fail.warn(
`Non-confirming licenses:\n ${_.pluck(invalidLicenses, 'path').join('\n')}`,
invalidLicenses.length
);
}

done();
});
};

0 comments on commit ded0ee0

Please sign in to comment.