Skip to content

Commit

Permalink
fix: create directory if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 28, 2016
1 parent aab2c38 commit 2d2904f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"lodash": "4.17.2",
"magic-string": "0.17.0",
"mkdirp": "0.5.1",
"moment": "2.17.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const _ = require('lodash');
const moment = require('moment');
const MagicString = require('magic-string');
Expand Down Expand Up @@ -202,6 +203,9 @@ class LicensePlugin {
if (output) {
this.debug(`exporting third-party summary to ${output}`);

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

const includePrivate = thirdParty.includePrivate;
const text = _.chain(this._dependencies)
.values()
Expand Down
45 changes: 45 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,51 @@ describe('LicensePlugin', () => {
});
});

it('should display single dependency and create directory if needed', (done) => {
const file = path.join(tmpDir.name, 'output', 'third-party.txt');
const instance = new LicensePlugin({
thirdParty: {
output: file,
},
});

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

const result = instance.exportThirdParties();

expect(result).not.toBeDefined();

fs.readFile(file, 'utf-8', (err, content) => {
if (err) {
done.fail(err);
return;
}

const txt = content.toString();
expect(txt).toBeDefined();
expect(txt).toEqual(
`Name: foo\n` +
`Version: 1.0.0\n` +
`License: MIT\n` +
`Private: false\n` +
`Description: Foo Package\n` +
`Author: Mickael Jeanroy <mickael.jeanroy@gmail.com>`
);

done();
});
});

it('should display list of dependencies', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const instance = new LicensePlugin({
Expand Down

0 comments on commit 2d2904f

Please sign in to comment.