From c5eb3a99afbab02ce78a919a5fce75aa1da82a59 Mon Sep 17 00:00:00 2001 From: Joe Bottigliero Date: Sat, 8 Jun 2019 15:30:29 -0500 Subject: [PATCH] feat(configuration): .versionrc.js files are now supported - Updates the configuration retrieval to support Javascript (`.js`) configurations. - Javascript configurations MUST export a configuration object _or_ a function (returning a configuration object) as the default export. closes #371 --- command.js | 10 +++------ lib/configuration.js | 40 ++++++++++++++++++++++++++++++++++++ test.js | 48 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 lib/configuration.js diff --git a/command.js b/command.js index c03ce5604..f3aa8db1e 100755 --- a/command.js +++ b/command.js @@ -1,10 +1,6 @@ -const findUp = require('find-up') -const defaults = require('./defaults') -const { readFileSync } = require('fs') - -const configPath = findUp.sync(['.versionrc', '.versionrc.json']) -const config = configPath ? JSON.parse(readFileSync(configPath)) : {} const spec = require('conventional-changelog-config-spec') +const { getConfiguration } = require('./lib/configuration') +const defaults = require('./defaults') const { START_OF_LAST_RELEASE_PATTERN } = require('./lib/lifecycles/changelog') const yargs = require('yargs') @@ -110,7 +106,7 @@ const yargs = require('yargs') .example('$0', 'Update changelog and tag release') .example('$0 -m "%s: see changelog for details"', 'Update changelog and tag release with custom commit message') .pkgConf('standard-version') - .config(config) + .config(getConfiguration()) .wrap(97) .check((args) => { if (args.changelogHeader && args.changelogHeader.search(START_OF_LAST_RELEASE_PATTERN) !== -1) { diff --git a/lib/configuration.js b/lib/configuration.js new file mode 100644 index 000000000..5f422bef8 --- /dev/null +++ b/lib/configuration.js @@ -0,0 +1,40 @@ +const path = require('path') +const findUp = require('find-up') +const { readFileSync } = require('fs') + +const CONFIGURATION_FILES = [ + '.versionrc', + '.versionrc.json', + '.versionrc.js' +] + +module.exports.getConfiguration = function () { + let config = {} + const configPath = findUp.sync(CONFIGURATION_FILES) + if (!configPath) { + return config + } + if (path.extname(configPath) === '.js') { + const jsConfiguration = require(configPath) + if (typeof jsConfiguration === 'function') { + config = jsConfiguration() + } else { + config = jsConfiguration + } + } else { + config = JSON.parse(readFileSync(configPath)) + } + + /** + * @todo we could eventually have deeper validation of the configuration (using `ajv`) and + * provide a more helpful error. + */ + if (typeof config !== 'object') { + throw Error( + '[standard-version] Invalid default export found in configuration file.\n' + + 'You must provide an object or function.' + ) + } + + return config +} diff --git a/test.js b/test.js index 02b3279ba..54a7007eb 100644 --- a/test.js +++ b/test.js @@ -1071,6 +1071,54 @@ describe('standard-version', function () { content.should.include('http://www.foo.com/1') }) + it('evaluates a config-function from .versionrc.js', function () { + // write configuration that overrides default issue + // URL format. + fs.writeFileSync( + '.versionrc.js', + `module.exports = function() { + return { + issueUrlFormat: 'http://www.versionrc.js/function/{{id}}' + } + }`, + 'utf-8' + ) + commit('feat: another commit addresses issue #1') + execCli() + // CHANGELOG should have the new issue URL format. + const content = fs.readFileSync('CHANGELOG.md', 'utf-8') + content.should.include('http://www.versionrc.js/function/1') + }) + + it('evaluates a config-object from .versionrc.js', function () { + // write configuration that overrides default issue + // URL format. + fs.writeFileSync( + '.versionrc.js', + `module.exports = { + issueUrlFormat: 'http://www.versionrc.js/object/{{id}}' + }`, + 'utf-8' + ) + commit('feat: another commit addresses issue #1') + execCli() + // CHANGELOG should have the new issue URL format. + const content = fs.readFileSync('CHANGELOG.md', 'utf-8') + content.should.include('http://www.versionrc.js/object/1') + }) + + it('throws an error when a non-object is returned from .versionrc.js', function () { + // write configuration that overrides default issue + // URL format. + fs.writeFileSync( + '.versionrc.js', + `module.exports = 3`, + 'utf-8' + ) + commit('feat: another commit addresses issue #1') + execCli().code.should.equal(1) + }) + it('.versionrc : releaseCommitMessageFormat', function () { // write configuration that overrides default issue // URL format.