From 55bbde8476013de7a2f24bf29c7c12cb07f96e3f Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Fri, 11 Sep 2020 19:46:12 +0300 Subject: [PATCH] feat: support custom updater as object as well as path (#630) --- README.md | 17 ++++++++++++++++- lib/updaters/index.js | 11 ++++++++++- test/core.spec.js | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e1002e75d..2b550b248 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/lib/updaters/index.js b/lib/updaters/index.js index 0c4293d93..faaab5e94 100644 --- a/lib/updaters/index.js +++ b/lib/updaters/index.js @@ -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) { diff --git a/test/core.spec.js b/test/core.spec.js index 99cb27eb2..22d2b8e2c 100644 --- a/test/core.spec.js +++ b/test/core.spec.js @@ -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 () {