Skip to content

Commit d5cabcd

Browse files
committed
fix: add packageData to conventional-changelog-writer context
1 parent 44abdf9 commit d5cabcd

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const intoStream = require('into-stream');
55
const parser = require('conventional-commits-parser').sync;
66
const writer = require('conventional-changelog-writer');
77
const filter = require('conventional-commits-filter');
8+
const readPkgUp = require('read-pkg-up');
89
const debug = require('debug')('semantic-release:release-notes-generator');
910
const loadChangelogConfig = require('./lib/load-changelog-config');
1011
const HOSTS_CONFIG = require('./lib/hosts-config');
@@ -26,7 +27,7 @@ const HOSTS_CONFIG = require('./lib/hosts-config');
2627
* @returns {String} The changelog for all the commits in `context.commits`.
2728
*/
2829
async function generateNotes(pluginConfig, context) {
29-
const {commits, lastRelease, nextRelease, options} = context;
30+
const {commits, lastRelease, nextRelease, options, cwd} = context;
3031
const repositoryUrl = options.repositoryUrl.replace(/\.git$/i, '');
3132
const {parserOpts, writerOpts} = await loadChangelogConfig(pluginConfig, context);
3233

@@ -59,6 +60,7 @@ async function generateNotes(pluginConfig, context) {
5960
linkCompare: currentTag && previousTag,
6061
issue,
6162
commit,
63+
packageData: ((await readPkgUp({normalize: false, cwd})) || {}).package,
6264
},
6365
{host: hostConfig, linkCompare, linkReferences, commit: commitConfig, issue: issueConfig}
6466
);

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"get-stream": "^5.0.0",
2525
"import-from": "^3.0.0",
2626
"into-stream": "^5.0.0",
27-
"lodash": "^4.17.4"
27+
"lodash": "^4.17.4",
28+
"read-pkg-up": "^6.0.0"
2829
},
2930
"devDependencies": {
3031
"ava": "^2.0.0",
@@ -37,8 +38,12 @@
3738
"conventional-changelog-jshint": "^2.0.0",
3839
"cz-conventional-changelog": "^2.0.0",
3940
"escape-string-regexp": "^2.0.0",
41+
"fs-extra": "^8.0.1",
4042
"nyc": "^14.0.0",
43+
"proxyquire": "^2.1.0",
4144
"semantic-release": "^15.0.0",
45+
"sinon": "^7.3.2",
46+
"tempy": "^0.3.0",
4247
"xo": "^0.24.0"
4348
},
4449
"engines": {

test/integration.test.js

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import {promisify} from 'util';
2+
import path from 'path';
23
import test from 'ava';
4+
import {outputJson} from 'fs-extra';
35
import escape from 'escape-string-regexp';
6+
import tempy from 'tempy';
7+
import proxyquire from 'proxyquire';
8+
import {spy} from 'sinon';
49
import {generateNotes} from '..';
510

611
const cwd = process.cwd();
7-
const repositoryUrl = 'https://github.com/owner/repo';
12+
const host = 'https://github.com';
13+
const owner = 'owner';
14+
const repository = 'repo';
15+
const repositoryUrl = `${host}/${owner}/${repository}`;
816
const lastRelease = {gitTag: 'v1.0.0'};
917
const nextRelease = {gitTag: 'v2.0.0', version: '2.0.0'};
1018

@@ -25,6 +33,61 @@ test('Use "conventional-changelog-angular" by default', async t => {
2533
);
2634
});
2735

36+
test('Set conventional-changelog-writer context', async t => {
37+
const cwd = tempy.directory();
38+
const writer = spy(require('conventional-changelog-writer'));
39+
const {generateNotes} = proxyquire('..', {'conventional-changelog-writer': writer});
40+
41+
const commits = [
42+
{hash: '111', message: 'fix(scope1): First fix'},
43+
{hash: '222', message: 'feat(scope2): Second feature'},
44+
];
45+
await generateNotes({}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits});
46+
47+
t.deepEqual(writer.args[0][0], {
48+
version: nextRelease.version,
49+
host,
50+
owner,
51+
repository,
52+
previousTag: lastRelease.gitTag,
53+
currentTag: nextRelease.gitTag,
54+
linkCompare: lastRelease.gitTag,
55+
issue: 'issues',
56+
commit: 'commit',
57+
packageData: undefined,
58+
linkReferences: undefined,
59+
});
60+
});
61+
62+
test('Set conventional-changelog-writer context with package.json', async t => {
63+
const cwd = tempy.directory();
64+
const writer = spy(require('conventional-changelog-writer'));
65+
const {generateNotes} = proxyquire('..', {'conventional-changelog-writer': writer});
66+
67+
const packageData = {name: 'package', version: '0.0.0'};
68+
await outputJson(path.resolve(cwd, 'package.json'), packageData);
69+
70+
const commits = [
71+
{hash: '111', message: 'fix(scope1): First fix'},
72+
{hash: '222', message: 'feat(scope2): Second feature'},
73+
];
74+
await generateNotes({}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits});
75+
76+
t.deepEqual(writer.args[0][0], {
77+
version: nextRelease.version,
78+
host,
79+
owner,
80+
repository,
81+
previousTag: lastRelease.gitTag,
82+
currentTag: nextRelease.gitTag,
83+
linkCompare: lastRelease.gitTag,
84+
issue: 'issues',
85+
commit: 'commit',
86+
packageData,
87+
linkReferences: undefined,
88+
});
89+
});
90+
2891
test('Accept a "preset" option', async t => {
2992
const commits = [
3093
{hash: '111', message: 'Fix: First fix (fixes #123)'},

0 commit comments

Comments
 (0)