From ee6b9876bedcf9af16b39c04ba8566284c535938 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@keylocation.sg> Date: Sun, 4 Jun 2017 16:42:24 +0200 Subject: [PATCH] Externalise hbs templates (#255) * Externalise hbs templates * Fix tests * Move templates * lint fix * Refactor templates location --- lib/config/definitions.js | 32 ++++++++++++------- lib/config/templates/branch-name.hbs | 1 + lib/config/templates/commit-message.hbs | 1 + lib/config/templates/group-branch-name.hbs | 1 + lib/config/templates/group-commit-message.hbs | 1 + lib/config/templates/group-pr-body.hbs | 1 + lib/config/templates/group-pr-title.hbs | 1 + lib/config/templates/pr-body.hbs | 3 ++ lib/config/templates/pr-title.hbs | 1 + test/helpers/npm.spec.js | 3 +- test/helpers/yarn.spec.js | 3 +- 11 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 lib/config/templates/branch-name.hbs create mode 100644 lib/config/templates/commit-message.hbs create mode 100644 lib/config/templates/group-branch-name.hbs create mode 100644 lib/config/templates/group-commit-message.hbs create mode 100644 lib/config/templates/group-pr-body.hbs create mode 100644 lib/config/templates/group-pr-title.hbs create mode 100644 lib/config/templates/pr-body.hbs create mode 100644 lib/config/templates/pr-title.hbs diff --git a/lib/config/definitions.js b/lib/config/definitions.js index c64da713db..ec13c0a6c9 100644 --- a/lib/config/definitions.js +++ b/lib/config/definitions.js @@ -1,4 +1,16 @@ -// const logger = require('winston'); +const fs = require('fs'); +const path = require('path'); + +function template(name) { + const shortName = `${name.replace(/([A-Z])/g, '-$1').toLowerCase()}.hbs`; + const hbsContents = fs.readFileSync( + // Long path is so that it works whether code is run from lib or dist + path.resolve(__dirname, '../../lib/config/templates/', shortName), + 'utf8' + ); + // Strip off any trailing line break + return hbsContents.replace(/\n$/, ''); +} module.exports = { getOptions, @@ -124,30 +136,28 @@ const options = [ name: 'branchName', description: 'Branch name template', type: 'string', - default: 'renovate/{{depName}}-{{newVersionMajor}}.x', + default: template('branchName'), cli: false, }, { name: 'commitMessage', description: 'Commit message template', type: 'string', - default: 'Update dependency {{depName}} to version {{newVersion}}', + default: template('commitMessage'), cli: false, }, { name: 'prTitle', description: 'Pull Request title template', type: 'string', - default: - '{{#if isPin}}Pin{{else}}Update{{/if}} dependency {{depName}} to version {{#if isRange}}{{newVersion}}{{else}}{{#if isMajor}}{{newVersionMajor}}.x{{else}}{{newVersion}}{{/if}}{{/if}}', + default: template('prTitle'), cli: false, }, { name: 'prBody', description: 'Pull Request body template', type: 'string', - default: - 'This Pull Request updates dependency {{depName}} from version `{{currentVersion}}` to `{{newVersion}}`\n\n{{changelog}}', + default: template('prBody'), cli: false, }, // Yarn Lock Maintenance @@ -217,28 +227,28 @@ const options = [ name: 'groupBranchName', description: 'Branch name template for the group', type: 'string', - default: 'renovate/{{groupSlug}}', + default: template('groupBranchName'), cli: false, }, { name: 'groupCommitMessage', description: 'Group commit message', type: 'string', - default: 'Renovate {{groupName}} packages', + default: template('groupCommitMessage'), cli: false, }, { name: 'groupPrTitle', description: 'Pull Request title template for the group', type: 'string', - default: 'Renovate {{groupName}} packages', + default: template('groupPrTitle'), cli: false, }, { name: 'groupPrBody', description: 'Pull Request body template for the group', type: 'string', - default: 'This PR renovates the package group "{{groupName}}".', + default: template('groupPrBody'), cli: false, }, // Pull Request options diff --git a/lib/config/templates/branch-name.hbs b/lib/config/templates/branch-name.hbs new file mode 100644 index 0000000000..0ce7efa5e4 --- /dev/null +++ b/lib/config/templates/branch-name.hbs @@ -0,0 +1 @@ +renovate/{{depName}}-{{newVersionMajor}}.x diff --git a/lib/config/templates/commit-message.hbs b/lib/config/templates/commit-message.hbs new file mode 100644 index 0000000000..2128aff862 --- /dev/null +++ b/lib/config/templates/commit-message.hbs @@ -0,0 +1 @@ +Update dependency {{depName}} to version {{newVersion}} diff --git a/lib/config/templates/group-branch-name.hbs b/lib/config/templates/group-branch-name.hbs new file mode 100644 index 0000000000..9b288fa914 --- /dev/null +++ b/lib/config/templates/group-branch-name.hbs @@ -0,0 +1 @@ +renovate/{{groupSlug}} diff --git a/lib/config/templates/group-commit-message.hbs b/lib/config/templates/group-commit-message.hbs new file mode 100644 index 0000000000..468a723413 --- /dev/null +++ b/lib/config/templates/group-commit-message.hbs @@ -0,0 +1 @@ +Renovate {{groupName}} packages diff --git a/lib/config/templates/group-pr-body.hbs b/lib/config/templates/group-pr-body.hbs new file mode 100644 index 0000000000..80f03083c5 --- /dev/null +++ b/lib/config/templates/group-pr-body.hbs @@ -0,0 +1 @@ +This PR renovates the package group "{{groupName}}". diff --git a/lib/config/templates/group-pr-title.hbs b/lib/config/templates/group-pr-title.hbs new file mode 100644 index 0000000000..468a723413 --- /dev/null +++ b/lib/config/templates/group-pr-title.hbs @@ -0,0 +1 @@ +Renovate {{groupName}} packages diff --git a/lib/config/templates/pr-body.hbs b/lib/config/templates/pr-body.hbs new file mode 100644 index 0000000000..7d4a3e2971 --- /dev/null +++ b/lib/config/templates/pr-body.hbs @@ -0,0 +1,3 @@ +This Pull Request updates dependency {{depName}} from version `{{currentVersion}}` to `{{newVersion}}` + +{{changelog}} diff --git a/lib/config/templates/pr-title.hbs b/lib/config/templates/pr-title.hbs new file mode 100644 index 0000000000..7b6f9ef2d7 --- /dev/null +++ b/lib/config/templates/pr-title.hbs @@ -0,0 +1 @@ +{{#if isPin}}Pin{{else}}Update{{/if}} dependency {{depName}} to version {{#if isRange}}{{newVersion}}{{else}}{{#if isMajor}}{{newVersionMajor}}.x{{else}}{{newVersion}}{{/if}}{{/if}} diff --git a/test/helpers/npm.spec.js b/test/helpers/npm.spec.js index 6ee4476ed3..7a71e2dc82 100644 --- a/test/helpers/npm.spec.js +++ b/test/helpers/npm.spec.js @@ -1,5 +1,4 @@ const npmHelper = require('../../lib/helpers/npm'); -const defaultConfig = require('../../lib/config/defaults').getConfig(); jest.mock('fs'); jest.mock('child_process'); @@ -57,7 +56,7 @@ describe('getLockFile(packageJson, config)', () => { describe('maintainLockFile(inputConfig)', () => { let config; beforeEach(() => { - config = Object.assign({}, defaultConfig); + config = {}; config.packageFile = 'package.json'; config.api = { getFileContent: jest.fn(), diff --git a/test/helpers/yarn.spec.js b/test/helpers/yarn.spec.js index 7856efacf0..8ba85f918c 100644 --- a/test/helpers/yarn.spec.js +++ b/test/helpers/yarn.spec.js @@ -1,5 +1,4 @@ const yarnHelper = require('../../lib/helpers/yarn'); -const defaultConfig = require('../../lib/config/defaults').getConfig(); jest.mock('fs'); jest.mock('child_process'); @@ -60,7 +59,7 @@ describe('getLockFile(packageJson, config)', () => { describe('maintainLockFile(inputConfig)', () => { let config; beforeEach(() => { - config = Object.assign({}, defaultConfig); + config = {}; config.packageFile = 'package.json'; config.api = { getFileContent: jest.fn(), -- GitLab