Skip to content

Commit

Permalink
feat: adds "ticketNumberSuffix" and "ticketNumberPosition" config opt…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
dudekaa committed Oct 30, 2019
1 parent 51cab09 commit 1b4e88c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ Here are the options you can set in your `.cz-config.js`:
* **skipQuestions**: {Array of Strings: default none}. List of questions you want to skip. Eg.: ['body', 'footer'].
* **appendBranchNameToCommitMessage**: If you use `cz-customizable` with `cz-customizable-ghooks`, you can get the branch name automatically appended to the commit message. This is done by a commit hook on `cz-customizable-ghooks`. This option has been added on `cz-customizable-ghooks`, v1.3.0. Default value is `true`.
* **ticketNumberPrefix**: {string, default 'ISSUES CLOSED:'}: Set custom prefix for footer ticker number.
* **ticketNumberSuffix**: {string, default ' '}: Set custom prefix for footer ticker number.
* **ticketNumberPosition**: {string <small>[ 'first', 'standard' or 'last' ]</small>, default none}: Set custom placement 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.
Expand Down
57 changes: 46 additions & 11 deletions buildCommit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ const defaultMaxLineWidth = 100;
const defaultBreaklineChar = '|';

const addTicketNumber = (ticketNumber, config) => {
let result;

if (!ticketNumber) {
return '';
}

result = ticketNumber.trim();

if (config.ticketNumberPrefix) {
return `${config.ticketNumberPrefix + ticketNumber.trim()} `;
result = `${config.ticketNumberPrefix}${result}`;
}

if (config.ticketNumberSuffix || config.ticketNumberSuffix === '') {
result = `${result}${config.ticketNumberSuffix}`;
} else {
result = `${result} `;
}
return `${ticketNumber.trim()} `;

return result;
};

const addScope = (scope, config) => {
Expand Down Expand Up @@ -67,15 +79,38 @@ module.exports = (answers, config) => {
indent: '',
width: defaultMaxLineWidth,
};

// Hard limit this line
// eslint-disable-next-line max-len
const head = (
addType(answers.type, config) +
addScope(answers.scope, config) +
addTicketNumber(answers.ticketNumber, config) +
addSubject(answers.subject)
).slice(0, defaultMaxLineWidth);
let head;

switch (config.ticketNumberPosition) {
case 'first':
// Hard limit this line
head = (
addTicketNumber(answers.ticketNumber, config) +
addType(answers.type, config) +
addScope(answers.scope, config) +
addSubject(answers.subject)
).slice(0, defaultMaxLineWidth);
break;
case 'last':
// Hard limit this line
head = (
addType(answers.type, config) +
addScope(answers.scope, config) +
addSubject(answers.subject) +
addTicketNumber(answers.ticketNumber, config)
).slice(0, defaultMaxLineWidth);
break;
case 'standard':
default:
// Hard limit this line
head = (
addType(answers.type, config) +
addScope(answers.scope, config) +
addTicketNumber(answers.ticketNumber, config) +
addSubject(answers.subject)
).slice(0, defaultMaxLineWidth);
break;
}

// Wrap these lines at 100 characters
let body = wrap(answers.body, wrapOptions) || '';
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ declare module "cz-customizable" {
skipQuestions?: string[];
appendBranchNameToCommitMessage?: boolean;
ticketNumberPrefix?: string;
ticketNumberSuffix?: string;
ticketNumberPosition?: string;
breakingPrefix?: string;
footerPrefix?: string;
subjectLimit?: number;
Expand Down
57 changes: 57 additions & 0 deletions spec/buildCommitSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,61 @@ line 2`;
expect(buildCommit(answersNoScope, options)).toEqual(expecteMessage);
});
});

describe('with ticketNumberSuffix', () => {
it('should be visible', () => {
const answersTicketNumberSuffix = {
...answers,
ticketNumber: '12345',
};
const options = {
allowTicketNumber: true,
ticketNumberSuffix: '@@@ ',
};

expect(buildCommit(answersTicketNumberSuffix, options)).toEqual('feat(app): 12345@@@ this is a new feature');
});
});

describe('with ticketNumberPosition', () => {
it('should be same', () => {
const answersTicketNumberSuffix = {
...answers,
ticketNumber: '12345',
};
const options = {
allowTicketNumber: true,
ticketNumberPosition: 'standard',
};

expect(buildCommit(answersTicketNumberSuffix, options)).toEqual('feat(app): 12345 this is a new feature');
});

it('should be "first"', () => {
const answersTicketNumberSuffix = {
...answers,
ticketNumber: '12345',
};
const options = {
allowTicketNumber: true,
ticketNumberPosition: 'first',
};

expect(buildCommit(answersTicketNumberSuffix, options)).toEqual('12345 feat(app): this is a new feature');
});

it('should be "last"', () => {
const answersTicketNumberSuffix = {
...answers,
ticketNumber: '12345',
};
const options = {
allowTicketNumber: true,
ticketNumberSuffix: '',
ticketNumberPosition: 'last',
};

expect(buildCommit(answersTicketNumberSuffix, options)).toEqual('feat(app): this is a new feature12345');
});
});
});

0 comments on commit 1b4e88c

Please sign in to comment.