Skip to content

Commit

Permalink
Add support for external templates
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete authored and b6pzeusbc54tvhw5jgpyw8pwz2x6gs committed Sep 19, 2018
1 parent d1883bd commit 3e94746
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ Then just use `--template` to point to your template:
auto-changelog --template changelog-template.hbs
```

You can also point to an external template by passing in a URL:

```bash
auto-changelog --template https://example.com/templates/compact.hbs
```

To see exactly what data is passed in to the templates, you can generate a JSON version of the changelog:

```bash
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"commander": "^2.9.0",
"handlebars": "^4.0.11",
"lodash.uniqby": "^4.7.0",
"node-fetch": "^2.2.0",
"parse-github-url": "^1.0.1",
"semver": "^5.1.0"
},
Expand Down
6 changes: 6 additions & 0 deletions src/template.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { join } from 'path'
import Handlebars from 'handlebars'
import fetch from 'node-fetch'
import { readFile, fileExists } from './utils'

const TEMPLATES_DIR = join(__dirname, '..', 'templates')
const MATCH_URL = /^https?:\/\/.+/

Handlebars.registerHelper('json', function (object) {
return new Handlebars.SafeString(JSON.stringify(object, null, 2))
Expand Down Expand Up @@ -47,6 +49,10 @@ Handlebars.registerHelper('matches', function (val, pattern, options) {
})

async function getTemplate (template) {
if (MATCH_URL.test(template)) {
const response = await fetch(template)
return response.text()
}
if (await fileExists(template)) {
return readFile(template)
}
Expand Down
6 changes: 6 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('compileTemplate', () => {
expect(await compileTemplate(path, { releases })).to.equal(expected)
})

it('compiles using url path', async () => {
const path = 'https://raw.githubusercontent.com/CookPete/auto-changelog/master/templates/compact.hbs'
const expected = await readFile(join(__dirname, 'data', 'template-compact.md'))
expect(await compileTemplate(path, { releases })).to.equal(expected)
})

it('throws an error when no template found', done => {
compileTemplate('not-found', { releases })
.then(() => done('Should throw an error'))
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,10 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"

node-fetch@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"

node-pre-gyp@^0.6.36:
version "0.6.38"
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.38.tgz#e92a20f83416415bb4086f6d1fb78b3da73d113d"
Expand Down

0 comments on commit 3e94746

Please sign in to comment.