From 3e9474696ad9fbefc7ab03fcb80a6d9e194ad5a1 Mon Sep 17 00:00:00 2001 From: Pete Cook Date: Mon, 23 Jul 2018 15:53:32 +0100 Subject: [PATCH] Add support for external templates Fixes https://github.com/CookPete/auto-changelog/issues/49 Closes https://github.com/CookPete/auto-changelog/pull/50 --- README.md | 6 ++++++ package.json | 1 + src/template.js | 6 ++++++ test/template.js | 6 ++++++ yarn.lock | 4 ++++ 5 files changed, 23 insertions(+) diff --git a/README.md b/README.md index b743a72a..3c8ad980 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 0a552477..de982591 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/template.js b/src/template.js index 3b4bb2b2..53cb428b 100644 --- a/src/template.js +++ b/src/template.js @@ -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)) @@ -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) } diff --git a/test/template.js b/test/template.js index 7f447459..a5898725 100644 --- a/test/template.js +++ b/test/template.js @@ -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')) diff --git a/yarn.lock b/yarn.lock index d0701867..709dc0dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"