Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@marionebl/conventional-commits-parser

Parse raw conventional commits

conventional-commits-parser, changelog, conventional, parser, parsing, logs

readme

NPM version Build Status Dependency Status Coverage Status

Parse raw conventional commits

Conventional Commit Message Format

A minimum input should contain a raw message.

Each commit message consists of a merge header, a header (mandatory), a body and a footer. Mention (optional) someone using the @ notation.

<merge>
<header>
<body>
<footer>

merge

The merge header may optionally have a special format that includes other parts, such as branch, issueId or source.

Merge branch <branch>
Merge pull request <issue-id> from <source>

The header may optionally have a special format that includes other parts, such as type, scope and subject. You could reference (optional) issues here.

<type>(<scope>): <subject>

The footer should contain any information about Important Notes (optional) and is also the place to reference (optional) issues.

<important note>
<references>

other parts

This module will only parse the message body. However, it is possible to include other fields such as hash, committer or date.

My commit message
-sideNotes-
It should warn the correct unfound file names.
Also it should continue if one file cannot be found.
Tests are added for these

Then sideNotes will be It should warn the correct unfound file names.\nAlso it should continue if one file cannot be found.\nTests are added for these. You can customize the fieldPattern.

Install

$ npm install --save conventional-commits-parser

Usage

var conventionalCommitsParser = require('conventional-commits-parser');

conventionalCommitsParser(options);

It returns a transform stream and expects an upstream that looks something like this:

'feat(scope): broadcast $destroy event on scope destruction\nCloses #1'
'feat(ng-list): Allow custom separator\nbla bla bla\n\nBREAKING CHANGE: some breaking change.\nThanks @stevemao\n'

Each chunk should be a commit. The downstream will look something like this:

{ type: 'feat',
  scope: 'scope',
  subject: 'broadcast $destroy event on scope destruction',
  merge: null,
  header: 'feat(scope): broadcast $destroy event on scope destruction',
  body: null,
  footer: 'Closes #1',
  notes: [],
  references:
   [ { action: 'Closes',
       owner: null,
       repository: null,
       issue: '1',
       raw: '#1',
       prefix: '#' } ],
  mentions: [],
  revert: null }
{ type: 'feat',
  scope: 'ng-list',
  subject: 'Allow custom separator',
  merge: null,
  header: 'feat(ng-list): Allow custom separator',
  body: 'bla bla bla',
  footer: 'BREAKING CHANGE: some breaking change.\nThanks @stevemao',
  notes:
   [ { title: 'BREAKING CHANGE',
       text: 'some breaking change.\nThanks @stevemao' } ],
  references: [],
  mentions: [ 'stevemao' ],
  revert: null }

API

conventionalCommitsParser([options])

Returns an transform stream. If there is any malformed commits it will be gracefully ignored (an empty data will be emitted so down stream can notice).

options

Type: object

mergePattern

Type: regex or string Default: null

Pattern to match merge headers. EG: branch merge, GitHub or GitLab like pull requests headers. When a merge header is parsed, the next line is used for conventionnal header parsing.

For example, if we have a commit

Merge pull request #1 from user/feature/feature-name

feat(scope): broadcast $destroy event on scope destruction

We can parse it with these options and the default headerPattern:

{
  mergePattern: /^Merge pull request #(\d+) from (.*)$/,
  mergeCorrespondence: ['id', 'source']
}
mergeCorrespondence

Type: array of string or string Default: null

Used to define what capturing group of mergePattern.

If it's a string it will be converted to an array separated by a comma.

headerPattern

Type: regex or string Default: /^(\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)$/

Used to match header pattern.

headerCorrespondence

Type: array of string or string Default ['type', 'scope', 'subject']

Used to define what capturing group of headerPattern captures what header part. The order of the array should correspond to the order of headerPattern's capturing group. If the part is not captured it is null. If it's a string it will be converted to an array separated by a comma.

referenceActions

Type: array of string or string Default: [ 'close', 'closes', 'closed', 'fix', 'fixes', 'fixed', 'resolve', 'resolves', 'resolved' ]

Keywords to reference an issue. This value is case insensitive. If it's a string it will be converted to an array separated by a comma.

Set it to null to reference an issue without any action.

issuePrefixes

Type: array of string or string Default: ['#']

The prefixes of an issue. EG: In gh-123 gh- is the prefix.

noteKeywords

Type: array of string or string Default: ['BREAKING CHANGE']

Keywords for important notes. This value is case insensitive. If it's a string it will be converted to an array separated by a comma.

fieldPattern

Type: regex or string Default: /^-(.*?)-$/

Pattern to match other fields.

revertPattern

Type: regex or string Default: /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./

Pattern to match what this commit reverts.

revertCorrespondence

Type: array of string or string Default: ['header', 'hash']

Used to define what capturing group of revertPattern captures what reverted commit fields. The order of the array should correspond to the order of revertPattern's capturing group.

For example, if we had commit

Revert "throw an error if a callback is passed"

This reverts commit 9bb4d6c.

If configured correctly, the parsed result would be

{
  revert: {
    header: 'throw an error if a callback is passed',
    hash: '9bb4d6c'
  }
}

It implies that this commit reverts a commit with header 'throw an error if a callback is passed' and hash '9bb4d6c'.

If it's a string it will be converted to an array separated by a comma.

warn

Type: function or boolean Default: function() {}

What warn function to use. For example, console.warn.bind(console) or grunt.log.writeln. By default, it's a noop. If it is true, it will error if commit cannot be parsed (strict).

conventionalCommitsParser.sync(commit, [options])

The sync version. Useful when parsing a single commit. Returns the result.

commit

A single commit to be parsed.

options

Same as the options of conventionalCommitsParser.

CLI

You can use cli to practice writing commit messages or parse messages from files. Note: the sample output might be different. It's just for demonstration purposes.

$ npm install --global conventional-commits-parser

If you run conventional-commits-parser without any arguments

$ conventional-commits-parser

You will enter an interactive shell. To show your parsed output enter "return" three times (or enter your specified separator).

> fix(title): a title is fixed


{"type":"fix","scope":"title","subject":"a title is fixed","header":"fix(title): a title is fixed","body":null,"footer":null,"notes":[],"references":[],"revert":null}

You can also use cli to parse messages from files.

If you have log.txt

feat(ngMessages): provide support for dynamic message resolution

Prior to this fix it was impossible to apply a binding to a the ngMessage directive to represent the name of the error.

BREAKING CHANGE: The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.

Closes #10036
Closes #9338

And you run

$ conventional-commits-parser log.txt
# or
$ cat log.txt | conventional-commits-parser

An array of json will be printed to stdout.

[
{"type":"feat","scope":"ngMessages","subject":"provide support for dynamic message resolution","header":"feat(ngMessages): provide support for dynamic message resolution","body":"Prior to this fix it was impossible to apply a binding to a the ngMessage directive to represent the name of the error.","footer":"BREAKING CHANGE: The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.\nCloses #10036\nCloses #9338","notes":[{"title":"BREAKING CHANGE","text":"The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive."}],"references":[{"action":"Closes","owner":null,"repository":null,"issue":"10036","raw":"#10036"},{"action":"Closes","owner":null,"repository":null,"issue":"9338","raw":"#9338"}],"revert":null}
]

Commits should be split by at least three newlines (\n\n\n) or you can specify a separator as the second argument.

Eg: in log2.txt


docs(ngMessageExp): split ngMessage docs up to show its alias more clearly
===

fix($animate): applyStyles from options on leave

Closes #10068

And you run

$ conventional-commits-parser log2.txt '==='
[
{"type":"docs","scope":"ngMessageExp","subject":"split ngMessage docs up to show its alias more clearly","header":"docs(ngMessageExp): split ngMessage docs up to show its alias more clearly","body":null,"footer":null,"notes":[],"references":[],"revert":null}
,
{"type":"fix","scope":"$animate","subject":"applyStyles from options on leave","header":"fix($animate): applyStyles from options on leave","body":null,"footer":"Closes #10068","notes":[],"references":[{"action":"Closes","owner":null,"repository":null,"issue":"10068","raw":"#10068"}],"revert":null}
]

Will be printed out.

You can specify one or more files. The output array will be in order of the input file paths. If you specify more than one separator, the last one will be used.

License

MIT © Steve Mao

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

  <a name="3.0.0"></a>

3.0.0 (2017-11-01)

Bug Fixes

  • cli: commit can be split when testing a commit file (f3b3a3f)
  • cli: error handling for ENOENT is fixed (3c92233)
  • cli: fix "undefined" in json string (0680e42)
  • cli: options format (491357e)
  • conventional-commits-parser: ignore comments (#231) (9db53e3), closes #224
  • deps: require split2 (1941c37)
  • error: change error type and wordings (d8be5e5)
  • footer: notes contains more than one paragraphs after references (d744ec7)
  • headerCorrespondence: string value for cli (fb774fc)
  • headerPattern: change how capturing groups works (fe1fe0c)
  • issuePrefixes: should return noMatch if falsy (72db2bf)
  • mention: fix mention matching (965986b), closes #26
  • newlines: preserve newlines in a part (06b8c7c), closes #15
  • notes: note keywords must appear at the beginning of a sentence (5a2059e), closes #23
  • parser: do not trim spaces but newlines (1e8c4c5)
  • parser: it returns null if there is no header (8571c9e)
  • regex: do not treat it as note if there are texts after keywords (571b03e)
  • regex: make getReferencePartsRegex stricter (62adf54), closes #27 #30 #27 #28
  • revertPattern: correct regex (8628983)
  • util: remove an accidentally commited file (3710a8c)
  • warn: should tell which commit cannot be parsed (04b0a9b)

Chores

Code Refactoring

  • breaks: change breaks to notes (5189a61), closes #2
  • merge: pull-request should be merge (4e7c61c)
  • regex: regex now takes options (eea319a)

Features

  • cli: able to have two files, separator works for interactive (db1e3b5)
  • cli: add aliases, more help details and tests (eb654a2)
  • cli: add missing options (8ac1cf7)
  • cli: add warn function for interactive shell (84fe31f)
  • correspondence: add headerCorrespondence and improve commit parts (aca9e95), closes #6
  • fieldPattern: should support string format (b6b6b52)
  • hash: drop support (1ccc751)
  • headerParts: headerParts can be anything (31e1c11), closes #10
  • issuePrefixes: init and referenceKeywords -> referenceActions (86bf798), closes #11
  • maxSubjectLength: removed (3448582)
  • mentions: @someone in commit (d60fe76), closes #24
  • newline: fields does not contain leading or trailing newlines (6db453b), closes #14
  • note: noteKeywords is case insensitive (f779a29), closes #21
  • options: all options can be a string (0fa17b4)
  • otherFields: other fields are possible to be included (9e06278)
  • improvements and bug fixes (1cde104), closes #5
  • migrate repo to lerna mono-repo (793e823)
  • regex: matching JIRA-123 like references (20f1f7a), closes #19
  • support squash commits (#31) (fff60c0)
  • owner: yield owner field (d8d0334), closes #12
  • parser: notes can have more than one paragraph (733bfa9), closes #3
  • pullRequest: Allow to skip and parse pull request header (a2e929f), closes #20
  • reference: able to reference an issue without an action (6474123), closes #22
  • reference: expose prefix (47df766), closes #17
  • references: allow header to reference an issue (df18a24)
  • references: support other formats of references (7c70213), closes #4 #8
  • regex: leading and trailing space for closes and breaks keywords are trimmed (9639860)
  • revert: parse a commit that reverts (2af7233)
  • stream: emmit an empty string to down stream if commit cannot be parsed (76bf84e)
  • sync: add the sync function (82071c6), closes #13
  • warn: optionally warn user what is wrong when commit cannot be parsed (32b3cda)

Performance Improvements

  • regex: regex should be constructed in index.html (15afd26)

BREAKING CHANGES

  • merge: pull request should be merge. Also make the parsed result to be consistent with other parts.
  • This module is imported from https://github.com/ajoslin/conventional-changelog, and is originally written by @vojtajina, @btford and @ajoslin.
  • hash: hash is no longer supported. This parser should just parse raw commit messages. Also text fields are appended with a newline " ".
  • regex: It returns a nomatch regex if it's keywords are missing.
  • headerParts: headerParts does not limit to type, scope and subject. They can now be defined in options.headerCorrespondence and the order is the order of capturing group in options.headerPattern. If part is missing in options.headerCorrespondence it is undefined. If part is not captured but is not missing in options.headerCorrespondence it is null.
  • maxSubjectLength: maxSubjectLength is not available any more.
  • issuePrefixes: options.referenceKeywords is now options.referenceActions
  • references: closes now becomes references and it is loosely based the links above.
  • parser: The regex for matching notes are loosen. The semicolon and space are optional. The notes object is no longer a key-value object but an array of note object, such as
    {
    title: 'BREAKING AMEND',
    text: 'some breaking change'
    }
    The detection of notes, closes, continueNote and isBody are mutually exclusive.
  • breaks: Variable name related to breaks changes to notes, because "Important Notes" a more generic term. There is no functional changes.
  • stream: It no longer skips the chunk if commit cannot be parsed. An empty string is passed to down stream
  • correspondence: body and footer will be null if they are not found. type and subject are nullable too.
  <a name="2.0.0"></a>

2.0.0 (2017-07-17)

Bug Fixes

  • cli: commit can be split when testing a commit file (f3b3a3f)
  • cli: error handling for ENOENT is fixed (3c92233)
  • cli: fix "undefined" in json string (0680e42)
  • cli: options format (491357e)
  • deps: require split2 (1941c37)
  • error: change error type and wordings (d8be5e5)
  • footer: notes contains more than one paragraphs after references (d744ec7)
  • headerCorrespondence: string value for cli (fb774fc)
  • headerPattern: change how capturing groups works (fe1fe0c)
  • issuePrefixes: should return noMatch if falsy (72db2bf)
  • mention: fix mention matching (965986b), closes #26
  • newlines: preserve newlines in a part (06b8c7c), closes #15
  • notes: note keywords must appear at the beginning of a sentence (5a2059e), closes #23
  • parser: do not trim spaces but newlines (1e8c4c5)
  • parser: it returns null if there is no header (8571c9e)
  • regex: do not treat it as note if there are texts after keywords (571b03e)
  • regex: make getReferencePartsRegex stricter (62adf54), closes #27 #30 #27 #28
  • revertPattern: correct regex (8628983)
  • util: remove an accidentally commited file (3710a8c)
  • warn: should tell which commit cannot be parsed (04b0a9b)

Chores

Code Refactoring

  • breaks: change breaks to notes (5189a61), closes #2
  • merge: pull-request should be merge (4e7c61c)
  • regex: regex now takes options (eea319a)

Features

  • cli: able to have two files, separator works for interactive (db1e3b5)
  • cli: add aliases, more help details and tests (eb654a2)
  • cli: add missing options (8ac1cf7)
  • cli: add warn function for interactive shell (84fe31f)
  • correspondence: add headerCorrespondence and improve commit parts (aca9e95), closes #6
  • fieldPattern: should support string format (b6b6b52)
  • hash: drop support (1ccc751)
  • headerParts: headerParts can be anything (31e1c11), closes #10
  • issuePrefixes: init and referenceKeywords -> referenceActions (86bf798), closes #11
  • maxSubjectLength: removed (3448582)
  • mentions: @someone in commit (d60fe76), closes #24
  • newline: fields does not contain leading or trailing newlines (6db453b), closes #14
  • note: noteKeywords is case insensitive (f779a29), closes #21
  • options: all options can be a string (0fa17b4)
  • otherFields: other fields are possible to be included (9e06278)
  • improvements and bug fixes (1cde104), closes #5
  • migrate repo to lerna mono-repo (793e823)
  • regex: matching JIRA-123 like references (20f1f7a), closes #19
  • support squash commits (#31) (fff60c0)
  • owner: yield owner field (d8d0334), closes #12
  • parser: notes can have more than one paragraph (733bfa9), closes #3
  • pullRequest: Allow to skip and parse pull request header (a2e929f), closes #20
  • reference: able to reference an issue without an action (6474123), closes #22
  • reference: expose prefix (47df766), closes #17
  • references: allow header to reference an issue (df18a24)
  • references: support other formats of references (7c70213), closes #4 #8
  • regex: leading and trailing space for closes and breaks keywords are trimmed (9639860)
  • revert: parse a commit that reverts (2af7233)
  • stream: emmit an empty string to down stream if commit cannot be parsed (76bf84e)
  • sync: add the sync function (82071c6), closes #13
  • warn: optionally warn user what is wrong when commit cannot be parsed (32b3cda)

Performance Improvements

  • regex: regex should be constructed in index.html (15afd26)

BREAKING CHANGES

  • merge: pull request should be merge. Also make the parsed result to be consistent with other parts.
  • This module is imported from https://github.com/ajoslin/conventional-changelog, and is originally written by @vojtajina, @btford and @ajoslin.
  • hash: hash is no longer supported. This parser should just parse raw commit messages. Also text fields are appended with a newline " ".
  • regex: It returns a nomatch regex if it's keywords are missing.
  • headerParts: headerParts does not limit to type, scope and subject. They can now be defined in options.headerCorrespondence and the order is the order of capturing group in options.headerPattern. If part is missing in options.headerCorrespondence it is undefined. If part is not captured but is not missing in options.headerCorrespondence it is null.
  • maxSubjectLength: maxSubjectLength is not available any more.
  • issuePrefixes: options.referenceKeywords is now options.referenceActions
  • references: closes now becomes references and it is loosely based the links above.
  • parser: The regex for matching notes are loosen. The semicolon and space are optional. The notes object is no longer a key-value object but an array of note object, such as
    {
    title: 'BREAKING AMEND',
    text: 'some breaking change'
    }
    The detection of notes, closes, continueNote and isBody are mutually exclusive.
  • breaks: Variable name related to breaks changes to notes, because "Important Notes" a more generic term. There is no functional changes.
  • stream: It no longer skips the chunk if commit cannot be parsed. An empty string is passed to down stream
  • correspondence: body and footer will be null if they are not found. type and subject are nullable too.

    1.3.0 (2016-10-15)

Features

  • support squash commits (#31) (860c7a1)

1.2.3 (2016-08-06)

Bug Fixes

  • regex: do not treat it as note if there are texts after keywords (9cb56bc)

1.2.2 (2016-05-04)

Bug Fixes

1.2.1 (2016-04-24)

Bug Fixes

  • mention: fix mention matching (43b32e7), closes #26

1.2.0 (2016-04-15)

Features

  • mentions: @someone in commit (b2eabbf), closes #24

1.1.0 (2016-04-10)

Bug Fixes

  • notes: note keywords must appear at the beginning of a sentence (6e13789), closes #23

Features

  • reference: able to reference an issue without an action (cf847b1), closes #22

1.0.1 (2016-02-05)

Bug Fixes

1.0.0 (2016-02-05)

0.2.0 (2016-02-04)

Features

  • note: noteKeywords is case insensitive (4442b86), closes #21
  • pullRequest: Allow to skip and parse pull request header (aa85033), closes #20
  • regex: matching JIRA-123 like references (5342f45), closes #19

0.1.2 (2015-09-18)

Bug Fixes

  • parser: do not trim spaces but newlines (62e7bf5)

0.1.1 (2015-09-12)

Bug Fixes

  • newlines: preserve newlines in a part (beb3d05), closes #15

Features