Skip to content

Commit b788c5f

Browse files
authored
feat: add support for bumping version # in bower.json (#148)
1 parent 70b20c8 commit b788c5f

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ _how it works:_
2323

2424
`standard-version` does the following:
2525

26-
1. bumps the version in _package.json_ (based on your commit history)
26+
1. bumps the version in _package.json/bower.json_ (based on your commit history)
2727
2. uses [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) to update _CHANGELOG.md_
28-
3. commits _package.json_ and _CHANGELOG.md_
28+
3. commits _package.json (et al.)_ and _CHANGELOG.md_
2929
4. tags a new release
3030

3131
## Installation
@@ -77,7 +77,7 @@ npm run release -- --first-release
7777
standard-version --first-release
7878
```
7979

80-
This will tag a release **without bumping the version in package.json**.
80+
This will tag a release **without bumping the version in package.json (_et al._)**.
8181

8282
When ready, push the git tag and `npm publish` your first release. \o/
8383

index.js

+44-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module.exports = function standardVersion (argv, done) {
1515
var pkgPath = path.resolve(process.cwd(), './package.json')
1616
var pkg = require(pkgPath)
1717
var defaults = require('./defaults')
18-
1918
var args = objectAssign({}, defaults, argv)
2019

2120
bumpVersion(args.releaseAs, function (err, release) {
@@ -29,11 +28,7 @@ module.exports = function standardVersion (argv, done) {
2928
if (!args.firstRelease) {
3029
var releaseType = getReleaseType(args.prerelease, release.releaseType, pkg.version)
3130
newVersion = semver.inc(pkg.version, releaseType, args.prerelease)
32-
33-
checkpoint(args, 'bumping version in package.json from %s to %s', [pkg.version, newVersion])
34-
35-
pkg.version = newVersion
36-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf-8')
31+
updateConfigs(args, newVersion)
3732
} else {
3833
checkpoint(args, 'skip version bump on first release', [], chalk.red(figures.cross))
3934
}
@@ -52,6 +47,37 @@ module.exports = function standardVersion (argv, done) {
5247
})
5348
}
5449

50+
/**
51+
* attempt to update the version # in a collection of common config
52+
* files, e.g., package.json, bower.json.
53+
*
54+
* @param argv config object
55+
* @param newVersion version # to update to.
56+
* @return {string}
57+
*/
58+
var configsToUpdate = {}
59+
function updateConfigs (args, newVersion) {
60+
configsToUpdate[path.resolve(process.cwd(), './package.json')] = false
61+
configsToUpdate[path.resolve(process.cwd(), './bower.json')] = false
62+
Object.keys(configsToUpdate).forEach(function (configPath) {
63+
try {
64+
var stat = fs.lstatSync(configPath)
65+
if (stat.isFile()) {
66+
var config = require(configPath)
67+
var filename = path.basename(configPath)
68+
config.version = newVersion
69+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n', 'utf-8')
70+
checkpoint(args, 'bumping version in ' + filename + ' from %s to %s', [config.version, newVersion])
71+
// flag any config files that we modify the version # for
72+
// as having been updated.
73+
configsToUpdate[configPath] = true
74+
}
75+
} catch (err) {
76+
if (err.code !== 'ENOENT') console.warn(err.message)
77+
}
78+
})
79+
}
80+
5581
function getReleaseType (prerelease, expectedReleaseType, currentVersion) {
5682
if (isString(prerelease)) {
5783
if (isInPrerelease(currentVersion)) {
@@ -160,7 +186,6 @@ function outputChangelog (argv, cb) {
160186

161187
function handledExec (argv, cmd, errorCb, successCb) {
162188
// Exec given cmd and handle possible errors
163-
164189
exec(cmd, function (err, stdout, stderr) {
165190
// If exec returns content in stderr, but no error, print it as a warning
166191
// If exec returns an error, print it and exit with return code 1
@@ -178,14 +203,19 @@ function commit (argv, newVersion, cb) {
178203
var msg = 'committing %s'
179204
var args = [argv.infile]
180205
var verify = argv.verify === false || argv.n ? '--no-verify ' : ''
181-
if (!argv.firstRelease) {
182-
msg += ' and %s'
183-
args.unshift('package.json')
184-
}
206+
var toAdd = ''
207+
// commit any of the config files that we've updated
208+
// the version # for.
209+
Object.keys(configsToUpdate).forEach(function (p) {
210+
if (configsToUpdate[p]) {
211+
msg += ' and %s'
212+
args.unshift(path.basename(p))
213+
toAdd += ' ' + path.relative(process.cwd(), p)
214+
}
215+
})
185216
checkpoint(argv, msg, args)
186-
187-
handledExec(argv, 'git add package.json ' + argv.infile, cb, function () {
188-
handledExec(argv, 'git commit ' + verify + (argv.sign ? '-S ' : '') + (argv.commitAll ? '' : ('package.json ' + argv.infile)) + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', cb, function () {
217+
handledExec(argv, 'git add' + toAdd + ' ' + argv.infile, cb, function () {
218+
handledExec(argv, 'git commit ' + verify + (argv.sign ? '-S ' : '') + (argv.commitAll ? '' : (argv.infile + toAdd)) + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', cb, function () {
189219
cb()
190220
})
191221
})

test.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ function writePackageJson (version, option) {
4949
delete require.cache[require.resolve(path.join(process.cwd(), 'package.json'))]
5050
}
5151

52+
function writeBowerJson (version, option) {
53+
option = option || {}
54+
var bower = objectAssign(option, { version: version })
55+
fs.writeFileSync('bower.json', JSON.stringify(bower), 'utf-8')
56+
}
57+
5258
function writeGitPreCommitHook () {
5359
fs.writeFileSync('.git/hooks/pre-commit', '#!/bin/sh\necho "precommit ran"\nexit 1', 'utf-8')
5460
fs.chmodSync('.git/hooks/pre-commit', '755')
@@ -152,7 +158,7 @@ describe('cli', function () {
152158
var captured = shell.cat('gitcapture.log').stdout.split('\n').map(function (line) {
153159
return line ? JSON.parse(line) : line
154160
})
155-
captured[captured.length - 3].should.deep.equal(['commit', '-S', 'package.json', 'CHANGELOG.md', '-m', 'chore(release): 1.0.1'])
161+
captured[captured.length - 3].should.deep.equal(['commit', '-S', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1'])
156162
captured[captured.length - 2].should.deep.equal(['tag', '-s', 'v1.0.1', '-m', 'chore(release): 1.0.1'])
157163

158164
unmock()
@@ -480,4 +486,22 @@ describe('standard-version', function () {
480486
done()
481487
})
482488
})
489+
490+
describe('bower.json support', function () {
491+
beforeEach(function () {
492+
writeBowerJson('1.0.0')
493+
})
494+
495+
it('bumps verson # in bower.json', function (done) {
496+
commit('feat: first commit')
497+
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
498+
commit('feat: new feature!')
499+
require('./index')({silent: true}, function (err) {
500+
if (err) return done(err)
501+
JSON.parse(fs.readFileSync('package.json', 'utf-8')).version.should.equal('1.1.0')
502+
getPackageVersion().should.equal('1.1.0')
503+
done()
504+
})
505+
})
506+
})
483507
})

0 commit comments

Comments
 (0)