Skip to content

Commit

Permalink
feat: implement subject feedback, scope filtering, subject filtering …
Browse files Browse the repository at this point in the history
…and config through package.json

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 committed Jan 10, 2019
1 parent 65d6a9f commit 64ac48e
Show file tree
Hide file tree
Showing 5 changed files with 7,479 additions and 29 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -7,8 +7,10 @@ notifications:
email: false
node_js:
- '4'
- '6'
- '8'
before_install:
- npm i -g npm@^2.0.0
- npm i -g npm
before_script:
- npm prune
after_success:
Expand Down
72 changes: 53 additions & 19 deletions engine.js
Expand Up @@ -4,13 +4,32 @@ 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.maxLineWidth - 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.
Expand All @@ -26,6 +45,7 @@ module.exports = function (options) {
};
});


return {
// When a user runs `git cz`, prompter will
// be executed. We pass you cz, which currently
Expand All @@ -39,7 +59,7 @@ 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');
console.log('\nLine 1 will have the maximum length of ' + options.maxLineWidth + ' characters (enforced). All other lines will be wrapped after ' + options.maxLineWidth + ' characters.\n');

// Let's ask some questions of the user
// so that we can populate our commit
Expand All @@ -58,13 +78,32 @@ module.exports = function (options) {
}, {
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',
Expand Down Expand Up @@ -98,35 +137,30 @@ module.exports = function (options) {
}
]).then(function(answers) {

var maxLineWidth = 100;

var wrapOptions = {
trim: true,
newline: '\n',
indent:'',
width: maxLineWidth
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 72 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 ? 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
14 changes: 9 additions & 5 deletions index.js
Expand Up @@ -2,12 +2,16 @@

var engine = require('./engine');
var conventionalCommitTypes = require('conventional-commit-types');
var configLoader = require('commitizen').configLoader

var config = configLoader.load()

module.exports = engine({
types: conventionalCommitTypes.types,
defaultType: process.env.CZ_TYPE,
defaultScope: process.env.CZ_SCOPE,
defaultSubject: process.env.CZ_SUBJECT,
defaultBody: process.env.CZ_BODY,
defaultIssues: process.env.CZ_ISSUES
defaultType: process.env.CZ_TYPE || config.defaultType,
defaultScope: process.env.CZ_SCOPE || config.defaultScope,
defaultSubject: process.env.CZ_SUBJECT || config.defaultSubject,
defaultBody: process.env.CZ_BODY || config.defaultBody,
defaultIssues: process.env.CZ_ISSUES || config.defaultIssues,
maxLineWidth: process.env.CZ_MAX_LINE_WIDTH || config.maxLineWidth || 100,
});

0 comments on commit 64ac48e

Please sign in to comment.