Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom updater as object as well as path #630

Merged
merged 2 commits into from Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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