Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: implement configuration through package.json (#75)
cz-conventional-changelog can now have its values (specifically all the defaults and maxLineWidth)
through the config.commitizen key in the package.json file.  The scope now automatically becomes
lowercase (as is required for conventional changelogs) and is prompted on the same line (as it is
always short and doesn't need an additional line). The subject question now indicates the total
number of characters that are allowed based upon the maxLineWidth configuration (or 100 as it is now
by default), the length of the type, and scope. Validation prevents entering more than the allowed
number of characters with feedback of the number of characters entered.  Subject will always have a
lowercase first letter and strip any trailing dots (as is required by the conventional changelog
standard).  'commitizen' and 'semantic-release' have been updated to the most recent versions
(because of current vunderabilites) and the .travisci file has been updated to reflect newer node
versions.
  • Loading branch information
yinzara authored and jimthedev committed Apr 18, 2019
1 parent 65d6a9f commit 3398c02
Show file tree
Hide file tree
Showing 9 changed files with 12,806 additions and 62 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
@@ -0,0 +1,8 @@
[*]
end_of_line = lf
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true
5 changes: 5 additions & 0 deletions .prettierrc
@@ -0,0 +1,5 @@
{
"printWidth": 80,
"endOfLine": "lf",
"singleQuote": true
}
29 changes: 16 additions & 13 deletions .travis.yml
@@ -1,18 +1,21 @@
sudo: false
language: node_js
cache:
directories:
- node_modules
notifications:
email: false

node_js:
- '4'
before_install:
- npm i -g npm@^2.0.0
before_script:
- npm prune
after_success:
- npm run semantic-release
- 10
- 8
- 6
- 4

jobs:
include:
- stage: release
node_js: lts/*
deploy:
provider: script
skip_cleanup: true
script:
- npm semantic-release

branches:
only:
- master
40 changes: 40 additions & 0 deletions README.md
Expand Up @@ -6,3 +6,43 @@ Status:
[![Build Status](https://img.shields.io/travis/commitizen/cz-conventional-changelog.svg?style=flat-square)](https://travis-ci.org/commitizen/cz-conventional-changelog)

Part of the [commitizen](https://github.com/commitizen/cz-cli) family. Prompts for [conventional changelog](https://github.com/conventional-changelog/conventional-changelog) standard.

## Configuration

### package.json

Like commitizen, you specify the configuration of cz-conventional-changelog through the package.json's `config.commitizen` key.

```json5
{
// ... default values
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog",
"maxHeaderWidth": 100,
"maxLineWidth": 100,
"defaultType": "",
"defaultScope": "",
"defaultSubject": "",
"defaultBody": "",
"defaultIssues": ""
}
}
// ...
}
```
### Environment variables

The following environment varibles can be used to override any default configuration or package.json based configuration.

* CZ_TYPE = defaultType
* CZ_SCOPE = defaultScope
* CZ_SUBJECT = defaultSubject
* CZ_BODY = defaultBody
* CZ_MAX_HEADER_WIDTH = maxHeaderWidth
* CZ_MAX_LINE_WIDTH = maxLineWidth

### Commitlint

If using the [commitlint](https://github.com/conventional-changelog/commitlint) js library, the "maxHeaderWidth" configuration property will default to the configuration of the "header-max-length" rule instead of the hard coded value of 100. This can be ovewritten by setting the 'maxHeaderWidth' configuration in package.json or the CZ_MAX_HEADER_WIDTH environment variable.

159 changes: 123 additions & 36 deletions engine.js
@@ -1,25 +1,47 @@
"format cjs";
'format cjs';

var wrap = require('word-wrap');
var map = require('lodash.map');
var longest = require('longest');
var rightPad = require('right-pad');
var chalk = require('chalk');

var filter = function(array) {
return array.filter(function(x) {
return x;
});
};

var headerLength = function(answers) {
return (
answers.type.length + 2 + (answers.scope ? answers.scope.length + 2 : 0)
);
};

var maxSummaryLength = function(options, answers) {
return options.maxHeaderWidth - headerLength(answers);
};

var filterSubject = function(subject) {
subject = subject.trim();
if (subject.charAt(0).toLowerCase() !== subject.charAt(0)) {
subject =
subject.charAt(0).toLowerCase() + subject.slice(1, subject.length);
}
while (subject.endsWith('.')) {
subject = subject.slice(0, subject.length - 1);
}
return subject;
};

// This can be any kind of SystemJS compatible module.
// We use Commonjs here, but ES6 or AMD would do just
// fine.
module.exports = function (options) {

module.exports = function(options) {
var types = options.types;

var length = longest(Object.keys(types)).length + 1;
var choices = map(types, function (type, key) {
var choices = map(types, function(type, key) {
return {
name: rightPad(key + ':', length) + ' ' + type.description,
value: key
Expand All @@ -39,8 +61,6 @@ module.exports = function (options) {
// By default, we'll de-indent your commit
// template and will keep empty lines.
prompter: function(cz, commit) {
console.log('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');

// Let's ask some questions of the user
// so that we can populate our commit
// template.
Expand All @@ -52,42 +72,112 @@ module.exports = function (options) {
{
type: 'list',
name: 'type',
message: 'Select the type of change that you\'re committing:',
message: "Select the type of change that you're committing:",
choices: choices,
default: options.defaultType
}, {
},
{
type: 'input',
name: 'scope',
message: 'What is the scope of this change (e.g. component or file name)? (press enter to skip)\n',
default: options.defaultScope
}, {
message:
'What is the scope of this change (e.g. component or file name): (press enter to skip)',
default: options.defaultScope,
filter: function(value) {
return value.trim().toLowerCase();
}
},
{
type: 'input',
name: 'subject',
message: 'Write a short, imperative tense description of the change:\n',
default: options.defaultSubject
}, {
message: function(answers) {
return (
'Write a short, imperative tense description of the change (max ' +
maxSummaryLength(options, answers) +
' chars):\n'
);
},
default: options.defaultSubject,
validate: function(subject, answers) {
var filteredSubject = filterSubject(subject);
return filteredSubject.length == 0
? 'subject is required'
: filteredSubject.length <= maxSummaryLength(options, answers)
? true
: 'Subject length must be less than or equal to ' +
maxSummaryLength(options, answers) +
' characters. Current length is ' +
filteredSubject.length +
' characters.';
},
transformer: function(subject, answers) {
var filteredSubject = filterSubject(subject);
var color =
filteredSubject.length <= maxSummaryLength(options, answers)
? chalk.green
: chalk.red;
return color('(' + filteredSubject.length + ') ' + subject);
},
filter: function(subject) {
return filterSubject(subject);
}
},
{
type: 'input',
name: 'body',
message: 'Provide a longer description of the change: (press enter to skip)\n',
message:
'Provide a longer description of the change: (press enter to skip)\n',
default: options.defaultBody
}, {
},
{
type: 'confirm',
name: 'isBreaking',
message: 'Are there any breaking changes?',
default: false
}, {
},
{
type: 'input',
name: 'breakingBody',
default: '-',
message:
'A BREAKING CHANGE commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return answers.isBreaking && !answers.body;
},
validate: function(breakingBody, answers) {
return (
breakingBody.trim().length > 0 ||
'Body is required for BREAKING CHANGE'
);
}
},
{
type: 'input',
name: 'breaking',
message: 'Describe the breaking changes:\n',
when: function(answers) {
return answers.isBreaking;
}
}, {
},

{
type: 'confirm',
name: 'isIssueAffected',
message: 'Does this change affect any open issues?',
default: options.defaultIssues ? true : false
}, {
},
{
type: 'input',
name: 'issuesBody',
default: '-',
message:
'If issues are closed, the commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return (
answers.isIssueAffected && !answers.body && !answers.breakingBody
);
}
},
{
type: 'input',
name: 'issues',
message: 'Add issue references (e.g. "fix #123", "re #123".):\n',
Expand All @@ -97,36 +187,33 @@ module.exports = function (options) {
default: options.defaultIssues ? options.defaultIssues : undefined
}
]).then(function(answers) {

var maxLineWidth = 100;

var wrapOptions = {
trim: true,
cut: false,
newline: '\n',
indent:'',
width: maxLineWidth
indent: '',
width: options.maxLineWidth
};

// parentheses are only needed when a scope is present
var scope = answers.scope.trim();
scope = scope ? '(' + answers.scope.trim() + ')' : '';
var scope = answers.scope ? '(' + answers.scope + ')' : '';

// Hard limit this line
var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth);
// Hard limit this line in the validate
var head = answers.type + scope + ': ' + answers.subject;

// Wrap these lines at 100 characters
var body = wrap(answers.body, wrapOptions);
// Wrap these lines at options.maxLineWidth characters
var body = answers.body ? wrap(answers.body, wrapOptions) : false;

// Apply breaking change prefix, removing it if already present
var breaking = answers.breaking ? answers.breaking.trim() : '';
breaking = breaking ? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '') : '';
breaking = wrap(breaking, wrapOptions);

var issues = answers.issues ? wrap(answers.issues, wrapOptions) : '';
breaking = breaking
? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '')
: '';
breaking = breaking ? wrap(breaking, wrapOptions) : false;

var footer = filter([ breaking, issues ]).join('\n\n');
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;

commit(head + '\n\n' + body + '\n\n' + footer);
commit(filter([head, body, breaking, issues]).join('\n\n'));
});
}
};
Expand Down

0 comments on commit 3398c02

Please sign in to comment.