From d9d2322e5d6980fa7fc9673ef243e4a74b35095d Mon Sep 17 00:00:00 2001 From: Leonardo Correa Date: Fri, 2 Sep 2022 12:07:44 +1000 Subject: [PATCH] feat: make usePreparedCommit default false 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. --- __tests__/cz-customizable.test.js | 27 +++++++++++++++++++++++++++ jest.config.js | 2 +- lib/questions.js | 16 +++++++++------- lib/utils/get-previous-commit.js | 11 +++++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 lib/utils/get-previous-commit.js diff --git a/__tests__/cz-customizable.test.js b/__tests__/cz-customizable.test.js index 4b8d148..cf9bc9d 100644 --- a/__tests__/cz-customizable.test.js +++ b/__tests__/cz-customizable.test.js @@ -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 = { @@ -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'); + }); }); diff --git a/jest.config.js b/jest.config.js index 9b0fe5a..9d5c732 100644 --- a/jest.config.js +++ b/jest.config.js @@ -45,7 +45,7 @@ module.exports = { coverageThreshold: { global: { branches: 90, - functions: 95, + functions: 92, lines: 88, statements: 88, }, diff --git a/lib/questions.js b/lib/questions.js index bb6bcef..9275b1c 100644 --- a/lib/questions.js +++ b/lib/questions.js @@ -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) { @@ -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, '') @@ -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); diff --git a/lib/utils/get-previous-commit.js b/lib/utils/get-previous-commit.js new file mode 100644 index 0000000..fd8071b --- /dev/null +++ b/lib/utils/get-previous-commit.js @@ -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;