Skip to content
Permalink
Browse files
feat: support custom updater as object as well as path (#630)
  • Loading branch information
eemeli committed Sep 11, 2020
1 parent 274f694 commit 55bbde8476013de7a2f24bf29c7c12cb07f96e3f
Showing with 48 additions and 2 deletions.
  1. +16 −1 README.md
  2. +10 −1 lib/updaters/index.js
  3. +22 −0 test/core.spec.js
@@ -355,7 +355,7 @@ As of version `7.1.0` you can configure multiple `bumpFiles` and `packageFiles`.

1. Specify a custom `bumpFile` "`filename`", this is the path to the file you want to "bump"
2. Specify the `bumpFile` "`updater`", this is _how_ the file will be bumped.

a. If your using a common type, you can use one of `standard-version`'s built-in `updaters` by specifying a `type`.

b. If your using an less-common version file, you can create your own `updater`.
@@ -383,6 +383,21 @@ As of version `7.1.0` you can configure multiple `bumpFiles` and `packageFiles`.
}
```

If using `.versionrc.js` as your configuration file, the `updater` may also be set as an object, rather than a path:

```js
// .versionrc.js
const tracker = {
filename: 'VERSION_TRACKER.json',
updater: require('./path/to/custom-version-updater')
}
module.exports = {
bumpFiles: [tracker],
packageFiles: [tracker]
}
```

#### Custom `updater`s

An `updater` is expected to be a Javascript module with _atleast_ two methods exposed: `readVersion` and `writeVersion`.
@@ -27,7 +27,16 @@ function getUpdaterByFilename (filename) {
}

function getCustomUpdater (updater) {
return require(path.resolve(process.cwd(), updater))
if (typeof updater === 'string') {
return require(path.resolve(process.cwd(), updater))
}
if (
typeof updater.readVersion === 'function' &&
typeof updater.writeVersion === 'function'
) {
return updater
}
throw new Error('Updater must be a string path or an object with readVersion and writeVersion methods')
}

module.exports.resolveUpdaterObjectFromArgument = function (arg) {
@@ -544,6 +544,28 @@ describe('standard-version', function () {
})
fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('6.4.0')
})

it('allows same object to be used in packageFiles and bumpFiles', async function () {
mock({
bump: 'minor',
fs: {
'VERSION_TRACKER.txt': fs.readFileSync(
'./test/mocks/VERSION-6.3.1.txt'
)
}
})
const origWarn = console.warn
console.warn = () => {
throw new Error('console.warn should not be called')
}
const filedesc = { filename: 'VERSION_TRACKER.txt', type: 'plain-text' }
try {
await exec({ packageFiles: [filedesc], bumpFiles: [filedesc] })
fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('6.4.0')
} finally {
console.warn = origWarn
}
})
})

it('bumps version # in npm-shrinkwrap.json', async function () {

0 comments on commit 55bbde8

Please sign in to comment.