Skip to content

Commit

Permalink
Merge fa724aa into 57ce557
Browse files Browse the repository at this point in the history
  • Loading branch information
jbottigliero committed Sep 28, 2019
2 parents 57ce557 + fa724aa commit d982a7f
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -37,7 +37,7 @@ module.exports = function standardVersion (argv) {
try {
let contents = fs.readFileSync(pkgPath, 'utf8')
pkg = {
version: updater.updater.readVersion(contents),
version: updater.updater.readVersion(contents, updater.options),
private: typeof updater.updater.isPrivate === 'function' ? updater.updater.isPrivate(contents) : false
}
} catch (err) {}
Expand Down
4 changes: 2 additions & 2 deletions lib/lifecycles/bump.js
Expand Up @@ -161,12 +161,12 @@ function updateConfigs (args, newVersion) {
checkpoint(
args,
'bumping version in ' + updater.filename + ' from %s to %s',
[updater.updater.readVersion(contents), newVersion]
[updater.updater.readVersion(contents, updater.options), newVersion]
)
writeFile(
args,
configPath,
updater.updater.writeVersion(contents, newVersion)
updater.updater.writeVersion(contents, newVersion, updater.options)
)
// flag any config files that we modify the version # for
// as having been updated.
Expand Down
3 changes: 2 additions & 1 deletion lib/updaters/index.js
Expand Up @@ -34,7 +34,8 @@ module.exports.resolveUpdaterObjectFromArgument = function (arg) {
let updater = arg
if (typeof updater !== 'object') {
updater = {
filename: arg
filename: arg,
options: {}
}
}
try {
Expand Down
28 changes: 28 additions & 0 deletions lib/updaters/types/regex.js
@@ -0,0 +1,28 @@
module.exports.readVersion = function (contents, options) {
let match = options.match
if (!match) {
throw Error('You must provide a `match` option when using the `regex` updater.')
}
const found = contents.match(match)
if (!found) {
throw Error('No matches found for provided `match`.')
}
if (!found.groups || !found.groups.version) {
throw Error('The named capture group `version` was not found.')
}
return found.groups.version
}

module.exports.writeVersion = function (contents, version, options) {
let replace = options.replace
if (!replace) {
throw Error('You must provide a `replace` option when using the `regex` updater.')
}
if (replace instanceof RegExp) {
replace = [replace]
}
replace.forEach((r) => {
contents = contents.replace(r, version)
})
return contents
}
67 changes: 67 additions & 0 deletions test.js
Expand Up @@ -963,6 +963,73 @@ describe('standard-version', function () {
fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('1.1.0')
})
})

it('bumps a custom `regex` file', function () {
fs.copyFileSync('../test/mocks/release.bundle', 'release.bundle')
commit('feat: first commit')
return require('./index')({
silent: true,
packageFiles: [
{
filename: 'release.bundle',
type: 'regex',
options: {
match: /version = '(?<version>.*)'/
}
}
],
bumpFiles: [
{
filename: 'release.bundle',
type: 'regex',
options: {
match: /version = '(?<version>.*)'/,
replace: [
/(?<=version = ').*(?=')/
]
}
}
]
})
.then(() => {
fs.readFileSync('release.bundle', 'utf-8').should.contain(`version = '0.0.2'`)
})
})

it('bumps a custom `regex` file with multiple `replace`', function () {
fs.copyFileSync('../test/mocks/VERSION-METADATA.txt', 'VERSION-METADATA.txt')
commit('feat: first commit')
return require('./index')({
silent: true,
packageFiles: [
{
filename: 'VERSION-METADATA.txt',
type: 'regex',
options: {
match: /version = '(?<version>.*)'/
}
}
],
bumpFiles: [
{
filename: 'VERSION-METADATA.txt',
type: 'regex',
options: {
match: /version = '(?<version>.*)'/,
replace: [
/(?<=version = ').*(?=')/,
/(?<=standard-version@).*(?=')/g
]
}
}
]
})
.then(() => {
fs.readFileSync('VERSION-METADATA.txt', 'utf-8')
.should
.eq(`name = 'standard-version@0.0.2'\nversion = '0.0.2'\ntags = ['standard-version@0.0.2']`)
})
})
})

describe('custom `packageFiles` support', function () {
Expand Down
3 changes: 3 additions & 0 deletions test/mocks/VERSION-METADATA.txt
@@ -0,0 +1,3 @@
name = 'standard-version@0.0.1'
version = '0.0.1'
tags = ['standard-version@0.0.1']
4 changes: 4 additions & 0 deletions test/mocks/release.bundle
@@ -0,0 +1,4 @@
name = 'release.bundle'
author = 'conventional-changelog'
version = '0.0.1'
metadata = ['standard-version']

0 comments on commit d982a7f

Please sign in to comment.