Skip to content

Commit

Permalink
feat: allow dependency output to be a callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Aug 8, 2019
1 parent 7e98ede commit e549ed8
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 11 deletions.
22 changes: 16 additions & 6 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,24 +323,34 @@ module.exports = class LicensePlugin {
}

const output = thirdParty.output;
if (output) {
this.debug(`exporting third-party summary to ${output}`);

// Create directory if it does not already exist.
mkdirp.sync(path.parse(output).dir);

if (output) {
const includePrivate = thirdParty.includePrivate;
const text = _.chain(this._dependencies)
const dependencies = _.chain(this._dependencies)
.values()
.filter((dependency) => includePrivate || !dependency.private)
.value();

if (_.isFunction(output)) {
output(dependencies);
return;
}

// Default is to export to given file.
const text = _.chain(dependencies)
.map((dependency) => dependency.text())
.join(`${EOL}${EOL}---${EOL}${EOL}`)
.trim()
.value();

const encoding = thirdParty.encoding || 'utf-8';

this.debug(`exporting third-party summary to ${output}`);
this.debug(`use encoding: ${encoding}`);

// Create directory if it does not already exist.
mkdirp.sync(path.parse(output).dir);

fs.writeFileSync(output, text || 'No third parties dependencies', {
encoding,
});
Expand Down
37 changes: 37 additions & 0 deletions test/integration/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,43 @@ describe('Dependency', () => {
});
});

it('should generate bundle and export dependencies to given function', (done) => {
const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const thirdPartyOutput = jasmine.createSpy('thirdPartyOutput');

const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),

output: {
file: bundleOutput,
format: 'es',
},

plugins: [
nodeResolve(),
commonjs(),

licensePlugin({
thirdParty: {
output: thirdPartyOutput,
},
}),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig.output))
.then(() => {
expect(thirdPartyOutput).toHaveBeenCalledWith([
jasmine.objectContaining({
name: 'lodash',
}),
]);

done();
});
});

it('should generate bundle with license header', (done) => {
const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const banner = 'test banner';
Expand Down
112 changes: 107 additions & 5 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ describe('LicensePlugin', () => {

fs.readFile(file, 'utf-8', (err, content) => {
if (err) {
console.log('Error reading file');
done.fail(err);
return;
}
Expand All @@ -1009,7 +1010,7 @@ describe('LicensePlugin', () => {
});
});

it('should display list of dependencies', (done) => {
it('should export list of dependencies to given file', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const instance = new LicensePlugin({
thirdParty: {
Expand Down Expand Up @@ -1070,7 +1071,7 @@ describe('LicensePlugin', () => {
});
});

it('should display list of dependencies with custom encoding', (done) => {
it('should export list of dependencies with custom encoding to given file', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const encoding = 'ascii';
const instance = new LicensePlugin({
Expand Down Expand Up @@ -1138,7 +1139,7 @@ describe('LicensePlugin', () => {
});
});

it('should display default message without dependencies', (done) => {
it('should export default message without ant dependencies', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const instance = new LicensePlugin({
thirdParty: {
Expand All @@ -1165,7 +1166,7 @@ describe('LicensePlugin', () => {
});
});

it('should not display private dependencies by default', (done) => {
it('should not export private dependencies by default', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const instance = new LicensePlugin({
thirdParty: {
Expand Down Expand Up @@ -1218,7 +1219,7 @@ describe('LicensePlugin', () => {
});
});

it('should display private dependencies if enabled', (done) => {
it('should export dependencies to output file if enabled', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const instance = new LicensePlugin({
thirdParty: {
Expand Down Expand Up @@ -1310,4 +1311,105 @@ describe('LicensePlugin', () => {
expect(result).not.toBeDefined();
expect(fs.writeFileSync).not.toHaveBeenCalled();
});

it('should export list of non-private dependencies to output function', () => {
const output = jasmine.createSpy('output');
const instance = new LicensePlugin({
thirdParty: {
output,
},
});

instance.addDependency({
name: 'foo',
version: '1.0.0',
description: 'Foo Package',
license: 'MIT',
private: false,
author: {
name: 'Mickael Jeanroy',
email: 'mickael.jeanroy@gmail.com',
},
});

instance.addDependency({
name: 'bar',
version: '2.0.0',
description: 'Bar Package',
license: 'Apache 2.0',
private: true,
});

const result = instance.exportThirdParties();

expect(result).not.toBeDefined();
expect(output).toHaveBeenCalledWith([
{
name: 'foo',
author: {
name: 'Mickael Jeanroy',
email: 'mickael.jeanroy@gmail.com',
},
version: '1.0.0',
description: 'Foo Package',
license: 'MIT',
private: false,
},
]);
});

it('should export list of dependencies to output function including private if enabled', () => {
const output = jasmine.createSpy('output');
const includePrivate = true;
const instance = new LicensePlugin({
thirdParty: {
output,
includePrivate,
},
});

instance.addDependency({
name: 'foo',
version: '1.0.0',
description: 'Foo Package',
license: 'MIT',
private: false,
author: {
name: 'Mickael Jeanroy',
email: 'mickael.jeanroy@gmail.com',
},
});

instance.addDependency({
name: 'bar',
version: '2.0.0',
description: 'Bar Package',
license: 'Apache 2.0',
private: true,
});

const result = instance.exportThirdParties();

expect(result).not.toBeDefined();
expect(output).toHaveBeenCalledWith([
{
name: 'foo',
author: {
name: 'Mickael Jeanroy',
email: 'mickael.jeanroy@gmail.com',
},
version: '1.0.0',
description: 'Foo Package',
license: 'MIT',
private: false,
},
{
name: 'bar',
version: '2.0.0',
description: 'Bar Package',
license: 'Apache 2.0',
private: true,
},
]);
});
});

0 comments on commit e549ed8

Please sign in to comment.