Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom date format configuration #143

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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