Skip to content

Commit

Permalink
fix: do not add dependency more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 21, 2016
1 parent 48b97dc commit 8ae542b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
36 changes: 21 additions & 15 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LicensePlugin {
constructor(options = {}) {
this._options = options;
this._cwd = process.cwd();
this._dependencies = [];
this._dependencies = {};
this._pkg = require(path.join(this._cwd, 'package.json'));
}

Expand Down Expand Up @@ -101,7 +101,7 @@ class LicensePlugin {
_,
moment,
pkg: this._pkg,
dependencies: this._dependencies,
dependencies: _.values(this._dependencies),
});

// Make a block comment if needed
Expand Down Expand Up @@ -145,19 +145,25 @@ class LicensePlugin {
* @return {void}
*/
addDependency(pkg) {
this._dependencies.push(_.pick(pkg, [
'name',
'author',
'contributors',
'maintainers',
'version',
'description',
'license',
'licenses',
'repository',
'homepage',
'private',
]));
const name = pkg.name;

if (!_.has(this._dependencies, name)) {
const dependency = _.pick(pkg, [
'name',
'author',
'contributors',
'maintainers',
'version',
'description',
'license',
'licenses',
'repository',
'homepage',
'private',
]);

this._dependencies[name] = dependency;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ describe('rollup-plugin-license', () => {
it('should return new plugin instance', () => {
const instance = plugin();
expect(instance).toBeDefined();
expect(instance._dependencies).toEqual([]);
expect(instance._dependencies).toEqual({});
});
});
80 changes: 71 additions & 9 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('LicensePlugin', () => {
const plugin = new LicensePlugin();
expect(plugin._cwd).toBeDefined();
expect(plugin._pkg).toBeDefined();
expect(plugin._dependencies).toEqual([]);
expect(plugin._dependencies).toEqual({});
});

it('should load pkg', () => {
Expand All @@ -44,17 +44,16 @@ describe('LicensePlugin', () => {

expect(result).not.toBeDefined();
expect(plugin.addDependency).toHaveBeenCalled();
expect(plugin._dependencies.length).toBe(1);
expect(plugin._dependencies).toEqual([
{
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,
},
]);
});
});

it('should load pkg and stop on cwd', () => {
Expand All @@ -67,7 +66,7 @@ describe('LicensePlugin', () => {

expect(result).not.toBeDefined();
expect(plugin.addDependency).not.toHaveBeenCalled();
expect(plugin._dependencies.length).toBe(0);
expect(plugin._dependencies).toEqual({});
});

it('should add dependency', () => {
Expand All @@ -90,21 +89,84 @@ describe('LicensePlugin', () => {

plugin.addDependency(pkg);

expect(plugin._dependencies.length).toBe(1);
expect(plugin._dependencies[0]).not.toBe(pkg);
expect(plugin._dependencies[0]).toEqual({
expect(plugin._dependencies.foo).toBeDefined();
expect(plugin._dependencies.foo).not.toBe(pkg);
expect(plugin._dependencies).toEqual({
foo: {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
description: 'Fake Description',
license: 'MIT',
homepage: 'https://www.google.fr',
private: true,
repository: {
type: 'GIT',
url: 'https://github.com/npm/npm.git',
},
},
});
});

it('should add dependency twice', () => {
const plugin = new LicensePlugin();
const pkg = {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@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);

expect(plugin._dependencies.foo).toBeDefined();
expect(plugin._dependencies.foo).not.toBe(pkg);
expect(plugin._dependencies).toEqual({
foo: {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
description: 'Fake Description',
license: 'MIT',
homepage: 'https://www.google.fr',
private: true,
repository: {
type: 'GIT',
url: 'https://github.com/npm/npm.git',
},
},
});

// Try to add the same pkg id

plugin.addDependency(pkg);

expect(plugin._dependencies).toEqual({
foo: {
name: 'foo',
version: '0.0.0',
author: 'Mickael Jeanroy <mickael.jeanroy@gmail.com>',
contributors: ['Test <test@gmail.com>'],
description: 'Fake Description',
license: 'MIT',
homepage: 'https://www.google.fr',
private: true,
repository: {
type: 'GIT',
url: 'https://github.com/npm/npm.git',
},
},
});
});

Expand Down

0 comments on commit 8ae542b

Please sign in to comment.