Skip to content

Template functions #54

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

Merged
merged 7 commits into from
Mar 15, 2017
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
2 changes: 1 addition & 1 deletion .grenrc → .grenrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
module.exports = {
"ignoreIssuesWith": [
"duplicate",
"wontfix",
Expand Down
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
docs/
node_modules/
Gruntfile.js
.grenrc
.grenrc.*
.jsdoc.conf.json
.eslintrc
.editorconfig
.travis.yml
30 changes: 18 additions & 12 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,25 @@ You can configure the output of **gren** using templates. Set your own configura
{
"template": {
"commit": "- {{message}}",
"issue": "- {{labels}} {{name}} {{link}}",
"issueInfo": {
"labels": "{{labels}}",
"label": "[**{{label}}**]",
"name": "{{name}}",
"link": "[{{text}}]({{url}})"
},
"release": "## {{release}} {{date}}",
"releaseInfo": {
"release": "{{release}}",
"date": "({{date}})"
}
"issue": "- {{labels}} {{name}} [{{text}}]({{url}})",
"label": "[**{{label}}**]",
"release": "## {{release}} {{date}}"
}
}
```
{% endraw %}

If you're using a `.grenrc.js` config file, you can use JavaScript to manipulate the templates using functions as values.
The function will have an object as first parameter, containing all the values to display. _i.e._

```javascript
/* .grenrc.js */

module.exports = {
template: {
issue: function (placeholders) {
return '- ' + placeholders.labels + ' | ' + placeholders.name.toLowerCase();
}
}
}
```
110 changes: 55 additions & 55 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
{
"name": "github-release-notes",
"version": "0.6.3",
"description": "Node module to publish release notes based on commits between the last two tags.",
"main": "./github-release-notes.js",
"scripts": {
"start": "node github-release-notes.js",
"test": "grunt test",
"docs": "node_modules/.bin/jsdoc -c .jsdoc.conf.json --verbose"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alexcanessa/github-release-notes.git"
},
"preferGlobal": "true",
"bin": {
"gren": "bin/gren.js"
},
"keywords": [
"Github",
"Release",
"notes",
"Tag",
"Changelog",
"Changelog Generator",
"Issues",
"Commits"
],
"author": "alexcanessa",
"contributors": [
"Alex Canessa <canessa.alex@gmail.com>"
],
"license": "ISC",
"bugs": {
"url": "https://github.com/alexcanessa/github-release-notes/issues"
},
"homepage": "https://github.com/alexcanessa/github-release-notes#readme",
"dependencies": {
"chalk": "^1.1.3",
"connectivity": "^1.0.0",
"deep-assign": "^2.0.0",
"es6-promise": "^3.2.1",
"github-api": "^3.0.0",
"require-yaml": "0.0.1"
},
"devDependencies": {
"eslint": "^3.6.0",
"eslint-config-standard": "^6.0.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^2.0.0",
"grunt": "^1.0.1",
"grunt-contrib-nodeunit": "^1.0.0",
"grunt-eslint": "^19.0.0",
"jsdoc": "^3.4.3",
"minami": "^1.1.1"
}
"name": "github-release-notes",
"version": "0.6.3",
"description": "Node module to publish release notes based on commits between the last two tags.",
"main": "./github-release-notes.js",
"scripts": {
"start": "node github-release-notes.js",
"test": "grunt test",
"docs": "node_modules/.bin/jsdoc -c .jsdoc.conf.json --verbose"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alexcanessa/github-release-notes.git"
},
"preferGlobal": "true",
"bin": {
"gren": "bin/gren.js"
},
"keywords": [
"Github",
"Release",
"notes",
"Tag",
"Changelog",
"Changelog Generator",
"Issues",
"Commits"
],
"author": "alexcanessa",
"contributors": [
"Alex Canessa <canessa.alex@gmail.com>"
],
"license": "ISC",
"bugs": {
"url": "https://github.com/alexcanessa/github-release-notes/issues"
},
"homepage": "https://github.com/alexcanessa/github-release-notes#readme",
"dependencies": {
"chalk": "^1.1.3",
"connectivity": "^1.0.0",
"es6-promise": "^3.2.1",
"github-api": "^3.0.0",
"require-yaml": "0.0.1"
},
"devDependencies": {
"eslint": "^3.6.0",
"eslint-config-standard": "^6.0.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^2.0.0",
"grunt": "^1.0.1",
"grunt-contrib-nodeunit": "^1.0.0",
"grunt-eslint": "^19.0.0",
"jsdoc": "^3.4.3",
"minami": "^1.1.1",
"object-assign-deep": "0.0.4"
}
}
15 changes: 5 additions & 10 deletions src/gren.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var chalk = require('chalk');
var Promise = Promise || require('es6-promise').Promise;
var connectivity = require('connectivity');
var templateConfig = require('./templates.json');
var ObjectAssign = require('deep-assign');
var ObjectAssign = require('object-assign-deep');
var configFile = utils.getConfigFromFile(process.cwd());

var defaults = {
Expand Down Expand Up @@ -330,7 +330,7 @@ function templateLabels(gren, issue) {
.map(function(label) {
return template.generate({
label: label.name
}, gren.options.template.issueInfo.label);
}, gren.options.template.label);
}).join('');
}

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

return releaseTemplate + '\n\n' + block.body;
}
Expand All @@ -365,17 +365,12 @@ function templateBlock(gren, block) {
* @return {string}
*/
function templateIssue(gren, issue) {
var issueTemplate = template.generate(gren.options.template.issueInfo, gren.options.template.issue);
var nameTemplate = template.generate({
name: issue.title
}, gren.options.template.issueInfo.name);

return template.generate({
labels: templateLabels(gren, issue),
name: nameTemplate,
name: issue.title,
text: '#' + issue.number,
url: issue.html_url
}, issueTemplate);
}, gren.options.template.issue);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
'use strict';

/**
* Generate the templated string based on
* a placeholders Object
*
* @since 0.6.0
* @private
*
* @param {Object} placeholders All the keys/values to update
* @param {string|Function} string The string or the function that needs to be replaced
*
* @return {string}
*/
function generate(placeholders, string) {
if (typeof string === 'function') {
return string(placeholders);
}

return Object.keys(placeholders)
.reduce(function(carry, placeholder) {
var placeholderRegExp = new RegExp('{{' + placeholder + '}}', 'g');
Expand Down
15 changes: 3 additions & 12 deletions src/templates.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"commit": "- {{message}}",
"issue": "- {{labels}} {{name}} {{link}}",
"issueInfo": {
"labels": "{{labels}}",
"label": "[**{{label}}**]",
"name": "{{name}}",
"link": "[{{text}}]({{url}})"
},
"release": "## {{release}} {{date}}",
"releaseInfo": {
"release": "{{release}}",
"date": "({{date}})"
}
"issue": "- {{labels}} {{name}} [{{text}}]({{url}})",
"label": "[**{{label}}**]",
"release": "## {{release}} {{date}}"
}
20 changes: 10 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function requireConfig(filepath) {
}

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

return require(filepath);
Expand All @@ -201,15 +201,15 @@ function requireConfig(filepath) {
*/
function getConfigFromFile(path) {
return [
'.grenrc.yml',
'.grenrc.json',
'.grenrc.yaml',
'.grenrc.js',
'.grenrc'
]
.reduce(function(carry, filename) {
return carry || requireConfig(path + '/' + filename);
}, false) || {};
'.grenrc.yml',
'.grenrc.json',
'.grenrc.yaml',
'.grenrc.js',
'.grenrc'
]
.reduce(function(carry, filename) {
return carry || requireConfig(path + '/' + filename);
}, false) || {};
}

module.exports = {
Expand Down