Skip to content

Commit

Permalink
feat: make usePreparedCommit default false
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.
  • Loading branch information
leonardoanalista committed Sep 2, 2022
1 parent f137861 commit d9d2322
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
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');
const getPreviousCommit = require('../lib/utils/get-previous-commit');

const commit = jest.fn();

jest.mock('./../lib/read-config');
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');
});
});
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: 90,
functions: 95,
functions: 92,
lines: 88,
statements: 88,
},
Expand Down
16 changes: 9 additions & 7 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 @@ -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;

0 comments on commit d9d2322

Please sign in to comment.