Skip to content

Commit

Permalink
feat: normalize author, contributors and license fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 21, 2016
1 parent 79b2379 commit 8652b07
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 30 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
}
Expand Down
167 changes: 138 additions & 29 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -49,11 +48,14 @@ describe('LicensePlugin', () => {
expect(plugin._dependencies).toEqual({
'fake-package': {
name: 'fake-package',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
version: '1.0.0',
description: 'Fake package used in unit tests',
license: 'MIT',
private: true,
author: {
name: 'Mickael Jeanroy',
email: 'mickael.jeanroy@gmail.com',
},
},
});
});
Expand All @@ -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({
Expand Down Expand Up @@ -122,17 +134,14 @@ describe('LicensePlugin', () => {
const pkg = {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
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);
Expand All @@ -143,8 +152,8 @@ describe('LicensePlugin', () => {
foo: {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
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',
Expand All @@ -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 <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com> (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 <mickael.jeanroy@gmail.com> (https://mjeanroy.com)',
contributors: 'Mickael Jeanroy <mickael.jeanroy@gmail.com> (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 <mickael.jeanroy@gmail.com> (https://mjeanroy.com)',
contributors: [
'Mickael Jeanroy <mickael.jeanroy@gmail.com> (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 <mickael.jeanroy@gmail.com> (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);
Expand All @@ -183,16 +298,13 @@ describe('LicensePlugin', () => {
foo: {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
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'},
},
});

Expand All @@ -204,16 +316,13 @@ describe('LicensePlugin', () => {
foo: {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
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'},
},
});
});
Expand Down

0 comments on commit 8652b07

Please sign in to comment.