Skip to content

Commit

Permalink
feat(pkg): load closest package.json by default
Browse files Browse the repository at this point in the history
Use read-pkg and read-pkg-up to load package.json.
Also tweak the error message a little bit.

Closes #91
  • Loading branch information
stevemao committed Sep 28, 2015
1 parent 6bed043 commit 5942809
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 deletions.
45 changes: 27 additions & 18 deletions index.js
Expand Up @@ -2,11 +2,12 @@
var conventionalCommitsParser = require('conventional-commits-parser');
var conventionalChangelogWriter = require('conventional-changelog-writer');
var dateFormat = require('dateformat');
var fs = require('fs');
var getPkgRepo = require('get-pkg-repo');
var gitRawCommits = require('git-raw-commits');
var gitSemverTags = require('git-semver-tags');
var Q = require('q');
var readPkg = require('read-pkg');
var readPkgUp = require('read-pkg-up');
var stream = require('stream');
var through = require('through2');
var url = require('url');
Expand All @@ -32,7 +33,6 @@ function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts,

options = _.merge({
pkg: {
path: 'package.json',
transform: function(pkg) {
return pkg;
}
Expand All @@ -58,7 +58,6 @@ function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts,
}
}, options);

options.pkg = options.pkg || {};
var loadPreset = options.preset;

if (loadPreset) {
Expand All @@ -71,7 +70,13 @@ function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts,
}
}

pkgPromise = Q.nfcall(fs.readFile, options.pkg.path, 'utf8');
if (options.pkg) {
if (options.pkg.path) {
pkgPromise = Q(readPkg(options.pkg.path)); // jshint ignore:line
} else {
pkgPromise = Q(readPkgUp()); // jshint ignore:line
}
}

semverTagsPromise = Q.nfcall(gitSemverTags);

Expand All @@ -95,15 +100,23 @@ function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts,
preset = {};
}

if (pkgObj.state === 'fulfilled') {
pkg = pkgObj.value;
try {
pkg = JSON.parse(pkg);
if (options.pkg) {
if (pkgObj.state === 'fulfilled') {
if (options.pkg.path) {
pkg = pkgObj.value;
} else {
pkg = pkgObj.value.pkg;
}

pkg = options.pkg.transform(pkg);
context.version = context.version || pkg.version;
context.packageData = pkg;

repo = getPkgRepo(pkg);
try {
repo = getPkgRepo(pkg);
} catch (err) {
repo = {};
}

if (repo.type) {
var browse = repo.browse();
Expand All @@ -112,12 +125,8 @@ function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts,
context.owner = context.owner || repo.user;
context.repository = context.repository || repo.project;
}
} catch (err) {
options.warn('package.json: "' + options.pkg.path + '" cannot be parsed');
}
} else {
if (options.pkg && options.pkg.path) {
options.warn('package.json: "' + options.pkg.path + '" does not exist');
} else if (options.pkg.path) {
options.warn(pkgObj.reason.toString());
}
}

Expand All @@ -129,13 +138,13 @@ function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts,
if (context.host && (!context.issue || !context.commit || !parserOpts || !parserOpts.referenceActions)) {
var type;

if (repo && repo.type) {
type = repo.type;
} else {
if (context.host) {
var match = context.host.match(rhosts);
if (match) {
type = match[0];
}
} else if (repo && repo.type) {
type = repo.type;
}

if (type) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -36,6 +36,8 @@
"lodash": "^3.9.3",
"meow": "^3.3.0",
"q": "^1.4.1",
"read-pkg": "^1.1.0",
"read-pkg-up": "^1.0.1",
"semver": "^5.0.1",
"tempfile": "^1.1.1",
"through2": "^2.0.0"
Expand Down
41 changes: 35 additions & 6 deletions test/test.js
Expand Up @@ -201,10 +201,20 @@ describe('conventionalChangelog', function() {
}));
});

it('should read the closest package.json by default', function(done) {
conventionalChangelog()
.pipe(through(function(chunk) {
expect(chunk.toString()).to.include('closes [#1](https://github.com/ajoslin/conventional-changelog/issues/1)');

done();
}));
});

it('should read host configs if only `parserOpts.referenceActions` is missing', function(done) {
conventionalChangelog({}, {
host: 'github',
repository: 'b/a',
owner: 'b',
repository: 'a',
issue: 'issue',
commit: 'commits'
}, {}, {}).pipe(through(function(chunk) {
Expand All @@ -220,7 +230,8 @@ describe('conventionalChangelog', function() {
it('should read github\'s host configs', function(done) {
conventionalChangelog({}, {
host: 'github',
repository: 'b/a'
owner: 'b',
repository: 'a'
}, {}, {}).pipe(through(function(chunk) {
chunk = chunk.toString();

Expand All @@ -234,7 +245,8 @@ describe('conventionalChangelog', function() {
it('should read bitbucket\'s host configs', function(done) {
conventionalChangelog({}, {
host: 'bitbucket',
repository: 'b/a'
owner: 'b',
repository: 'a'
}, {}, {}).pipe(through(function(chunk) {
chunk = chunk.toString();

Expand All @@ -248,7 +260,8 @@ describe('conventionalChangelog', function() {
it('should read gitlab\'s host configs', function(done) {
conventionalChangelog({}, {
host: 'gitlab',
repository: 'b/a'
owner: 'b',
repository: 'a'
}, {}, {}).pipe(through(function(chunk) {
chunk = chunk.toString();

Expand Down Expand Up @@ -598,7 +611,7 @@ describe('conventionalChangelog', function() {
path: 'no'
},
warn: function(warning) {
expect(warning).to.equal('package.json: "no" does not exist');
expect(warning).to.include('Error');

done();
}
Expand All @@ -611,7 +624,7 @@ describe('conventionalChangelog', function() {
path: __dirname + '/fixtures/_malformation.json'
},
warn: function(warning) {
expect(warning).to.equal('package.json: "' + __dirname + '/fixtures/_malformation.json" cannot be parsed');
expect(warning).to.include('Error');

done();
}
Expand All @@ -632,6 +645,22 @@ describe('conventionalChangelog', function() {
});
});

it('should error if there is an error in `options.pkg.transform`', function(done) {
conventionalChangelog({
pkg: {
path: __dirname + '/fixtures/_short.json',
transform: function() {
undefined.a = 10;
}
}
})
.on('error', function(err) {
expect(err.message).to.include('undefined');

done();
});
});

it('should error if it errors in git-raw-commits', function(done) {
conventionalChangelog({}, {}, {
unknowOptions: false
Expand Down

0 comments on commit 5942809

Please sign in to comment.