Skip to content

Commit ddc5c00

Browse files
authored
feat(configuration): .versionrc.js files are now supported (#378)
* 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 * docs: Updates README to include details for the new '.versionrc.js' support * fix: updates error message to be a bit more descriptive and helpful.
1 parent 36f85c6 commit ddc5c00

File tree

4 files changed

+94
-9
lines changed

4 files changed

+94
-9
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ You can configure `standard-version` either by:
7171

7272
1. Placing a `standard-version` stanza in your `package.json` (assuming
7373
your project is JavaScript).
74-
1. Creating a `.versionrc` or `.versionrc.json`.
74+
2. Creating a `.versionrc`, `.versionrc.json` or `.versionrc.js`.
75+
- If you are using a `.versionrc.js` your default export must be a configuration object, or a function returning a configuration object.
7576

76-
Any of the command line paramters accepted by `standard-version` can instead
77+
Any of the command line parameters accepted by `standard-version` can instead
7778
be provided via configuration. Please refer to the [conventional-changelog-config-spec](https://github.com/conventional-changelog/conventional-changelog-config-spec/) for details on available configuration options.
7879

80+
7981
### Customizing CHANGELOG Generation
8082

8183
By default, `standard-version` uses the [conventionalcommits preset](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-conventionalcommits).

command.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
const findUp = require('find-up')
2-
const defaults = require('./defaults')
3-
const { readFileSync } = require('fs')
4-
5-
const configPath = findUp.sync(['.versionrc', '.versionrc.json'])
6-
const config = configPath ? JSON.parse(readFileSync(configPath)) : {}
71
const spec = require('conventional-changelog-config-spec')
2+
const { getConfiguration } = require('./lib/configuration')
3+
const defaults = require('./defaults')
84
const { START_OF_LAST_RELEASE_PATTERN } = require('./lib/lifecycles/changelog')
95

106
const yargs = require('yargs')
@@ -110,7 +106,7 @@ const yargs = require('yargs')
110106
.example('$0', 'Update changelog and tag release')
111107
.example('$0 -m "%s: see changelog for details"', 'Update changelog and tag release with custom commit message')
112108
.pkgConf('standard-version')
113-
.config(config)
109+
.config(getConfiguration())
114110
.wrap(97)
115111
.check((args) => {
116112
if (args.changelogHeader && args.changelogHeader.search(START_OF_LAST_RELEASE_PATTERN) !== -1) {

lib/configuration.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const path = require('path')
2+
const findUp = require('find-up')
3+
const { readFileSync } = require('fs')
4+
5+
const CONFIGURATION_FILES = [
6+
'.versionrc',
7+
'.versionrc.json',
8+
'.versionrc.js'
9+
]
10+
11+
module.exports.getConfiguration = function () {
12+
let config = {}
13+
const configPath = findUp.sync(CONFIGURATION_FILES)
14+
if (!configPath) {
15+
return config
16+
}
17+
if (path.extname(configPath) === '.js') {
18+
const jsConfiguration = require(configPath)
19+
if (typeof jsConfiguration === 'function') {
20+
config = jsConfiguration()
21+
} else {
22+
config = jsConfiguration
23+
}
24+
} else {
25+
config = JSON.parse(readFileSync(configPath))
26+
}
27+
28+
/**
29+
* @todo we could eventually have deeper validation of the configuration (using `ajv`) and
30+
* provide a more helpful error.
31+
*/
32+
if (typeof config !== 'object') {
33+
throw Error(
34+
`[standard-version] Invalid configuration in ${configPath} provided. Expected an object but found ${typeof config}.`
35+
)
36+
}
37+
38+
return config
39+
}

test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,54 @@ describe('standard-version', function () {
10991099
content.should.include('http://www.foo.com/1')
11001100
})
11011101

1102+
it('evaluates a config-function from .versionrc.js', function () {
1103+
// write configuration that overrides default issue
1104+
// URL format.
1105+
fs.writeFileSync(
1106+
'.versionrc.js',
1107+
`module.exports = function() {
1108+
return {
1109+
issueUrlFormat: 'http://www.versionrc.js/function/{{id}}'
1110+
}
1111+
}`,
1112+
'utf-8'
1113+
)
1114+
commit('feat: another commit addresses issue #1')
1115+
execCli()
1116+
// CHANGELOG should have the new issue URL format.
1117+
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
1118+
content.should.include('http://www.versionrc.js/function/1')
1119+
})
1120+
1121+
it('evaluates a config-object from .versionrc.js', function () {
1122+
// write configuration that overrides default issue
1123+
// URL format.
1124+
fs.writeFileSync(
1125+
'.versionrc.js',
1126+
`module.exports = {
1127+
issueUrlFormat: 'http://www.versionrc.js/object/{{id}}'
1128+
}`,
1129+
'utf-8'
1130+
)
1131+
commit('feat: another commit addresses issue #1')
1132+
execCli()
1133+
// CHANGELOG should have the new issue URL format.
1134+
const content = fs.readFileSync('CHANGELOG.md', 'utf-8')
1135+
content.should.include('http://www.versionrc.js/object/1')
1136+
})
1137+
1138+
it('throws an error when a non-object is returned from .versionrc.js', function () {
1139+
// write configuration that overrides default issue
1140+
// URL format.
1141+
fs.writeFileSync(
1142+
'.versionrc.js',
1143+
`module.exports = 3`,
1144+
'utf-8'
1145+
)
1146+
commit('feat: another commit addresses issue #1')
1147+
execCli().code.should.equal(1)
1148+
})
1149+
11021150
it('.versionrc : releaseCommitMessageFormat', function () {
11031151
// write configuration that overrides default issue
11041152
// URL format.

0 commit comments

Comments
 (0)