From a7182017cc8f53ba65554d0fa6f893e1e5e523fc Mon Sep 17 00:00:00 2001 From: Ian Egner Date: Thu, 1 Oct 2020 15:31:02 +0100 Subject: [PATCH] fix: only use prepared commit message if run as hook --- index.js | 6 ++++-- questions.js | 6 +++--- spec/questionsSpec.js | 19 ++++++++++--------- standalone.js | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index d00e06b..e7239f6 100755 --- a/index.js +++ b/index.js @@ -46,12 +46,14 @@ const readConfigFile = () => { }; module.exports = { - prompter(cz, commit) { + prompter(cz, commit, isStandalone = false) { const config = readConfigFile(); config.subjectLimit = config.subjectLimit || 100; log.info('All lines except first will be wrapped after 100 characters.'); - const questions = require('./questions').getQuestions(config, cz); + const isHook = process.argv.includes('--hook'); + + const questions = require('./questions').getQuestions(config, cz, isStandalone || isHook); cz.prompt(questions).then(answers => { if (answers.confirmCommit === 'edit') { diff --git a/questions.js b/questions.js index b0f0fb6..609ec07 100644 --- a/questions.js +++ b/questions.js @@ -41,7 +41,7 @@ const getPreparedCommit = context => { }; module.exports = { - getQuestions(config, cz) { + getQuestions(config, cz, isStandaloneOrHook = false) { // normalize config optional options const scopeOverrides = config.scopeOverrides || {}; const messages = config.messages || {}; @@ -132,7 +132,7 @@ module.exports = { type: 'input', name: 'subject', message: messages.subject, - default: getPreparedCommit('subject'), + default: isStandaloneOrHook ? getPreparedCommit('subject') : null, validate(value) { const limit = config.subjectLimit || 100; if (value.length > limit) { @@ -150,7 +150,7 @@ module.exports = { type: 'input', name: 'body', message: messages.body, - default: getPreparedCommit('body'), + default: isStandaloneOrHook ? getPreparedCommit('body') : null, }, { type: 'input', diff --git a/spec/questionsSpec.js b/spec/questionsSpec.js index 2b1b5b9..430e4a3 100644 --- a/spec/questionsSpec.js +++ b/spec/questionsSpec.js @@ -13,7 +13,8 @@ describe('cz-customizable', () => { Separator: jasmine.createSpy(), }; - const getQuestion = number => questions.getQuestions(config, mockedCz)[number - 1]; + const getQuestion = (number, isStandaloneOrHook = false) => + questions.getQuestions(config, mockedCz, isStandaloneOrHook)[number - 1]; it('should array of questions be returned', () => { config = { @@ -277,29 +278,29 @@ describe('cz-customizable', () => { it('should ignore if there is no prepared commit file', () => { existsSync.andReturn(false); - expect(getQuestion(5).default).toEqual(null); - expect(getQuestion(6).default).toEqual(null); + expect(getQuestion(5, true).default).toEqual(null); + expect(getQuestion(6, true).default).toEqual(null); }); it('should ignore an empty prepared commit', () => { existsSync.andReturn(true); readFileSync.andReturn(''); - expect(getQuestion(5).default).toEqual(null); - expect(getQuestion(6).default).toEqual(null); + expect(getQuestion(5, true).default).toEqual(null); + expect(getQuestion(6, true).default).toEqual(null); }); it('should take a single line commit as the subject', () => { existsSync.andReturn(true); readFileSync.andReturn('my commit'); - expect(getQuestion(5).default).toEqual('my commit'); - expect(getQuestion(6).default).toEqual(null); + expect(getQuestion(5, true).default).toEqual('my commit'); + expect(getQuestion(6, true).default).toEqual(null); }); it('should split multi line commit between the subject and the body', () => { existsSync.andReturn(true); readFileSync.andReturn('my commit\nmessage\n\nis on several lines'); - expect(getQuestion(5).default).toEqual('my commit'); - expect(getQuestion(6).default).toEqual(`message|is on several lines`); + expect(getQuestion(5, true).default).toEqual('my commit'); + expect(getQuestion(6, true).default).toEqual(`message|is on several lines`); }); }); }); diff --git a/standalone.js b/standalone.js index dd18564..024bd48 100755 --- a/standalone.js +++ b/standalone.js @@ -16,4 +16,4 @@ const commit = commitMessage => { } }; -app.prompter(inquirer, commit); +app.prompter(inquirer, commit, true);