Skip to content

Commit

Permalink
fix: Ensures provided packageFiles arguments are merged with `bumpF…
Browse files Browse the repository at this point in the history
…iles` 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
  • Loading branch information
jbottigliero committed Oct 19, 2021
1 parent a0bc835 commit 1de282a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ module.exports = async 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
for (const packageFile of args.packageFiles) {
const updater = resolveUpdaterObjectFromArgument(packageFile)
if (!updater) return
const pkgPath = path.resolve(process.cwd(), updater.filename)
try {
const contents = fs.readFileSync(pkgPath, 'utf8')
Expand Down
23 changes: 18 additions & 5 deletions lib/updaters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getUpdaterByFilename (filename) {
)
}

function getCustomUpdater (updater) {
function getCustomUpdaterFromPath (updater) {
if (typeof updater === 'string') {
return require(path.resolve(process.cwd(), updater))
}
Expand All @@ -39,32 +39,45 @@ function getCustomUpdater (updater) {
throw new Error('Updater must be a string path or an object with readVersion and writeVersion methods')
}

/**
* 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
}

Expand Down
36 changes: 31 additions & 5 deletions test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ require('chai').should()
// set by mock()
let standardVersion

function exec (opt = '', git) {
function exec(opt = '', git) {
if (typeof opt === 'string') {
opt = cli.parse(`standard-version ${opt}`)
}
if (!git) opt.skip = Object.assign({}, opt.skip, { commit: true, tag: true })
return standardVersion(opt)
}

function getPackageVersion () {
function getPackageVersion() {
return JSON.parse(fs.readFileSync('package.json', 'utf-8')).version
}

Expand All @@ -42,7 +42,7 @@ function getPackageVersion () {
* pkg?: { [string]: any }
* tags?: string[] | Error
*/
function mock ({ bump, changelog, execFile, fs, pkg, tags } = {}) {
function mock({ bump, changelog, execFile, fs, pkg, tags } = {}) {
mockery.enable({ warnOnUnregistered: false, useCleanCache: true })

mockery.registerMock('conventional-recommended-bump', function (opt, cb) {
Expand All @@ -56,7 +56,7 @@ function mock ({ bump, changelog, execFile, fs, pkg, tags } = {}) {
'conventional-changelog',
(opt) =>
new Readable({
read (_size) {
read(_size) {
const next = changelog.shift()
if (next instanceof Error) {
this.destroy(next)
Expand Down Expand Up @@ -94,7 +94,7 @@ function mock ({ bump, changelog, execFile, fs, pkg, tags } = {}) {
return () => stdMocks.flush()
}

function unmock () {
function unmock() {
mockery.deregisterAll()
mockery.disable()
mockFS.restore()
Expand Down Expand Up @@ -568,6 +568,32 @@ describe('standard-version', function () {
})
})

it('`packageFiles` are bumped along with `bumpFiles` defaults [standard-version#533]', async function () {
mock({
bump: 'minor',
fs: {
'.gitignore': '',
'package-lock.json': JSON.stringify({ version: '1.0.0' }),
'manifest.json': fs.readFileSync('./test/mocks/manifest-6.3.1.json')
},
tags: ['v1.0.0']
})

await exec({
silent: true,
packageFiles: [
{
filename: 'manifest.json',
type: 'json'
}
]
})

JSON.parse(fs.readFileSync('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')
})

it('bumps version # in npm-shrinkwrap.json', async function () {
mock({
bump: 'minor',
Expand Down
3 changes: 3 additions & 0 deletions test/mocks/manifest-6.3.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "6.3.1"
}

0 comments on commit 1de282a

Please sign in to comment.