Skip to content

Commit

Permalink
feat: add options subjectSeparator typePrefix and typeSuffix (leoforf…
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoanalista authored Apr 4, 2019
1 parent 48dc07d commit 3a4b4d0
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ Hopefully this will help you to have consistent commit messages and have a fully

Here are the options you can set in your `.cz-config.js`:

* **subjectLimit**: {number, default 100}: This is the commit first line. Example: `feat: this is a new feature` or `feat(scopePayments): this is a new feature`
* **subjectSeparator**: {string, default ': '}: This is the subject separator. Example: `feat: this is a new feature`
* **typePrefix**: {string, default ''}: This is the commit type prefix. Example: config: `{ typePrefix: '[' }`, result: `[feat: this is a new feature`
* **typeSuffix**: {string, default ''}: This is the commit type suffix. Example: config: `{ typePrefix: '[', typeSuffix: ']', subjectSeparator: ' ' }`, result: `[feat] this is a new feature`

* **scopes**: {Array of Strings}: Specify the scopes for your particular project. Eg.: for some banking system: ["acccounts", "payments"]. For another travelling application: ["bookings", "search", "profile"]
* **scopeOverrides**: {Object where key contains a Array of String}: Use this when you want to override scopes for a specific commit type. Example bellow specify scopes when type is `fix`:
```
Expand All @@ -78,7 +83,6 @@ Here are the options you can set in your `.cz-config.js`:
]
}
```
* **subjectLimit**: {number, default 100}: This is the commit first line.
* **allowCustomScopes**: {boolean, default false}: adds the option `custom` to scope selection so you can still type a scope if you need.
* **allowBreakingChanges**: {Array of Strings: default none}. List of commit types you would like to the question `breaking change` prompted. Eg.: ['feat', 'fix'].
* **skipQuestions**: {Array of Strings: default none}. List of questions you want to skip. Eg.: ['body', 'footer'].
Expand Down
43 changes: 27 additions & 16 deletions buildCommit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const _ = require('lodash');
const wrap = require('word-wrap');

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

function addTicketNumber(ticketNumber, config) {
if (!ticketNumber) {
return '';
Expand All @@ -10,26 +14,33 @@ function addTicketNumber(ticketNumber, config) {
return `${ticketNumber.trim()} `;
}

module.exports = function buildCommit(answers, config) {
const maxLineWidth = 100;
function 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);
}

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

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

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

function addScope(scope) {
if (!scope) return ': '; // it could be type === WIP. So there is no scope

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

function addSubject(subject) {
return subject.trim();
}

function escapeSpecialChars(result) {
// eslint-disable-next-line no-useless-escape
const specialChars = ['`'];
Expand All @@ -47,11 +58,11 @@ module.exports = function buildCommit(answers, config) {
// Hard limit this line
// eslint-disable-next-line max-len
const head = (
answers.type +
addScope(answers.scope) +
addType(answers.type, config) +
addScope(answers.scope, config) +
addTicketNumber(answers.ticketNumber, config) +
addSubject(answers.subject)
).slice(0, maxLineWidth);
).slice(0, defaultMaxLineWidth);

// Wrap these lines at 100 characters
let body = wrap(answers.body, wrapOptions) || '';
Expand Down
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"editor": "1.0.0",
"find-config": "^1.0.0",
"inquirer": "^6.2.2",
"lodash": "^4.17.11",
"temp": "^0.9.0",
"word-wrap": "^1.2.3"
},
Expand Down
65 changes: 65 additions & 0 deletions spec/buildCommitSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const buildCommit = require('../buildCommit');

describe('buildCommit()', () => {
const answers = {
type: 'feat',
scope: 'app',
subject: 'this is a new feature',
};

it('subject with default subject separator', () => {
const options = {};
expect(buildCommit(answers, options)).toEqual('feat(app): this is a new feature');
});

it('subject with custom subject separator option', () => {
const options = {
subjectSeparator: ' - ',
};
expect(buildCommit(answers, options)).toEqual('feat(app) - this is a new feature');
});

it('subject 1 empty character separator', () => {
const options = {
subjectSeparator: ' ',
};
expect(buildCommit(answers, options)).toEqual('feat(app) this is a new feature');
});

describe('without scope', () => {
it('subject without scope', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
};
const options = {};
expect(buildCommit(answersNoScope, options)).toEqual('feat: this is a new feature');
});

it('subject without scope', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
};
const options = {
subjectSeparator: ' - ',
};
expect(buildCommit(answersNoScope, options)).toEqual('feat - this is a new feature');
});
});

describe('type prefix and type suffix', () => {
it('subject with both', () => {
const answersNoScope = {
type: 'feat',
subject: 'this is a new feature',
};
const options = {
typePrefix: '[',
typeSuffix: ']',
subjectSeparator: ' ',
};
expect(buildCommit(answersNoScope, options)).toEqual('[feat] this is a new feature');
});
});
});

0 comments on commit 3a4b4d0

Please sign in to comment.