From 219180622d92d1ffef1f9fc5cd5b3925084bcdb0 Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Sun, 13 Mar 2016 16:10:05 +0100 Subject: [PATCH] Support two operators and/or as multiple common licenses --- lib/licensify.js | 23 ++++++++++-- test/test-multiple-common-licenses/index.js | 2 + .../test-conjunctive-and-operator/index.js | 1 + .../package.json | 6 +++ .../test-disjunctive-or-operator/index.js | 1 + .../test-disjunctive-or-operator/package.json | 6 +++ .../package.json | 10 +++++ test/test.js | 37 +++++++++++++++++++ 8 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 test/test-multiple-common-licenses/index.js create mode 100644 test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/index.js create mode 100644 test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/package.json create mode 100644 test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/index.js create mode 100644 test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/package.json create mode 100644 test/test-multiple-common-licenses/package.json diff --git a/lib/licensify.js b/lib/licensify.js index a0a80db..ec17540 100644 --- a/lib/licensify.js +++ b/lib/licensify.js @@ -24,9 +24,13 @@ var props = [ 'homepage', 'version' ]; +var operators = [ + 'OR', + 'AND' +] function extractLicense (pkg, summary) { - var name; + var name = '', operator = '', names = [], licenses = []; switch (typeName(pkg.license)) { case 'string': name = pkg.license; @@ -35,8 +39,21 @@ function extractLicense (pkg, summary) { name = pkg.license.type; break; } - if (name) { - summary.license = appendUrlToLicense(name); + + if (name.match(/^\(.+\)$/)) { + names = name.match(/\(([^)]+)\)/)[1] + operators.forEach(function (o) { + var license = names.split(o); + if (license.length > licenses.length) { + operator = o; + licenses = license; + } + }); + summary.license = licenses.map(function (license) { + return appendUrlToLicense(license.trim()); + }).filter(function (v) { return !!v; }).join(' ' + operator + ' '); + } else if (name) { + summary.license = appendUrlToLicense(name); } } diff --git a/test/test-multiple-common-licenses/index.js b/test/test-multiple-common-licenses/index.js new file mode 100644 index 0000000..07f277d --- /dev/null +++ b/test/test-multiple-common-licenses/index.js @@ -0,0 +1,2 @@ +var and = require('test-conjunctive-and-operator'); +var or = require('test-disjunctive-or-operator'); diff --git a/test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/index.js b/test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/index.js new file mode 100644 index 0000000..0b96b2d --- /dev/null +++ b/test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/index.js @@ -0,0 +1 @@ +module.exports = 'and'; diff --git a/test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/package.json b/test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/package.json new file mode 100644 index 0000000..1833948 --- /dev/null +++ b/test/test-multiple-common-licenses/node_modules/test-conjunctive-and-operator/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-conjunctive-and-operator", + "description": "", + "version": "0.0.1", + "license" : "(LGPL-2.1 AND MIT)" +} diff --git a/test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/index.js b/test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/index.js new file mode 100644 index 0000000..ad5f2ad --- /dev/null +++ b/test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/index.js @@ -0,0 +1 @@ +module.exports = 'or'; diff --git a/test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/package.json b/test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/package.json new file mode 100644 index 0000000..7eb93a8 --- /dev/null +++ b/test/test-multiple-common-licenses/node_modules/test-disjunctive-or-operator/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-disjunctive-or-operator", + "description": "", + "version": "0.0.1", + "license" : "(LGPL-2.1 OR MIT)" +} diff --git a/test/test-multiple-common-licenses/package.json b/test/test-multiple-common-licenses/package.json new file mode 100644 index 0000000..fafb993 --- /dev/null +++ b/test/test-multiple-common-licenses/package.json @@ -0,0 +1,10 @@ +{ + "name": "test-multiple-common-licenses", + "description": "", + "version": "0.0.1", + "dependencies": { + "test-conjunctive-and-operator": "./node_modules/test-conjunctive-and-operator", + "test-disjunctive-or-operator": "./node_modules/test-disjunctive-or-operator" + }, + "license": "MIT" +} diff --git a/test/test.js b/test/test.js index 8114081..3a8c384 100644 --- a/test/test.js +++ b/test/test.js @@ -341,3 +341,40 @@ describe('type-less licenses', function () { }); }); }); + +describe('multiple common licenses', function () { + var expectedModules = [ + 'test-conjunctive-and-operator', + 'test-disjunctive-or-operator' + ]; + var expectedOperators = [ + 'OR', 'AND' + ] + + describe('should also be in output', function () { + var header; + before(function (done) { + var save = saveFirstChunk(); + var b = browserify(); + b.add(path.normalize(path.join(__dirname, 'test-multiple-common-licenses', 'index.js'))); + b.plugin(licensify); + b.bundle().pipe(save).pipe(es.wait(function(err, data) { + assert(!err); + header = save.firstChunk; + done(); + })); + }); + expectedModules.forEach(function (moduleName) { + var re = new RegExp(' \* ' + moduleName + '\:$', 'gm'); + it('ensure header includes [' + moduleName + ']', function () { + assert(re.test(header)); + }); + }); + expectedOperators.forEach(function (operators) { + var re = new RegExp(operators, 'gm'); + it('ensure header includes [' + operators + ']', function () { + assert(re.test(header)); + }); + }); + }); +});