Skip to content

Commit

Permalink
feat: add option for breakline in footer and body (#80)
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #29
  • Loading branch information
leonardoanalista authored Apr 5, 2019
1 parent b46ec36 commit 61c1869
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Here are the options you can set in your `.cz-config.js`:
* **ticketNumberPrefix**: {string, default 'ISSUES CLOSED:'}: Set custom prefix for footer ticker number.
* **breakingPrefix**: {string, default 'BREAKING CHANGE:'}: Set a custom prefix for the breaking change block in commit messages.
* **footerPrefix**: {string, default 'ISSUES CLOSED:'}: Set a custom prefix for the footer block in commit messages. Set to empty string to remove prefix.
* **breaklineChar**: {string, default '|'}: It gets replaced with \n to create the breakline in your commit message. This is supported for fields `body` and `footer` at the moment.

## Related tools
- (https://github.com/commitizen/cz-cli)
Expand Down
62 changes: 34 additions & 28 deletions buildCommit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,71 @@ const wrap = require('word-wrap');

const defaultSubjectSeparator = ': ';
const defaultMaxLineWidth = 100;
const defaultBreaklineChar = '|';

function addTicketNumber(ticketNumber, config) {
const addTicketNumber = (ticketNumber, config) => {
if (!ticketNumber) {
return '';
}
if (config.ticketNumberPrefix) {
return `${config.ticketNumberPrefix + ticketNumber.trim()} `;
}
return `${ticketNumber.trim()} `;
}
};

function addScope(scope, config) {
const addScope = (scope, config) => {
const separator = _.get(config, 'subjectSeparator', defaultSubjectSeparator);

if (!scope) return separator; // it could be type === WIP. So there is no scope

return `(${scope.trim()})${separator}`;
}
};

function addSubject(subject) {
return _.trim(subject);
}
const addSubject = subject => _.trim(subject);

function addType(type, config) {
const addType = (type, config) => {
const prefix = _.get(config, 'typePrefix', '');
const suffix = _.get(config, 'typeSuffix', '');

return _.trim(`${prefix}${type}${suffix}`);
}
};

function addFooter(footer, config) {
const addBreaklinesIfNeeded = (value, breaklineChar = defaultBreaklineChar) =>
value
.split(breaklineChar)
.join('\n')
.valueOf();

const addFooter = (footer, config) => {
if (config && config.footerPrefix === '') return `\n\n${footer}`;

const footerPrefix = config && config.footerPrefix ? config.footerPrefix : 'ISSUES CLOSED:';
return `\n\n${footerPrefix} ${footer}`;
}

module.exports = function buildCommit(answers, config) {
return `\n\n${footerPrefix} ${addBreaklinesIfNeeded(footer, config.breaklineChar)}`;
};

const escapeSpecialChars = result => {
// eslint-disable-next-line no-useless-escape
const specialChars = ['`'];

let newResult = result;
// eslint-disable-next-line array-callback-return
specialChars.map(item => {
// If user types "feat: `string`", the commit preview should show "feat: `\string\`".
// Don't worry. The git log will be "feat: `string`"
newResult = result.replace(new RegExp(item, 'g'), '\\`');
});
return newResult;
};

module.exports = (answers, config) => {
const wrapOptions = {
trim: true,
newline: '\n',
indent: '',
width: defaultMaxLineWidth,
};

function escapeSpecialChars(result) {
// eslint-disable-next-line no-useless-escape
const specialChars = ['`'];

let newResult = result;
// eslint-disable-next-line array-callback-return
specialChars.map(item => {
// If user types "feat: `string`", the commit preview should show "feat: `\string\`".
// Don't worry. The git log will be "feat: `string`"
newResult = result.replace(new RegExp(item, 'g'), '\\`');
});
return newResult;
}

// Hard limit this line
// eslint-disable-next-line max-len
const head = (
Expand All @@ -73,7 +79,7 @@ module.exports = function buildCommit(answers, config) {

// Wrap these lines at 100 characters
let body = wrap(answers.body, wrapOptions) || '';
body = body.split('|').join('\n');
body = addBreaklinesIfNeeded(body, config.breaklineChar);

const breaking = wrap(answers.breaking, wrapOptions);
const footer = wrap(answers.footer, wrapOptions);
Expand Down
2 changes: 2 additions & 0 deletions cz-config-EXAMPLE.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ module.exports = {

// limit subject length
subjectLimit: 100,
// breaklineChar: '|', // It is supported for fields body and footer.
// footerPrefix : 'ISSUES CLOSED:', // default value
};
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const log = require('./logger');
const buildCommit = require('./buildCommit');

/* istanbul ignore next */
function readConfigFile() {
const readConfigFile = () => {
// First try to find the .cz-config.js config file
const czConfig = findConfig.require(CZ_CONFIG_NAME, { home: false });

Expand Down Expand Up @@ -43,7 +43,7 @@ function readConfigFile() {
'Unable to find a configuration file. Please refer to documentation to learn how to ser up: https://github.com/leonardoanalista/cz-customizable#steps "'
);
return null;
}
};

module.exports = {
prompter(cz, commit) {
Expand Down
4 changes: 2 additions & 2 deletions questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const log = require('./logger');

const isNotWip = answers => answers.type.toLowerCase() !== 'wip';

function isValidateTicketNo(value, config) {
const isValidateTicketNo = (value, config) => {
if (!value) {
return !config.isTicketNumberRequired;
}
Expand All @@ -15,7 +15,7 @@ function isValidateTicketNo(value, config) {
return false;
}
return true;
}
};

module.exports = {
getQuestions(config, cz) {
Expand Down
36 changes: 36 additions & 0 deletions spec/buildCommitSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,40 @@ describe('buildCommit()', () => {
expect(buildCommit(answersNoScope, options)).toEqual('[feat] this is a new feature');
});
});

describe('pipe replaced with new line', () => {
// I know it looks weird on tests but this proves to have the correct breakline inserted.
const expecteMessage = `feat: this is a new feature\n
body with new line now
body line2
ISSUES CLOSED: footer with new line
line 2`;

it('should add breakline for body and footer', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
body: 'body with new line now|body line2',
footer: 'footer with new line|line 2',
};
const options = {};

expect(buildCommit(answersNoScope, options)).toEqual(expecteMessage);
});

it('should override breakline character with option breaklineChar', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
body: 'body with new line now@@@body line2',
footer: 'footer with new line@@@line 2',
};
const options = {
breaklineChar: '@@@',
};

expect(buildCommit(answersNoScope, options)).toEqual(expecteMessage);
});
});
});

0 comments on commit 61c1869

Please sign in to comment.