Skip to content

Commit

Permalink
Add support for custom date format configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Mar 9, 2018
1 parent c1688c4 commit 44e8a16
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ As `pr-log` reads repository information from your project you have to add the `
}
```

### Changelog formatting

#### Custom date format

If you want to use a custom date format you can configure `pr-log.dateFormat` in your `package.json`. For example:

```json
{
"pr-log": { "dateFormat": "DD.MM.YYYY" }
}
```

Please refer to the [moment.js documentation](https://momentjs.com/docs/#/displaying/format/) for details about the format tokens.

## Usage

To create or update your changelog run
Expand Down
5 changes: 3 additions & 2 deletions bin/pr-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ const findRemoteAlias = findRemoteAliasFactory({ git });
const githubClient = createGithubClient();
const getMergedPullRequests = getMergedPullRequestsFactory({ githubClient, git, getPullRequestLabel });
const getCurrentDate = () => new Date();
const packageInfo = require(path.join(process.cwd(), 'package.json'));
const dependencies = {
githubClient,
prependFile: promisify(prepend),
packageInfo: require(path.join(process.cwd(), 'package.json')),
packageInfo,
ensureCleanLocalGitState: ensureCleanLocalGitState({ git, findRemoteAlias }),
getMergedPullRequests,
createChangelog: createChangelogFactory({ getCurrentDate })
createChangelog: createChangelogFactory({ getCurrentDate, packageInfo })
};
const cliAgent = createCliAgent(dependencies);

Expand Down
7 changes: 5 additions & 2 deletions lib/createChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ function formatLinkToPullRequest(repo, pullRequestId) {
return `[#${pullRequestId}](https://github.com/${repo}/pull/${pullRequestId})`;
}

export default ({ getCurrentDate }) => {
export default ({ getCurrentDate, packageInfo }) => {
const defaultDateFormat = 'MMMM D, YYYY';
const { dateFormat = defaultDateFormat } = packageInfo['pr-log'] || {};

return function createChangelog(newVersionNumber, validLabels, mergedPullRequests, repo) {
const groupedPullRequests = R.groupBy(R.prop('label'), mergedPullRequests);
const date = moment(getCurrentDate()).locale('en').format('MMMM D, YYYY');
const date = moment(getCurrentDate()).locale('en').format(dateFormat);
const title = `## ${newVersionNumber} (${date})`;

let changelog = `${title}\n\n`;
Expand Down
15 changes: 12 additions & 3 deletions test/unit/lib/createChangelogSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ import createChangelogFactory from '../../../lib/createChangelog';
import defaultValidLabels from '../../../lib/validLabels';

test('contains a title with the version number and the formatted date', (t) => {
const createChangelog = createChangelogFactory({ getCurrentDate: () => new Date(0) });
const createChangelog = createChangelogFactory({ getCurrentDate: () => new Date(0), packageInfo: {} });
const changelog = createChangelog('1.0.0', defaultValidLabels, []);
const expectedTitle = '## 1.0.0 (January 1, 1970)';

t.true(changelog.includes(expectedTitle));
});

test('format the date with a custom date format', (t) => {
const packageInfo = { 'pr-log': { dateFormat: 'DD.MM.YYYY' } };
const createChangelog = createChangelogFactory({ getCurrentDate: () => new Date(0), packageInfo });
const changelog = createChangelog('1.0.0', defaultValidLabels, []);
const expectedTitle = '## 1.0.0 (01.01.1970)';

t.true(changelog.includes(expectedTitle));
});

test('creates a formatted changelog', (t) => {
const createChangelog = createChangelogFactory({ getCurrentDate: () => new Date(0) });
const createChangelog = createChangelogFactory({ getCurrentDate: () => new Date(0), packageInfo: {} });
const mergedPullRequests = [
{
id: '1',
Expand Down Expand Up @@ -48,7 +57,7 @@ test('creates a formatted changelog', (t) => {
});

test('uses custom labels when provided', (t) => {
const createChangelog = createChangelogFactory({ getCurrentDate: () => new Date(0) });
const createChangelog = createChangelogFactory({ getCurrentDate: () => new Date(0), packageInfo: {} });
const customValidLabels = {
core: 'Core Features',
addons: 'Addons'
Expand Down

0 comments on commit 44e8a16

Please sign in to comment.