Skip to content

Commit

Permalink
feat(format): Add option to enable emoji (disabled by default)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: emoji are disabled by default.
Set option `emoji` to `true` to enable.
  • Loading branch information
pvdlg committed Aug 27, 2017
1 parent 51948e5 commit 06d18aa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ You can customize cz-conventional-commit in `package.json`:
"path": "@metahub/cz-conventional-commit",
"cz-conventional-commit": {
"maxSubjectLength": 72,
"bodyLineLength": 100
"bodyLineLength": 100,
"emoji": true
}
}
}
Expand All @@ -62,6 +63,7 @@ See [commitizen documentation](https://github.com/commitizen/cz-cli#making-your-
| ------------------ | ------------------------------------------------------------------------------------------ | ------- |
| `maxSubjectLength` | Length at which to truncate the commit head (head includes type, scope, subject and emoji) | 72 |
| `bodyLineLength` | Length at which to wrap body lines | 100 |
| `emoji` | To add and emoji at the end of the commit message | `false` |

## Commit types

Expand Down
8 changes: 4 additions & 4 deletions src/format-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {truncate} from 'lodash';
* @param {Object} config configuration
* @param {string} config.maxSubjectLength maximum subject length
* @param {string} config.bodyLineLength length of body lines
*
* @param {boolean} config.emoji `true` to add emoji at the end of the commit message
* @return {string} formatted commit
*/
export default function formatCommit(answers, options, config) {
Expand All @@ -39,12 +39,12 @@ export default function formatCommit(answers, options, config) {
const scope = answers.scope ? answers.scope.trim() : '';
const isAlias = !Object.prototype.hasOwnProperty.call(types, answers.type);
const type = isAlias ? aliases[answers.type].type : answers.type;
const emoji = isAlias ? aliases[answers.type].emoji : types[type].emoji;
const emoji = config.emoji ? (isAlias ? aliases[answers.type].emoji : types[type].emoji) : '';
// Limit head to maxSubjectLength (including the trailing emoji)
// We use the String.length function to determine the length of the emoji even it returns an innacurate value (2 chars per emoji), because most likely a commit hook or linter veryfyingt he head length will also use String.length.
const head = `${truncate(`${type}${scope ? `(${scope})` : ''}: ${answers.subject.trim()}`, {
length: config.maxSubjectLength - emoji.length - 1,
})} ${emoji}`;
length: config.maxSubjectLength - (config.emoji ? emoji.length + 1 : 0),
})}${config.emoji ? ` ${emoji}` : ''}`;
const body = wrap(answers.body, wrapOptions);
const footer = [
answers.breaking
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {merge} from 'lodash';
import questions from './questions';
import format from './format-commit';

const config = merge(configLoader.load(), {'cz-conventional-commit': {maxSubjectLength: 72, bodyLineLength: 100}})[
'cz-conventional-commit'
];
const config = merge(configLoader.load(), {
'cz-conventional-commit': {maxSubjectLength: 72, bodyLineLength: 100, emoji: false},
})['cz-conventional-commit'];

module.exports = {
prompter(cz, commit) {
Expand Down
1 change: 1 addition & 0 deletions src/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {typesOrder} from 'conventional-changelog-metahub/types';
* @param {Object} config configuration
* @param {string} config.maxSubjectLength maximum subject length
* @param {string} config.bodyLineLength length of body lines
* @param {boolean} config.emoji `true` to add emoji at the end of the commit message
*/
export default function questions(options, config) {
const {types, aliases} = options;
Expand Down
31 changes: 28 additions & 3 deletions test/format-commit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ import formatCommit from '../lib/format-commit';

const config = {maxSubjectLength: 50, bodyLineLength: 72};

test('Each type and aliases produce a commit with the input subject', t => {
const input = {
subject: 'Commit subject',
};

Object.keys(types).concat(Object.keys(aliases)).forEach(type => {
t.is(parser.sync(formatCommit(Object.assign({}, {type}, input), {types, aliases}, config)).subject, input.subject);
});
});

test('Each type and aliases produce a commit with a subject ending with the appropriate emoji', t => {
const input = {
subject: 'Commit subject',
};

Object.keys(types).forEach(type => {
Object.keys(types).concat(Object.keys(aliases)).forEach(type => {
t.true(
parser
.sync(formatCommit(Object.assign({type}, input), {types, aliases}, config))
.sync(
formatCommit(Object.assign({}, {type}, input), {types, aliases}, Object.assign({}, config, {emoji: true}))
)
.subject.endsWith(aliases[type] ? aliases[type].emoji : types[type].emoji)
);
});
Expand All @@ -41,7 +53,20 @@ test('Does not add unnecessary new line at teh end of the commit message', t =>
t.false(formatCommit(input, {types, aliases}, config).endsWith('\n'));
});

test('Truncate the subject to fit 50 characters', t => {
test('Truncate the subject to fit 50 characters (with emoji)', t => {
const input = {
type: 'feat',
subject:
'Veryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy long suject',
};

t.is(
parser.sync(formatCommit(input, {types, aliases}, Object.assign({}, config, {emoji: true}))).header.length,
config.maxSubjectLength
);
});

test('Truncate the subject to fit 50 characters (without emoji)', t => {
const input = {
type: 'feat',
subject:
Expand Down

0 comments on commit 06d18aa

Please sign in to comment.