Skip to content

Commit

Permalink
fix: add option usePreparedCommit default false (#196)
Browse files Browse the repository at this point in the history
I am not sure if people use this option but it is likely to be deprecated.

BREAKING CHANGE:
now usePreparedCommit is false. Please update your .cz-config with usePreparedCommit: true if you like to continue using it.
  • Loading branch information
leonardoanalista authored Sep 2, 2022
1 parent 9b82ca8 commit 8a3ef9f
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 157 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Here are the options you can set in your `.cz-config.js`:
* **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.
* **upperCaseSubject**: { boolean, default false }: Capitalizes first subject letter if set to `true`
* **askForBreakingChangeFirst**: { boolean, default false }: It asks for breaking change as first question when set to `true`
* **usePreparedCommit**: { boolean, default false }: It re-uses commit from ./.git/COMMIT_EDITMSG when set to `true`

## Related tools
- (https://github.com/commitizen/cz-cli)
Expand Down
27 changes: 27 additions & 0 deletions __tests__/cz-customizable.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const czModule = require('../index');
const readConfigFile = require('../lib/read-config-file');
const getPreviousCommit = require('../lib/utils/get-previous-commit');

const commit = jest.fn();

jest.mock('./../lib/read-config-file');
jest.mock('./../lib/utils/get-previous-commit');

beforeEach(() => {
const defaultConfig = {
Expand Down Expand Up @@ -347,4 +349,29 @@ describe('cz-customizable', () => {
czModule.prompter(mockCz, commit);
expect(commit).toHaveBeenCalledWith('feat(myScope): TICKET-1234 create a new cool feature');
});

it('should call commit() function with preparedCommit message', () => {
getPreviousCommit.mockReturnValue('fix: a terrible bug');

readConfigFile.mockReturnValue({
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
usePreparedCommit: true,
});

const answers = {
confirmCommit: 'yes',
type: 'feat',
subject: 'create a new cool feature',
};

const mockCz = getMockedCz(answers);
czModule.prompter(mockCz, commit);
expect(commit).toHaveBeenCalledWith('feat: create a new cool feature');
});
});
1 change: 1 addition & 0 deletions cz-config-EXAMPLE.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {

scopes: [{ name: 'accounts' }, { name: 'admin' }, { name: 'exampleScope' }, { name: 'changeMe' }],

usePreparedCommit: false, // to re-use commit from ./.git/COMMIT_EDITMSG
allowTicketNumber: false,
isTicketNumberRequired: false,
ticketNumberPrefix: 'TICKET-',
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 96,
functions: 100,
functions: 95,
lines: 96,
statements: 96,
},
Expand Down
20 changes: 11 additions & 9 deletions lib/questions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs');
const _ = require('lodash');
const buildCommit = require('./build-commit');
const log = require('./logger');
const getPreviousCommit = require('./utils/get-previous-commit');

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

const isValidateTicketNo = (value, config) => {
if (!value) {
Expand All @@ -19,10 +19,12 @@ const isValidateTicketNo = (value, config) => {
return true;
};

const getPreparedCommit = context => {
const getPreparedCommit = (context) => {
let message = null;
if (fs.existsSync('./.git/COMMIT_EDITMSG')) {
let preparedCommit = fs.readFileSync('./.git/COMMIT_EDITMSG', 'utf-8');

let preparedCommit = getPreviousCommit();

if (preparedCommit) {
preparedCommit = preparedCommit
.replace(/^#.*/gm, '')
.replace(/^\s*[\r\n]/gm, '')
Expand Down Expand Up @@ -134,7 +136,7 @@ module.exports = {
type: 'input',
name: 'subject',
message: messages.subject,
default: getPreparedCommit('subject'),
default: config.usePreparedCommit && getPreparedCommit('subject'),
validate(value) {
const limit = config.subjectLimit || 100;
if (value.length > limit) {
Expand All @@ -152,7 +154,7 @@ module.exports = {
type: 'input',
name: 'body',
message: messages.body,
default: getPreparedCommit('body'),
default: config.usePreparedCommit && getPreparedCommit('body'),
},
{
type: 'input',
Expand Down Expand Up @@ -192,10 +194,10 @@ module.exports = {
},
];

questions = questions.filter(item => !skipQuestions.includes(item.name));
questions = questions.filter((item) => !skipQuestions.includes(item.name));

if (config.askForBreakingChangeFirst) {
const isBreaking = oneQuestion => oneQuestion.name === 'breaking';
const isBreaking = (oneQuestion) => oneQuestion.name === 'breaking';

const breakingQuestion = _.filter(questions, isBreaking);
const questionWithoutBreaking = _.reject(questions, isBreaking);
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/get-previous-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs');

const getPreparedCommit = (filePath = './.git/COMMIT_EDITMSG') => {
if (fs.existsSync(filePath)) {
return fs.readFileSync('./.git/COMMIT_EDITMSG', 'utf-8');
}

return null;
};

module.exports = getPreparedCommit;
Loading

0 comments on commit 8a3ef9f

Please sign in to comment.