Skip to content

Commit

Permalink
fix: add packageData to conventional-changelog-writer context
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Jun 26, 2019
1 parent 44abdf9 commit d5cabcd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const intoStream = require('into-stream');
const parser = require('conventional-commits-parser').sync;
const writer = require('conventional-changelog-writer');
const filter = require('conventional-commits-filter');
const readPkgUp = require('read-pkg-up');
const debug = require('debug')('semantic-release:release-notes-generator');
const loadChangelogConfig = require('./lib/load-changelog-config');
const HOSTS_CONFIG = require('./lib/hosts-config');
Expand All @@ -26,7 +27,7 @@ const HOSTS_CONFIG = require('./lib/hosts-config');
* @returns {String} The changelog for all the commits in `context.commits`.
*/
async function generateNotes(pluginConfig, context) {
const {commits, lastRelease, nextRelease, options} = context;
const {commits, lastRelease, nextRelease, options, cwd} = context;
const repositoryUrl = options.repositoryUrl.replace(/\.git$/i, '');
const {parserOpts, writerOpts} = await loadChangelogConfig(pluginConfig, context);

Expand Down Expand Up @@ -59,6 +60,7 @@ async function generateNotes(pluginConfig, context) {
linkCompare: currentTag && previousTag,
issue,
commit,
packageData: ((await readPkgUp({normalize: false, cwd})) || {}).package,
},
{host: hostConfig, linkCompare, linkReferences, commit: commitConfig, issue: issueConfig}
);
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"get-stream": "^5.0.0",
"import-from": "^3.0.0",
"into-stream": "^5.0.0",
"lodash": "^4.17.4"
"lodash": "^4.17.4",
"read-pkg-up": "^6.0.0"
},
"devDependencies": {
"ava": "^2.0.0",
Expand All @@ -37,8 +38,12 @@
"conventional-changelog-jshint": "^2.0.0",
"cz-conventional-changelog": "^2.0.0",
"escape-string-regexp": "^2.0.0",
"fs-extra": "^8.0.1",
"nyc": "^14.0.0",
"proxyquire": "^2.1.0",
"semantic-release": "^15.0.0",
"sinon": "^7.3.2",
"tempy": "^0.3.0",
"xo": "^0.24.0"
},
"engines": {
Expand Down
65 changes: 64 additions & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {promisify} from 'util';
import path from 'path';
import test from 'ava';
import {outputJson} from 'fs-extra';
import escape from 'escape-string-regexp';
import tempy from 'tempy';
import proxyquire from 'proxyquire';
import {spy} from 'sinon';
import {generateNotes} from '..';

const cwd = process.cwd();
const repositoryUrl = 'https://github.com/owner/repo';
const host = 'https://github.com';
const owner = 'owner';
const repository = 'repo';
const repositoryUrl = `${host}/${owner}/${repository}`;
const lastRelease = {gitTag: 'v1.0.0'};
const nextRelease = {gitTag: 'v2.0.0', version: '2.0.0'};

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

test('Set conventional-changelog-writer context', async t => {
const cwd = tempy.directory();
const writer = spy(require('conventional-changelog-writer'));
const {generateNotes} = proxyquire('..', {'conventional-changelog-writer': writer});

const commits = [
{hash: '111', message: 'fix(scope1): First fix'},
{hash: '222', message: 'feat(scope2): Second feature'},
];
await generateNotes({}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits});

t.deepEqual(writer.args[0][0], {
version: nextRelease.version,
host,
owner,
repository,
previousTag: lastRelease.gitTag,
currentTag: nextRelease.gitTag,
linkCompare: lastRelease.gitTag,
issue: 'issues',
commit: 'commit',
packageData: undefined,
linkReferences: undefined,
});
});

test('Set conventional-changelog-writer context with package.json', async t => {
const cwd = tempy.directory();
const writer = spy(require('conventional-changelog-writer'));
const {generateNotes} = proxyquire('..', {'conventional-changelog-writer': writer});

const packageData = {name: 'package', version: '0.0.0'};
await outputJson(path.resolve(cwd, 'package.json'), packageData);

const commits = [
{hash: '111', message: 'fix(scope1): First fix'},
{hash: '222', message: 'feat(scope2): Second feature'},
];
await generateNotes({}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits});

t.deepEqual(writer.args[0][0], {
version: nextRelease.version,
host,
owner,
repository,
previousTag: lastRelease.gitTag,
currentTag: nextRelease.gitTag,
linkCompare: lastRelease.gitTag,
issue: 'issues',
commit: 'commit',
packageData,
linkReferences: undefined,
});
});

test('Accept a "preset" option', async t => {
const commits = [
{hash: '111', message: 'Fix: First fix (fixes #123)'},
Expand Down

0 comments on commit d5cabcd

Please sign in to comment.