From 1110293e68895c8deb3f8a8b0a3826754bd5aaf2 Mon Sep 17 00:00:00 2001 From: Joe Bottigliero Date: Sat, 25 Jan 2020 13:00:31 -0600 Subject: [PATCH] fix: Ensures provided `packageFiles` arguments are merged with `bumpFiles` when no `bumpFiles` argument is specified (default). - Merges `packageFiles` with `defaults.bumpFiles` before merging in arguments when a `packageFile` argument is present. - Adds some simple "validation" to updaters and improves error messaging. - Adds a test case covering #533 closes #533 --- index.js | 8 ++++++++ lib/updaters/index.js | 23 ++++++++++++++++++----- test.js | 22 ++++++++++++++++++++++ test/mocks/manifest-6.3.1.json | 3 +++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 test/mocks/manifest-6.3.1.json diff --git a/index.js b/index.js index 74de6e031..5caf8c2ab 100755 --- a/index.js +++ b/index.js @@ -37,11 +37,19 @@ module.exports = function standardVersion (argv) { throw Error(`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`) } + /** + * If an argument for `packageFiles` provided, we include it as a "default" `bumpFile`. + */ + if (argv.packageFiles) { + defaults.bumpFiles = defaults.bumpFiles.concat(argv.packageFiles) + } + const args = Object.assign({}, defaults, argv) let pkg args.packageFiles.forEach((packageFile) => { if (pkg) return const updater = resolveUpdaterObjectFromArgument(packageFile) + if (!updater) return const pkgPath = path.resolve(process.cwd(), updater.filename) try { const contents = fs.readFileSync(pkgPath, 'utf8') diff --git a/lib/updaters/index.js b/lib/updaters/index.js index 84ed1a0fa..8430b8cde 100644 --- a/lib/updaters/index.js +++ b/lib/updaters/index.js @@ -22,36 +22,49 @@ function getUpdaterByFilename (filename) { ) } -function getCustomUpdater (updater) { +function getCustomUpdaterFromPath (updater) { return require(path.resolve(process.cwd(), updater)) } +/** + * Simple check to determine if the object provided is a compatible updater. + */ +function isValidUpdater (obj) { + return ( + typeof obj.readVersion === 'function' && + typeof obj.writeVersion === 'function' + ) +} + module.exports.resolveUpdaterObjectFromArgument = function (arg) { /** * If an Object was not provided, we assume it's the path/filename * of the updater. */ let updater = arg + if (isValidUpdater(updater)) { + return updater + } if (typeof updater !== 'object') { updater = { filename: arg } } try { - if (updater.updater) { - updater.updater = getCustomUpdater(updater.updater) + if (typeof updater.updater === 'string') { + updater.updater = getCustomUpdaterFromPath(updater.updater) } else if (updater.type) { updater.updater = getUpdaterByType(updater.type) } else { updater.updater = getUpdaterByFilename(updater.filename) } } catch (err) { - if (err.code !== 'ENOENT') console.warn(err.message) + if (err.code !== 'ENOENT') console.warn(`Unable to obtain updater for: ${JSON.stringify(arg)}\n - Error: ${err.message}\n - Skipping...`) } /** * We weren't able to resolve an updater for the argument. */ - if (!updater.updater) { + if (!isValidUpdater(updater.updater)) { return false } diff --git a/test.js b/test.js index b170513c9..a7f1cb0f1 100644 --- a/test.js +++ b/test.js @@ -988,6 +988,28 @@ describe('standard-version', function () { fs.readFileSync('VERSION_TRACKER.txt', 'utf-8').should.equal('6.4.0') }) }) + + it('`packageFiles` are bumped along with `bumpFiles` defaults [standard-version#533]', function () { + fs.writeFileSync('.gitignore', '', 'utf-8') + fs.mkdirSync('./dist') + fs.copyFileSync('../test/mocks/manifest-6.3.1.json', './dist/manifest.json') + writePackageLockJson('1.0.0') + commit('feat: yet another commit') + return require('./index')({ + silent: true, + packageFiles: [ + { + filename: './dist/manifest.json', + type: 'json' + } + ] + }) + .then(() => { + JSON.parse(fs.readFileSync('./dist/manifest.json', 'utf-8')).version.should.equal('6.4.0') + JSON.parse(fs.readFileSync('package.json', 'utf-8')).version.should.equal('6.4.0') + JSON.parse(fs.readFileSync('package-lock.json', 'utf-8')).version.should.equal('6.4.0') + }) + }) }) describe('npm-shrinkwrap.json support', function () { diff --git a/test/mocks/manifest-6.3.1.json b/test/mocks/manifest-6.3.1.json new file mode 100644 index 000000000..22a463080 --- /dev/null +++ b/test/mocks/manifest-6.3.1.json @@ -0,0 +1,3 @@ +{ + "version": "6.3.1" +} \ No newline at end of file