Skip to content

Commit

Permalink
feat: support custom updater as object as well as path (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Sep 11, 2020
1 parent 274f694 commit 55bbde8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -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`.
Expand Down Expand Up @@ -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`.
Expand Down
11 changes: 10 additions & 1 deletion lib/updaters/index.js
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions test/core.spec.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 55bbde8

Please sign in to comment.