Repository: conventional-changelog/standard-version
Pull request #224 (2017-11-25 07:47:52)

Feature/support multiple new version in format
# Description

Add support for commit message like this:

## In npm scripts

One can use `\n` here:

```json
  "scripts": {
    "release": "standard-version -m 'chore(release): %s \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v%s/CHANGELOG.md'"
  },
```

## In shell directly

But has to enter `\` manually in shell

```sh
> standard-version -m 'chore(release): %s \
quote> \
quote> \
quote> * CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v%s/CHANGELOG.md'
```

## which generates this in git commit message:

```txt
chore(release): 1.0.0

* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v1.0.0/CHANGELOG.md
```

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14360861/badge)](https://coveralls.io/builds/14360861)

Coverage increased (+0.007%) to 99.686% when pulling **823fd7129bc72fa2b659cb46c3fefdbfae6e3a05 on tomchentw:feature/support-multiple-new-version-in-format** into **3fdd7fa22fa40f2e9f899aac24493f391e72a074 on conventional-changelog:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14360863/badge)](https://coveralls.io/builds/14360863)

Coverage increased (+0.007%) to 99.686% when pulling **823fd7129bc72fa2b659cb46c3fefdbfae6e3a05 on tomchentw:feature/support-multiple-new-version-in-format** into **3fdd7fa22fa40f2e9f899aac24493f391e72a074 on conventional-changelog:master**.

tomchentw: @stevemao thanks for the review. Should I just merge with rebase directly or wait for the contributors?
tomchentw: nvm, it seems I'm also the owner (when?) of this repo LOL
stevemao: I'd run it pass @bcoe if you can wait
tomchentw: No problem.
bcoe: @stevemao @tomchentw this seems quite reasonable to me 👍 would.

Will this work if, when entering the message in the command line, I don't close the quotes and hit enter a few times in the terminal; the approach one usually takes when entering a multi-line git commit.
tomchentw: @bcoe I've tested and it seems to work. Basically, two ways:

## In npm scripts

One can use `\n` here:

```json
  "scripts": {
    "release": "standard-version -m 'chore(release): %s \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v%s/CHANGELOG.md'"
  },
```

## In shell directly

But has to enter `\` manually in shell

```sh
> standard-version -m 'chore(release): %s \
quote> \
quote> \
quote> * CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v%s/CHANGELOG.md'
```

Patch:
diff --git a/lib/format-commit-message.js b/lib/format-commit-message.js
index 06b8702..b79ad2e 100644
--- a/lib/format-commit-message.js
+++ b/lib/format-commit-message.js
@@ -1,5 +1,10 @@
 const util = require('util')
 
-module.exports = function (msg, newVersion) {
-  return String(msg).indexOf('%s') !== -1 ? util.format(msg, newVersion) : msg
+module.exports = function (rawMsg, newVersion) {
+  const message = String(rawMsg)
+  const matchCount = (message.match(/%s/g) || []).length
+  const args = Array(1 + matchCount)
+  args[0] = message
+  args.fill(newVersion, 1, args.length)
+  return util.format.apply(util, args)
 }


Test patch:
diff --git a/test.js b/test.js
index a99a555..b18cf71 100644
--- a/test.js
+++ b/test.js
@@ -9,6 +9,7 @@ var stream = require('stream')
 var mockGit = require('mock-git')
 var mockery = require('mockery')
 var semver = require('semver')
+var formatCommitMessage = require('./lib/format-commit-message')
 var cli = require('./command')
 var standardVersion = require('./index')
 
@@ -101,6 +102,18 @@ function getPackageVersion () {
   return JSON.parse(fs.readFileSync('package.json', 'utf-8')).version
 }
 
+describe('format-commit-message', function () {
+  it('works for no %s', function () {
+    formatCommitMessage('chore(release): 1.0.0', '1.0.0').should.equal('chore(release): 1.0.0')
+  })
+  it('works for one %s', function () {
+    formatCommitMessage('chore(release): %s', '1.0.0').should.equal('chore(release): 1.0.0')
+  })
+  it('works for two %s', function () {
+    formatCommitMessage('chore(release): %s \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v%s/CHANGELOG.md', '1.0.0').should.equal('chore(release): 1.0.0 \n\n* CHANGELOG: https://github.com/conventional-changelog/standard-version/blob/v1.0.0/CHANGELOG.md')
+  })
+})
+
 describe('cli', function () {
   beforeEach(initInTempFolder)
   afterEach(finishTemp)