Skip to content

Commit 083f03e

Browse files
authored
Template functions (#54)
* Switch to js configuration for .grenrc * Ignore a few files for npm * Simplify the template options, change a dependency for deep-assign object * Update docs for the template system * Remove extra console.log * Run eslint * Indent utils.js
1 parent dbe212e commit 083f03e

File tree

8 files changed

+115
-100
lines changed

8 files changed

+115
-100
lines changed

.grenrc renamed to .grenrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
module.exports = {
22
"ignoreIssuesWith": [
33
"duplicate",
44
"wontfix",

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
docs/
22
node_modules/
3+
Gruntfile.js
4+
.grenrc
5+
.grenrc.*
6+
.jsdoc.conf.json
7+
.eslintrc
8+
.editorconfig
9+
.travis.yml

docs/options.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,25 @@ You can configure the output of **gren** using templates. Set your own configura
7777
{
7878
"template": {
7979
"commit": "- {{message}}",
80-
"issue": "- {{labels}} {{name}} {{link}}",
81-
"issueInfo": {
82-
"labels": "{{labels}}",
83-
"label": "[**{{label}}**]",
84-
"name": "{{name}}",
85-
"link": "[{{text}}]({{url}})"
86-
},
87-
"release": "## {{release}} {{date}}",
88-
"releaseInfo": {
89-
"release": "{{release}}",
90-
"date": "({{date}})"
91-
}
80+
"issue": "- {{labels}} {{name}} [{{text}}]({{url}})",
81+
"label": "[**{{label}}**]",
82+
"release": "## {{release}} {{date}}"
9283
}
9384
}
9485
```
9586
{% endraw %}
87+
88+
If you're using a `.grenrc.js` config file, you can use JavaScript to manipulate the templates using functions as values.
89+
The function will have an object as first parameter, containing all the values to display. _i.e._
90+
91+
```javascript
92+
/* .grenrc.js */
93+
94+
module.exports = {
95+
template: {
96+
issue: function (placeholders) {
97+
return '- ' + placeholders.labels + ' | ' + placeholders.name.toLowerCase();
98+
}
99+
}
100+
}
101+
```

package.json

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
{
2-
"name": "github-release-notes",
3-
"version": "0.6.3",
4-
"description": "Node module to publish release notes based on commits between the last two tags.",
5-
"main": "./github-release-notes.js",
6-
"scripts": {
7-
"start": "node github-release-notes.js",
8-
"test": "grunt test",
9-
"docs": "node_modules/.bin/jsdoc -c .jsdoc.conf.json --verbose"
10-
},
11-
"repository": {
12-
"type": "git",
13-
"url": "git+https://github.com/alexcanessa/github-release-notes.git"
14-
},
15-
"preferGlobal": "true",
16-
"bin": {
17-
"gren": "bin/gren.js"
18-
},
19-
"keywords": [
20-
"Github",
21-
"Release",
22-
"notes",
23-
"Tag",
24-
"Changelog",
25-
"Changelog Generator",
26-
"Issues",
27-
"Commits"
28-
],
29-
"author": "alexcanessa",
30-
"contributors": [
31-
"Alex Canessa <[email protected]>"
32-
],
33-
"license": "ISC",
34-
"bugs": {
35-
"url": "https://github.com/alexcanessa/github-release-notes/issues"
36-
},
37-
"homepage": "https://github.com/alexcanessa/github-release-notes#readme",
38-
"dependencies": {
39-
"chalk": "^1.1.3",
40-
"connectivity": "^1.0.0",
41-
"deep-assign": "^2.0.0",
42-
"es6-promise": "^3.2.1",
43-
"github-api": "^3.0.0",
44-
"require-yaml": "0.0.1"
45-
},
46-
"devDependencies": {
47-
"eslint": "^3.6.0",
48-
"eslint-config-standard": "^6.0.1",
49-
"eslint-plugin-promise": "^3.5.0",
50-
"eslint-plugin-standard": "^2.0.0",
51-
"grunt": "^1.0.1",
52-
"grunt-contrib-nodeunit": "^1.0.0",
53-
"grunt-eslint": "^19.0.0",
54-
"jsdoc": "^3.4.3",
55-
"minami": "^1.1.1"
56-
}
2+
"name": "github-release-notes",
3+
"version": "0.6.3",
4+
"description": "Node module to publish release notes based on commits between the last two tags.",
5+
"main": "./github-release-notes.js",
6+
"scripts": {
7+
"start": "node github-release-notes.js",
8+
"test": "grunt test",
9+
"docs": "node_modules/.bin/jsdoc -c .jsdoc.conf.json --verbose"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/alexcanessa/github-release-notes.git"
14+
},
15+
"preferGlobal": "true",
16+
"bin": {
17+
"gren": "bin/gren.js"
18+
},
19+
"keywords": [
20+
"Github",
21+
"Release",
22+
"notes",
23+
"Tag",
24+
"Changelog",
25+
"Changelog Generator",
26+
"Issues",
27+
"Commits"
28+
],
29+
"author": "alexcanessa",
30+
"contributors": [
31+
"Alex Canessa <[email protected]>"
32+
],
33+
"license": "ISC",
34+
"bugs": {
35+
"url": "https://github.com/alexcanessa/github-release-notes/issues"
36+
},
37+
"homepage": "https://github.com/alexcanessa/github-release-notes#readme",
38+
"dependencies": {
39+
"chalk": "^1.1.3",
40+
"connectivity": "^1.0.0",
41+
"es6-promise": "^3.2.1",
42+
"github-api": "^3.0.0",
43+
"require-yaml": "0.0.1"
44+
},
45+
"devDependencies": {
46+
"eslint": "^3.6.0",
47+
"eslint-config-standard": "^6.0.1",
48+
"eslint-plugin-promise": "^3.5.0",
49+
"eslint-plugin-standard": "^2.0.0",
50+
"grunt": "^1.0.1",
51+
"grunt-contrib-nodeunit": "^1.0.0",
52+
"grunt-eslint": "^19.0.0",
53+
"jsdoc": "^3.4.3",
54+
"minami": "^1.1.1",
55+
"object-assign-deep": "0.0.4"
56+
}
5757
}

src/gren.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var chalk = require('chalk');
99
var Promise = Promise || require('es6-promise').Promise;
1010
var connectivity = require('connectivity');
1111
var templateConfig = require('./templates.json');
12-
var ObjectAssign = require('deep-assign');
12+
var ObjectAssign = require('object-assign-deep');
1313
var configFile = utils.getConfigFromFile(process.cwd());
1414

1515
var defaults = {
@@ -330,7 +330,7 @@ function templateLabels(gren, issue) {
330330
.map(function(label) {
331331
return template.generate({
332332
label: label.name
333-
}, gren.options.template.issueInfo.label);
333+
}, gren.options.template.label);
334334
}).join('');
335335
}
336336

@@ -349,7 +349,7 @@ function templateBlock(gren, block) {
349349
var releaseTemplate = template.generate({
350350
release: block.name,
351351
date: utils.formatDate(date)
352-
}, template.generate(gren.options.template.releaseInfo, gren.options.template.release));
352+
}, gren.options.template.release);
353353

354354
return releaseTemplate + '\n\n' + block.body;
355355
}
@@ -365,17 +365,12 @@ function templateBlock(gren, block) {
365365
* @return {string}
366366
*/
367367
function templateIssue(gren, issue) {
368-
var issueTemplate = template.generate(gren.options.template.issueInfo, gren.options.template.issue);
369-
var nameTemplate = template.generate({
370-
name: issue.title
371-
}, gren.options.template.issueInfo.name);
372-
373368
return template.generate({
374369
labels: templateLabels(gren, issue),
375-
name: nameTemplate,
370+
name: issue.title,
376371
text: '#' + issue.number,
377372
url: issue.html_url
378-
}, issueTemplate);
373+
}, gren.options.template.issue);
379374
}
380375

381376
/**

src/template.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
'use strict';
22

3+
/**
4+
* Generate the templated string based on
5+
* a placeholders Object
6+
*
7+
* @since 0.6.0
8+
* @private
9+
*
10+
* @param {Object} placeholders All the keys/values to update
11+
* @param {string|Function} string The string or the function that needs to be replaced
12+
*
13+
* @return {string}
14+
*/
315
function generate(placeholders, string) {
16+
if (typeof string === 'function') {
17+
return string(placeholders);
18+
}
19+
420
return Object.keys(placeholders)
521
.reduce(function(carry, placeholder) {
622
var placeholderRegExp = new RegExp('{{' + placeholder + '}}', 'g');

src/templates.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
{
22
"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-
}
3+
"issue": "- {{labels}} {{name}} [{{text}}]({{url}})",
4+
"label": "[**{{label}}**]",
5+
"release": "## {{release}} {{date}}"
156
}

src/utils.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ function requireConfig(filepath) {
184184
}
185185

186186
if (filepath.match(/\./g).length === 1) {
187-
return JSON.parse(fs.readFileSync(filepath, "utf8"));
187+
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
188188
}
189189

190190
return require(filepath);
@@ -201,15 +201,15 @@ function requireConfig(filepath) {
201201
*/
202202
function getConfigFromFile(path) {
203203
return [
204-
'.grenrc.yml',
205-
'.grenrc.json',
206-
'.grenrc.yaml',
207-
'.grenrc.js',
208-
'.grenrc'
209-
]
210-
.reduce(function(carry, filename) {
211-
return carry || requireConfig(path + '/' + filename);
212-
}, false) || {};
204+
'.grenrc.yml',
205+
'.grenrc.json',
206+
'.grenrc.yaml',
207+
'.grenrc.js',
208+
'.grenrc'
209+
]
210+
.reduce(function(carry, filename) {
211+
return carry || requireConfig(path + '/' + filename);
212+
}, false) || {};
213213
}
214214

215215
module.exports = {

0 commit comments

Comments
 (0)