Skip to content

Commit 2785023

Browse files
authored
fix: Ensures provided packageFiles arguments are merged with bumpFiles when no bumpFiles argument is specified (default). (conventional-changelog#534)
- 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 conventional-changelog#533 closes conventional-changelog#533
1 parent 938828d commit 2785023

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ module.exports = async function standardVersion (argv) {
3737
throw Error(`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`)
3838
}
3939

40+
/**
41+
* If an argument for `packageFiles` provided, we include it as a "default" `bumpFile`.
42+
*/
43+
if (argv.packageFiles) {
44+
defaults.bumpFiles = defaults.bumpFiles.concat(argv.packageFiles)
45+
}
46+
4047
const args = Object.assign({}, defaults, argv)
4148
let pkg
4249
for (const packageFile of args.packageFiles) {
4350
const updater = resolveUpdaterObjectFromArgument(packageFile)
51+
if (!updater) return
4452
const pkgPath = path.resolve(process.cwd(), updater.filename)
4553
try {
4654
const contents = fs.readFileSync(pkgPath, 'utf8')

lib/updaters/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function getUpdaterByFilename (filename) {
2626
)
2727
}
2828

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

42+
/**
43+
* Simple check to determine if the object provided is a compatible updater.
44+
*/
45+
function isValidUpdater (obj) {
46+
return (
47+
typeof obj.readVersion === 'function' &&
48+
typeof obj.writeVersion === 'function'
49+
)
50+
}
51+
4252
module.exports.resolveUpdaterObjectFromArgument = function (arg) {
4353
/**
4454
* If an Object was not provided, we assume it's the path/filename
4555
* of the updater.
4656
*/
4757
let updater = arg
58+
if (isValidUpdater(updater)) {
59+
return updater
60+
}
4861
if (typeof updater !== 'object') {
4962
updater = {
5063
filename: arg
5164
}
5265
}
5366
try {
54-
if (updater.updater) {
55-
updater.updater = getCustomUpdater(updater.updater)
67+
if (typeof updater.updater === 'string') {
68+
updater.updater = getCustomUpdaterFromPath(updater.updater)
5669
} else if (updater.type) {
5770
updater.updater = getUpdaterByType(updater.type)
5871
} else {
5972
updater.updater = getUpdaterByFilename(updater.filename)
6073
}
6174
} catch (err) {
62-
if (err.code !== 'ENOENT') console.warn(err.message)
75+
if (err.code !== 'ENOENT') console.warn(`Unable to obtain updater for: ${JSON.stringify(arg)}\n - Error: ${err.message}\n - Skipping...`)
6376
}
6477
/**
6578
* We weren't able to resolve an updater for the argument.
6679
*/
67-
if (!updater.updater) {
80+
if (!isValidUpdater(updater.updater)) {
6881
return false
6982
}
7083

test/core.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,32 @@ describe('standard-version', function () {
568568
})
569569
})
570570

571+
it('`packageFiles` are bumped along with `bumpFiles` defaults [standard-version#533]', async function () {
572+
mock({
573+
bump: 'minor',
574+
fs: {
575+
'.gitignore': '',
576+
'package-lock.json': JSON.stringify({ version: '1.0.0' }),
577+
'manifest.json': fs.readFileSync('./test/mocks/manifest-6.3.1.json')
578+
},
579+
tags: ['v1.0.0']
580+
})
581+
582+
await exec({
583+
silent: true,
584+
packageFiles: [
585+
{
586+
filename: 'manifest.json',
587+
type: 'json'
588+
}
589+
]
590+
})
591+
592+
JSON.parse(fs.readFileSync('manifest.json', 'utf-8')).version.should.equal('6.4.0')
593+
JSON.parse(fs.readFileSync('package.json', 'utf-8')).version.should.equal('6.4.0')
594+
JSON.parse(fs.readFileSync('package-lock.json', 'utf-8')).version.should.equal('6.4.0')
595+
})
596+
571597
it('bumps version # in npm-shrinkwrap.json', async function () {
572598
mock({
573599
bump: 'minor',

test/mocks/manifest-6.3.1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "6.3.1"
3+
}

0 commit comments

Comments
 (0)