From 9b1ed6b384cb67ffcb0de0b779ded868a83f1c3a Mon Sep 17 00:00:00 2001 From: jimmy-guzman Date: Fri, 17 Sep 2021 22:06:37 -0500 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=F0=9F=90=9B=20prevent=20wraping=20?= =?UTF-8?q?if=20`headerMaxLength`=20is=20over=20`72`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../format-message/formatGitMessage.test.ts | 50 ++++++++++++++++++- src/utils/format-message/formatGitMessage.ts | 10 ++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/utils/format-message/formatGitMessage.test.ts b/src/utils/format-message/formatGitMessage.test.ts index d4ffba60..e68eeaf0 100644 --- a/src/utils/format-message/formatGitMessage.test.ts +++ b/src/utils/format-message/formatGitMessage.test.ts @@ -1,5 +1,5 @@ /* eslint-disable jest/no-large-snapshots */ -import { formatCommitMessage } from './formatGitMessage' +import { formatCommitMessage, wrap } from './formatGitMessage' import { defaultConfig } from '../../defaults' const setupFormatCommitMessage = (config = {}, answers = {}): string => { @@ -163,4 +163,52 @@ describe('formatCommitMessage', () => { `"feat(*): ✨ this has \\\\\`quotes\\\\\`"` ) }) + it('should not wrap message when headerMaxLength is longer than the default width (72)', () => { + const formattedMessage = setupFormatCommitMessage( + { ...defaultConfig, headerMaxLength: 75 }, + { + body: '', + breaking: '', + issues: '', + subject: + 'this is a very very very very very very very very very subject', + } + ) + + expect(formattedMessage).toMatchInlineSnapshot( + `"feat(*): ✨ this is a very very very very very very very very very subject"` + ) + expect(formattedMessage).toHaveLength(73) + }) + it('should leverage default width (72) when headerMaxLength is less', () => { + const formattedMessage = setupFormatCommitMessage( + { ...defaultConfig, headerMaxLength: 71 }, + { + body: '', + breaking: '', + issues: '', + subject: + 'this is a very very very very very very very very very subject', // This is intentionally longer than headerMaxLength + } + ) + + expect(formattedMessage).toMatchInlineSnapshot(` +"feat(*): ✨ this is a very very very very very very very very very +subject" +`) + expect(formattedMessage.split('\n')[0]).toHaveLength(65) + }) + describe('wrap', () => { + it('should wrap', () => { + const wrappedString = wrap( + `feat(*): ✨ this is a very very very very very very very very very subject` + ) + + expect(wrappedString).toMatchInlineSnapshot(` +"feat(*): ✨ this is a very very very very very very very very very +subject" +`) + expect(wrappedString.split('\n')[0]).toHaveLength(65) + }) + }) }) diff --git a/src/utils/format-message/formatGitMessage.ts b/src/utils/format-message/formatGitMessage.ts index 2ae80ac6..d15b5638 100644 --- a/src/utils/format-message/formatGitMessage.ts +++ b/src/utils/format-message/formatGitMessage.ts @@ -1,8 +1,10 @@ import { GitzyConfig, Answers } from '../../interfaces' -const wrap = (string: string, width = 72): string => { +const MAX_WIDTH = 72 + +export const wrap = (string: string, maxWidth = MAX_WIDTH): string => { const regex = new RegExp( - `(?![^\\n]{1,${width}}$)([^\\n]{1,${width}})\\s`, + `(?![^\\n]{1,${maxWidth}}$)([^\\n]{1,${maxWidth}})\\s`, 'g' ) @@ -50,6 +52,8 @@ export const formatCommitMessage = ( const body = answers.body.trim() ? `\n\n${answers.body}` : '' const breaking = createBreaking(answers.breaking, config) const issues = createIssues(answers.issues, config) + const maxWidth = + config.headerMaxLength > MAX_WIDTH ? config.headerMaxLength : MAX_WIDTH - return wrap(normalizeMessage(`${head}${body}${breaking}${issues}`)) + return wrap(normalizeMessage(`${head}${body}${breaking}${issues}`), maxWidth) }