Skip to content

Commit

Permalink
feat: support cordova versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
azachar committed Jul 26, 2016
1 parent 16f46cd commit dda5409
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 14 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

> stop using `npm version`, use `standard-version` it rocks!
Automatic versioning and CHANGELOG management, using GitHub's new squash button and
NPM and Cordova Automatic versioning and CHANGELOG management, using GitHub's new squash button and
the [recommended workflow](https://github.com/conventional-changelog/conventional-changelog-cli#recommended-workflow) for `conventional-changelog`.

_how it works:_
Expand All @@ -22,9 +22,10 @@ _how it works:_
`standard-version` does the following:

1. bumps the version in _package.json_ (based on your commit history)
2. uses [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) to update _CHANGELOG.md_
3. commits _package.json_ and _CHANGELOG.md_
4. tags a new release
2. bumps the _version_ and _android-versionCode_ in _config.xml_ (based on your commit history)
3. uses [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) to update _CHANGELOG.md_
4. commits _package.json_ and _CHANGELOG.md_ and optionally _config.xml_
5. tags a new release

## Installation

Expand All @@ -33,7 +34,7 @@ _how it works:_
Install and add to `devDependencies`:

```
npm i --save-dev standard-version
npm i --save-dev azachar/standard-version
```

Add an [`npm run` script](https://docs.npmjs.com/cli/run-script) to your _package.json_:
Expand All @@ -55,7 +56,7 @@ This has the benefit of making your repo/package more portable, so that other de
Install globally (add to your `PATH`):

```
npm i -g standard-version
npm i -g azachar/standard-version
```

Now you can use `standard-version` in place of `npm version`.
Expand Down
52 changes: 45 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,41 @@ var exec = require('child_process').exec
var fs = require('fs')
var accessSync = require('fs-access').sync
var pkgPath = path.resolve(process.cwd(), './package.json')
var cordovaPath = path.resolve(process.cwd(), './config.xml')
var pkg = require(pkgPath)
var semver = require('semver')
var util = require('util')

function isCordovaProject () {
return !!fs.existsSync(cordovaPath)
}

function androidVersionCode (version) {
var major = semver.major(version)
var minor = semver.minor(version)
var patch = semver.patch(version)
return major * 1000000 + minor * 10000 + patch * 10
}

function bumpCordova (oldVersion, newVersion) {
var newAndroidVersionCode = androidVersionCode(newVersion)
checkpoint('bumping version in config.xml from %s to %s (%s)', [oldVersion, newVersion, newAndroidVersionCode])

var cordovaVersionRegExp = /(<widget.*version=("|'))(v?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)("|')/
var androidVersionCodeRegExp = /(<widget.*android-versionCode=("|'))(\d+)("|')/

fs.readFile(cordovaPath, 'utf8', function (err, data) {
if (err) return console.log(chalk.red(err))

var result = data.replace(cordovaVersionRegExp, '$1' + newVersion + '$4')
result = result.replace(androidVersionCodeRegExp, '$1' + newAndroidVersionCode + '$4')

fs.writeFile(cordovaPath, result, 'utf8', function (err) {
if (err) return console.log(chalk.red(err))
})
})
}

conventionalRecommendedBump({
preset: 'angular'
}, function (err, release) {
Expand All @@ -66,6 +97,11 @@ conventionalRecommendedBump({
var newVersion = pkg.version
if (!argv.firstRelease) {
newVersion = semver.inc(pkg.version, release.releaseAs)

if (isCordovaProject()) {
bumpCordova(pkg.version, newVersion)
}

checkpoint('bumping version in package.json from %s to %s', [pkg.version, newVersion])
pkg.version = newVersion
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf-8')
Expand All @@ -92,10 +128,10 @@ function outputChangelog (argv, cb) {
var changelogStream = conventionalChangelog({
preset: 'angular'
})
.on('error', function (err) {
console.error(chalk.red(err.message))
process.exit(1)
})
.on('error', function (err) {
console.error(chalk.red(err.message))
process.exit(1)
})

changelogStream.on('data', function (buffer) {
content += buffer.toString()
Expand Down Expand Up @@ -126,9 +162,11 @@ function commit (argv, newVersion, cb) {
process.exit(1)
}
}
exec('git add package.json ' + argv.infile, function (err, stdout, stderr) {

var filesToComit = 'package.json ' + (isCordovaProject() ? 'config.xml ' : '') + argv.infile
exec('git add ' + filesToComit, function (err, stdout, stderr) {
handleExecError(err, stderr)
exec('git commit ' + verify + (argv.sign ? '-S ' : '') + 'package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
exec('git commit ' + verify + (argv.sign ? '-S ' : '') + filesToComit + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
handleExecError(err, stderr)
return cb()
})
Expand Down Expand Up @@ -178,4 +216,4 @@ function checkpoint (msg, args, figure) {
console.info((figure || chalk.green(figures.tick)) + ' ' + util.format.apply(util, [msg].concat(args.map(function (arg) {
return chalk.bold(arg)
}))))
};
}
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"standard"
],
"author": "Ben Coe <ben@npmjs.com>",
"contributors": [
{
"name": "Andrej Zachar <andrej@chocolatejar.eu>"
}
],
"license": "ISC",
"bugs": {
"url": "https://github.com/conventional-changelog/standard-version/issues"
Expand All @@ -39,6 +44,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"chai-files": "^1.4.0",
"coveralls": "^2.11.9",
"mocha": "^2.5.1",
"mock-git": "^1.0.2",
Expand Down
68 changes: 67 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ var path = require('path')
var mockGit = require('mock-git')
var cliPath = path.resolve(__dirname, './index.js')

require('chai').should()
var chai = require('chai')
chai.should()

var chaiFiles = require('chai-files')
chai.use(chaiFiles)

var expect = chai.expect
var file = chaiFiles.file

function commit (msg) {
shell.exec('git commit --allow-empty -m"' + msg + '"')
Expand All @@ -24,6 +31,11 @@ function writePackageJson (version) {
}), 'utf-8')
}

function writeCordovaConfig (version, androidVersionCode) {
var content = '\x3C?xml version=\'1.0\' encoding=\'utf-8\'?\x3E\n\x3Cwidget id="com.app.cordova" version="' + version + '" android-versionCode="' + androidVersionCode + '" xmlns="http:\x2F\x2Fwww.w3.org\x2Fns\x2Fwidgets" xmlns:cdv="http:\x2F\x2Fcordova.apache.org\x2Fns\x2F1.0" xmlns:gap="http:\x2F\x2Fphonegap.com\x2Fns\x2F1.0" xmlns:android="http:\x2F\x2Fschemas.android.com\x2Fapk\x2Fres\x2Fandroid"\x3E\n\x3C\x2Fwidget\x3E'
fs.writeFileSync('config.xml', content, 'utf-8')
}

function writeGitPreCommitHook () {
fs.writeFileSync('.git/hooks/pre-commit', '#!/bin/sh\necho "precommit ran"\nexit 1', 'utf-8')
fs.chmodSync('.git/hooks/pre-commit', '755')
Expand Down Expand Up @@ -89,6 +101,60 @@ describe('cli', function () {
})
})

describe('cordova', function () {
it("config.xml doesn't exits", function () {
writePackageJson('1.2.9')
execCli().code.should.equal(0)
expect(file('config.xml')).to.not.exist
})

describe('config.xml exits', function () {
it('increase cordova version', function () {
writeCordovaConfig('1.2.15', '1020150')
writePackageJson('1.2.15')
execCli().code.should.equal(0)
var content = fs.readFileSync('config.xml', 'utf-8')
content.should.match(/version="1.2.16"/)
})

it('config.xml increase android-versionCode', function () {
writeCordovaConfig('1.2.15', '1020150')
writePackageJson('1.2.15')
execCli().code.should.equal(0)
var content = fs.readFileSync('config.xml', 'utf-8')
content.should.match(/android-versionCode="1020160"/)
})

it('ignore android version code', function () {
writePackageJson('1.2.3')
writeCordovaConfig('1.2.3', '1234567')
execCli().code.should.equal(0)
var content = fs.readFileSync('config.xml', 'utf-8')
content.should.match(/version="1.2.4"/)
content.should.match(/android-versionCode="1020040"/)
})

it('--sign signs the commit and tag', function () {
// mock git with file that writes args to gitcapture.log
return mockGit('require("fs").appendFileSync("gitcapture.log", JSON.stringify(process.argv.splice(2)) + "\\n")')
.then(function (unmock) {
writePackageJson('1.0.0')
writeCordovaConfig('1.0.0', '100000')

execCli('--sign').code.should.equal(0)

var captured = shell.cat('gitcapture.log').stdout.split('\n').map(function (line) {
return line ? JSON.parse(line) : line
})
captured[captured.length - 3].should.deep.equal(['commit', '-S', 'package.json', 'config.xml', 'CHANGELOG.md', '-m', 'chore(release): 1.0.1'])
captured[captured.length - 2].should.deep.equal(['tag', '-s', 'v1.0.1', '-m', 'chore(release): 1.0.1'])

unmock()
})
})
})
})

describe('with mocked git', function () {
it('--sign signs the commit and tag', function () {
// mock git with file that writes args to gitcapture.log
Expand Down

0 comments on commit dda5409

Please sign in to comment.