diff --git a/package.json b/package.json index 353fec1f..beb13927 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,9 @@ "license": "MIT", "dependencies": { "lodash": "4.17.2", + "magic-string": "0.16.0", "moment": "2.16.0", - "magic-string": "0.16.0" + "parse-author": "1.0.0" }, "devDependencies": { "babel": "6.5.2", diff --git a/src/license-plugin.js b/src/license-plugin.js index fecb6026..e99da4b2 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -26,6 +26,7 @@ const fs = require('fs'); const path = require('path'); const _ = require('lodash'); const moment = require('moment'); +const parseAuthor = require('parse-author'); const MagicString = require('magic-string'); const EOL = '\n'; @@ -193,6 +194,38 @@ class LicensePlugin { 'private', ]); + // Parse the author field to get an object. + if (_.isString(dependency.author)) { + dependency.author = parseAuthor(dependency.author); + } + + // Parse the contributor array. + if (dependency.contributors) { + // Translate to an array if it is not already. + if (_.isString(dependency.contributors)) { + dependency.contributors = [dependency.contributors]; + } + + // Parse each contributor to produce a single object for each person. + dependency.contributors = _.map(dependency.contributors, (contributor) => { + return _.isString(contributor) ? parseAuthor(contributor) : contributor; + }); + } + + // The `licenses` field is deprecated but may be used in some packages. + // Map it to a standard license field. + if (!dependency.license && dependency.licenses) { + // Map it to a valid license field. + // See: https://docs.npmjs.com/files/package.json#license + dependency.license = `(${_.chain(dependency.licenses) + .map((license) => license.type || license) + .value() + .join(' OR ')})`; + + // Remove it. + delete dependency.licenses; + } + this._dependencies[name] = dependency; } } diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index 6a23457c..4f4f5fce 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -24,7 +24,6 @@ const fs = require('fs'); const path = require('path'); -const _ = require('lodash'); const moment = require('moment'); const LicensePlugin = require('../dist/license-plugin.js'); @@ -49,11 +48,14 @@ describe('LicensePlugin', () => { expect(plugin._dependencies).toEqual({ 'fake-package': { name: 'fake-package', - author: 'Mickael Jeanroy ', version: '1.0.0', description: 'Fake package used in unit tests', license: 'MIT', private: true, + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + }, }, }); }); @@ -80,7 +82,17 @@ describe('LicensePlugin', () => { plugin.load(id); expect(plugin._dependencies).toEqual({ - 'fake-package': _.pick(pkg, ['name', 'author', 'version', 'description', 'license', 'private']), + 'fake-package': { + name: 'fake-package', + version: '1.0.0', + description: 'Fake package used in unit tests', + license: 'MIT', + private: true, + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + }, + }, }); expect(plugin._cache).toEqual({ @@ -122,17 +134,14 @@ describe('LicensePlugin', () => { const pkg = { name: 'foo', version: '0.0.0', - author: 'Mickael Jeanroy ', - contributors: ['Test '], + author: {name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}, + contributors: [{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}], description: 'Fake Description', main: 'src/index.js', license: 'MIT', homepage: 'https://www.google.fr', private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, }; plugin.addDependency(pkg); @@ -143,8 +152,8 @@ describe('LicensePlugin', () => { foo: { name: 'foo', version: '0.0.0', - author: 'Mickael Jeanroy ', - contributors: ['Test '], + author: {name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}, + contributors: [{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}], description: 'Fake Description', license: 'MIT', homepage: 'https://www.google.fr', @@ -157,22 +166,128 @@ describe('LicensePlugin', () => { }); }); - it('should add dependency twice', () => { + it('should add dependency and parse author field', () => { const plugin = new LicensePlugin(); const pkg = { name: 'foo', version: '0.0.0', - author: 'Mickael Jeanroy ', - contributors: ['Test '], + author: 'Mickael Jeanroy (https://mjeanroy.com)', + contributors: [{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}], description: 'Fake Description', main: 'src/index.js', license: 'MIT', homepage: 'https://www.google.fr', private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, + }; + + plugin.addDependency(pkg); + + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo).toEqual(jasmine.objectContaining({ + author: { + name: 'Mickael Jeanroy', + url: 'https://mjeanroy.com', + email: 'mickael.jeanroy@gmail.com', }, + })); + }); + + it('should add dependency and parse contributors field as a string', () => { + const plugin = new LicensePlugin(); + const pkg = { + name: 'foo', + version: '0.0.0', + author: 'Mickael Jeanroy (https://mjeanroy.com)', + contributors: 'Mickael Jeanroy (https://mjeanroy.com)', + description: 'Fake Description', + main: 'src/index.js', + license: 'MIT', + homepage: 'https://www.google.fr', + private: true, + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, + }; + + plugin.addDependency(pkg); + + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo).toEqual(jasmine.objectContaining({ + contributors: [{ + name: 'Mickael Jeanroy', + url: 'https://mjeanroy.com', + email: 'mickael.jeanroy@gmail.com', + }], + })); + }); + + it('should add dependency and parse contributors field', () => { + const plugin = new LicensePlugin(); + const pkg = { + name: 'foo', + version: '0.0.0', + author: 'Mickael Jeanroy (https://mjeanroy.com)', + contributors: [ + 'Mickael Jeanroy (https://mjeanroy.com)', + {name: 'John Doe', email: 'johndoe@doe.com'}, + ], + description: 'Fake Description', + main: 'src/index.js', + license: 'MIT', + homepage: 'https://www.google.fr', + private: true, + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, + }; + + plugin.addDependency(pkg); + + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo).toEqual(jasmine.objectContaining({ + contributors: [ + {name: 'Mickael Jeanroy', url: 'https://mjeanroy.com', email: 'mickael.jeanroy@gmail.com'}, + {name: 'John Doe', email: 'johndoe@doe.com'}, + ], + })); + }); + + it('should add dependency and parse licenses field', () => { + const plugin = new LicensePlugin(); + const pkg = { + name: 'foo', + version: '0.0.0', + author: 'Mickael Jeanroy (https://mjeanroy.com)', + description: 'Fake Description', + main: 'src/index.js', + licenses: [ + {type: 'MIT', url: 'http://www.opensource.org/licenses/mit-license.php'}, + {type: 'Apache-2.0', url: 'http://opensource.org/licenses/apache2.0.php'}, + ], + homepage: 'https://www.google.fr', + private: true, + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, + }; + + plugin.addDependency(pkg); + + expect(plugin._dependencies.foo).toBeDefined(); + expect(plugin._dependencies.foo.licenses).not.toBeDefined(); + expect(plugin._dependencies.foo).toEqual(jasmine.objectContaining({ + license: '(MIT OR Apache-2.0)', + })); + }); + + it('should not add dependency twice', () => { + const plugin = new LicensePlugin(); + const pkg = { + name: 'foo', + version: '0.0.0', + author: {name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}, + contributors: [{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}], + description: 'Fake Description', + main: 'src/index.js', + license: 'MIT', + homepage: 'https://www.google.fr', + private: true, + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, }; plugin.addDependency(pkg); @@ -183,16 +298,13 @@ describe('LicensePlugin', () => { foo: { name: 'foo', version: '0.0.0', - author: 'Mickael Jeanroy ', - contributors: ['Test '], + author: {name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}, + contributors: [{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}], description: 'Fake Description', license: 'MIT', homepage: 'https://www.google.fr', private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, }, }); @@ -204,16 +316,13 @@ describe('LicensePlugin', () => { foo: { name: 'foo', version: '0.0.0', - author: 'Mickael Jeanroy ', - contributors: ['Test '], + author: {name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}, + contributors: [{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'}], description: 'Fake Description', license: 'MIT', homepage: 'https://www.google.fr', private: true, - repository: { - type: 'GIT', - url: 'https://github.com/npm/npm.git', - }, + repository: {type: 'GIT', url: 'https://github.com/npm/npm.git'}, }, }); });