Skip to content

Commit 5d50346

Browse files
authored
Custom templating (#34)
* Introduce custom templating * Remove console.logs and remove unused chalk * Indent js lines * Get the options from a different gren.json * Add the Templating system * Remove extra variable for config file * Simplify label templating * Use deep assign to merge the config file for templating * Add deep-assign to package.json * Update the readme file based on the templating system
1 parent a4cca14 commit 5d50346

File tree

5 files changed

+136
-32
lines changed

5 files changed

+136
-32
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,56 @@ Following the options for the module:
6969
- `--include-messages=merges|commits|all` used to filter the messages added to the release notes. Default: `commits`
7070
- `--override=true|false` Override the release notes if existing. Default: `false`
7171

72+
### Config file
73+
74+
You can create a `.gren.json` file where the task will be ran, where to specify your options.
75+
The options in the file would be camelCase *e.g*:
76+
77+
```json
78+
{
79+
"action": "release",
80+
"timeWrap": "history",
81+
"dataSource": "commits"
82+
}
83+
```
84+
85+
### Templates
86+
87+
With **gren** you can decide how you want to output the informations, using the templates.
88+
89+
The default configuration is the following:
90+
91+
```json
92+
{
93+
"...": "...",
94+
"template": {
95+
"commit": "- {{message}}",
96+
"issue": "- {{labels}} {{name}} {{link}}",
97+
"issueInfo": {
98+
"labels": "{{labels}}",
99+
"label": "[**{{label}}**]",
100+
"name": "{{name}}",
101+
"link": "[{{text}}]({{url}})"
102+
},
103+
"release": "## {{release}} {{date}}",
104+
"releaseInfo": {
105+
"release": "{{release}}",
106+
"date": "({{date}})"
107+
}
108+
}
109+
}
110+
```
111+
112+
To customise the templates you need to change the `.gren.json` config file. You only need to specify what you want to change *e.g.*
113+
114+
```json
115+
{
116+
"template": {
117+
"issue": "- {{name}}"
118+
}
119+
}
120+
```
121+
72122
## Examples
73123

74124
The ways to use **gren** are various.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
"dependencies": {
3535
"chalk": "^1.1.3",
3636
"connectivity": "^1.0.0",
37+
"deep-assign": "^2.0.0",
3738
"es6-promise": "^3.2.1",
3839
"github-api": "^3.0.0",
39-
"object-assign": "^4.1.0"
4040
},
4141
"devDependencies": {
4242
"eslint": "^3.6.0",

src/gren.js

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
var utils = require('./utils');
44
var githubInfo = require('./github-info');
5+
var template = require('./template');
56
var Github = require('github-api');
67
var fs = require('fs');
78
var chalk = require('chalk');
89
var Promise = Promise || require('es6-promise').Promise;
910
var connectivity = require('connectivity');
10-
var ObjectAssign = require('object-assign');
11+
var templateConfig = require('./templates.json');
12+
var ObjectAssign = require('deep-assign');
13+
var configFile = fs.existsSync(process.cwd() + '/.gren.json') ? require(process.cwd() + '/.gren.json') : {};
1114

1215
var defaults = {
1316
tags: false,
@@ -20,11 +23,12 @@ var defaults = {
2023
includeMessages: 'commits', // || merges || all
2124
prerelease: false,
2225
dateZero: new Date(0),
23-
override: false
26+
override: false,
27+
template: templateConfig
2428
};
2529

2630
/**
27-
* Edit arelease from a given tag (in the options)
31+
* Edit a release from a given tag (in the options)
2832
*
2933
* @since 0.5.0
3034
* @private
@@ -289,8 +293,10 @@ function getLastTwoReleases(gren) {
289293
*
290294
* @return {string}
291295
*/
292-
function templateCommits(message) {
293-
return '- ' + message;
296+
function templateCommits(gren, message) {
297+
return template.generate({
298+
message: message
299+
}, gren.options.template.commit);
294300
}
295301

296302
/**
@@ -303,11 +309,16 @@ function templateCommits(message) {
303309
*
304310
* @return {string}
305311
*/
306-
function templateLabels(issue) {
307-
return issue.labels ? issue.labels.map(function(label) {
308-
return '[**' + label.name + '**] ';
309-
})
310-
.join('') : '[closed]';
312+
function templateLabels(gren, issue) {
313+
if (!issue.labels.length) {
314+
issue.labels.push({name: 'closed'});
315+
}
316+
317+
return issue.labels.map(function(label) {
318+
return template.generate({
319+
label: label.name
320+
}, gren.options.template.issueInfo.label);
321+
}).join('');
311322
}
312323

313324
/**
@@ -320,11 +331,14 @@ function templateLabels(issue) {
320331
*
321332
* @return {string}
322333
*/
323-
function templateBlock(block) {
334+
function templateBlock(gren, block) {
324335
var date = new Date(block.date);
336+
var releaseTemplate = template.generate({
337+
release: block.name,
338+
date: utils.formatDate(date)
339+
}, template.generate(gren.options.template.releaseInfo, gren.options.template.release));
325340

326-
return '## ' + block.name + ' (' + utils.formatDate(date) + ')' + '\n\n' +
327-
block.body;
341+
return releaseTemplate + '\n\n' + block.body;
328342
}
329343

330344
/**
@@ -337,8 +351,18 @@ function templateBlock(block) {
337351
*
338352
* @return {string}
339353
*/
340-
function templateIssue(issue) {
341-
return '- ' + templateLabels(issue) + issue.title + ' [#' + issue.number + '](' + issue.html_url + ')';
354+
function templateIssue(gren, issue) {
355+
var issueTemplate = template.generate(gren.options.template.issueInfo, gren.options.template.issue);
356+
var nameTemplate = template.generate({
357+
name: issue.title
358+
}, gren.options.template.issueInfo.name);
359+
360+
return template.generate({
361+
labels: templateLabels(gren, issue),
362+
name: nameTemplate,
363+
text: '#' + issue.number,
364+
url: issue.html_url
365+
}, issueTemplate);
342366
}
343367

344368
/**
@@ -351,10 +375,10 @@ function templateIssue(issue) {
351375
*
352376
* @return {string}
353377
*/
354-
function templateChangelog(blocks) {
378+
function templateChangelog(gren, blocks) {
355379
return '# Changelog\n\n' +
356380
blocks
357-
.map(templateBlock)
381+
.map(templateBlock.bind(null, gren))
358382
.join('\n\n --- \n\n');
359383
}
360384

@@ -405,7 +429,7 @@ function generateCommitsBody(gren, messages) {
405429

406430
return filterMap.commits(message);
407431
})
408-
.map(templateCommits)
432+
.map(templateCommits.bind(null, gren))
409433
.join('\n');
410434
}
411435

@@ -498,19 +522,19 @@ function getClosedIssues(gren, releaseRanges) {
498522

499523
return gren.issues.listIssues({
500524
state: 'closed',
501-
since: releaseRanges[0][1].date
525+
since: releaseRanges[releaseRanges.length - 1][1].date
502526
})
503-
.then(function(response) {
504-
loaded();
527+
.then(function(response) {
528+
loaded();
505529

506-
var filteredIssues = response.data.filter(function(issue) {
507-
return !issue.pull_request;
508-
});
530+
var filteredIssues = response.data.filter(function(issue) {
531+
return !issue.pull_request;
532+
});
509533

510-
process.stdout.write(filteredIssues.length + ' issues found\n');
534+
process.stdout.write(filteredIssues.length + ' issues found\n');
511535

512-
return filteredIssues;
513-
});
536+
return filteredIssues;
537+
});
514538
}
515539

516540
/**
@@ -539,7 +563,7 @@ function getIssueBlocks(gren, releaseRanges) {
539563
Date.parse(range[0].date)
540564
);
541565
})
542-
.map(templateIssue);
566+
.map(templateIssue.bind(null, gren));
543567

544568
return {
545569
id: range[0].id,
@@ -564,7 +588,7 @@ function getIssueBlocks(gren, releaseRanges) {
564588
*/
565589
function sortReleasesByDate(releaseDates) {
566590
return releaseDates.sort(function(release1, release2) {
567-
return new Date(release1.date) < new Date(release2.date) ? 1 : -1;
591+
return new Date(release2.date) - new Date(release1.date);
568592
});
569593
}
570594

@@ -627,7 +651,7 @@ function generateReleaseDatesChangelogBody(gren) {
627651
return dataSource[gren.options.dataSource](gren, releaseRanges);
628652
})
629653
.then(function(blocks) {
630-
return templateChangelog(blocks);
654+
return templateChangelog(gren, blocks);
631655
});
632656
}
633657

@@ -733,7 +757,8 @@ function hasNetwork() {
733757
* @constructor
734758
*/
735759
function GithubReleaseNotes(options) {
736-
this.options = ObjectAssign({}, defaults, options || utils.getBashOptions(process.argv));
760+
this.options = ObjectAssign({}, defaults, configFile, options || utils.getBashOptions(process.argv));
761+
console.log(this.options.template);
737762
this.options.tags = this.options.tags && this.options.tags.split(',');
738763
this.repo = null;
739764
this.issues = null;

src/template.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
function generate(placeholders, string) {
4+
return Object.keys(placeholders)
5+
.reduce(function(carry, placeholder) {
6+
var placeholderRegExp = new RegExp('{{' + placeholder + '}}', 'g');
7+
8+
return carry.replace(placeholderRegExp, placeholders[placeholder]);
9+
}, string);
10+
}
11+
12+
module.exports = {
13+
generate: generate
14+
};

src/templates.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"commit": "- {{message}}",
3+
"issue": "- {{labels}} {{name}} {{link}}",
4+
"issueInfo": {
5+
"labels": "{{labels}}",
6+
"label": "[**{{label}}**]",
7+
"name": "{{name}}",
8+
"link": "[{{text}}]({{url}})"
9+
},
10+
"release": "## {{release}} {{date}}",
11+
"releaseInfo": {
12+
"release": "{{release}}",
13+
"date": "({{date}})"
14+
}
15+
}

0 commit comments

Comments
 (0)