diff --git a/README.md b/README.md index 535a16b..498b838 100644 --- a/README.md +++ b/README.md @@ -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 [ 'first', 'standard' or 'last' ], 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. diff --git a/buildCommit.js b/buildCommit.js index d1288db..ab48d04 100644 --- a/buildCommit.js +++ b/buildCommit.js @@ -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) => { @@ -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) || ''; diff --git a/index.d.ts b/index.d.ts index c302607..c84125b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -22,6 +22,8 @@ declare module "cz-customizable" { skipQuestions?: string[]; appendBranchNameToCommitMessage?: boolean; ticketNumberPrefix?: string; + ticketNumberSuffix?: string; + ticketNumberPosition?: string; breakingPrefix?: string; footerPrefix?: string; subjectLimit?: number; diff --git a/spec/buildCommitSpec.js b/spec/buildCommitSpec.js index fc21ebd..cfd6871 100644 --- a/spec/buildCommitSpec.js +++ b/spec/buildCommitSpec.js @@ -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'); + }); + }); });