Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update wrap functionality #104

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions buildCommit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const wrap = require('word-wrap');
const defaultSubjectSeparator = ': ';
const defaultMaxLineWidth = 100;
const defaultBreaklineChar = '|';
const defaultBreakingPrefix = 'BREAKING CHANGE:';
const defaultFooterPrefix = 'ISSUES CLOSED:';

const addTicketNumber = (ticketNumber, config) => {
if (!ticketNumber) {
Expand Down Expand Up @@ -32,18 +34,35 @@ const addType = (type, config) => {
return _.trim(`${prefix}${type}${suffix}`);
};

const addBreaklinesIfNeeded = (value, breaklineChar = defaultBreaklineChar) =>
value
const addBreaklinesIfNeededAndWrap = (value = '', config) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds perfect to me! thanks for the fix!

const breaklineChar = _.get(config, 'breaklineChar', defaultBreaklineChar);
const wrapOptions = {
trim: true,
newline: '\n',
indent: '',
width: defaultMaxLineWidth,
};

return value
.split(breaklineChar)
// Wrap each line at 100 characters
.map(p => wrap(p, wrapOptions))
.join('\n')
.valueOf();
}

const addBreaking = (breaking, config) => {
const breakingPrefix = _.get(config, 'breakingPrefix', defaultBreakingPrefix);

return `\n\n${breakingPrefix}\n${breaking}`;
};

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

const footerPrefix = config && config.footerPrefix ? config.footerPrefix : 'ISSUES CLOSED:';
const footerPrefix = _.get(config, 'footerPrefix', defaultFooterPrefix);

return `\n\n${footerPrefix} ${addBreaklinesIfNeeded(footer, config.breaklineChar)}`;
return `\n\n${footerPrefix} ${addBreaklinesIfNeededAndWrap(footer, config)}`;
};

const escapeSpecialChars = result => {
Expand All @@ -61,13 +80,6 @@ const escapeSpecialChars = result => {
};

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

// Hard limit this line
// eslint-disable-next-line max-len
const head = (
Expand All @@ -77,23 +89,15 @@ module.exports = (answers, config) => {
addSubject(answers.subject)
).slice(0, defaultMaxLineWidth);

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

const breaking = wrap(answers.breaking, wrapOptions);
const footer = wrap(answers.footer, wrapOptions);

let result = head;
if (body) {
result += `\n\n${body}`;
if (answers.body) {
result += `\n\n${addBreaklinesIfNeededAndWrap(answers.body, config)}`;
}
if (breaking) {
const breakingPrefix = config && config.breakingPrefix ? config.breakingPrefix : 'BREAKING CHANGE:';
result += `\n\n${breakingPrefix}\n${breaking}`;
if (answers.breaking) {
result += addBreaking(answers.breaking, config);
}
if (footer) {
result += addFooter(footer, config);
if (answers.footer) {
result += addFooter(answers.footer, config);
}

return escapeSpecialChars(result);
Expand Down
33 changes: 33 additions & 0 deletions spec/buildCommitSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,37 @@ line 2`;
expect(buildCommit(answersNoScope, options)).toEqual(expecteMessage);
});
});

describe('wrap only separate line with 100 characters limit', () => {
// I know it looks weird on tests but this proves to have the correct breakline inserted.
const expecteMessage = `docs: update docs\n
1. update description
2. add commit message format section
3. add docker section
4. add commit helper tool section`;

it('should add breakline for body and wrap each line separately', () => {
const answersNoScope = {
type: 'docs',
subject: 'update docs',
body: '1. update description|2. add commit message format section|3. add docker section|4. add commit helper tool section'
};
const options = {};

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

it('should add breakline for body and wrap each line separately with option breaklineChar', () => {
const answersNoScope = {
type: 'docs',
subject: 'update docs',
body: '1. update description@@@2. add commit message format section@@@3. add docker section@@@4. add commit helper tool section',
};
const options = {
breaklineChar: '@@@',
};

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