Skip to content

Commit

Permalink
fix(cli): 🐛 prevent wraping if headerMaxLength is over 72
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-guzman committed Nov 9, 2021
1 parent 614f502 commit 9b1ed6b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
50 changes: 49 additions & 1 deletion src/utils/format-message/formatGitMessage.test.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down Expand Up @@ -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)
})
})
})
10 changes: 7 additions & 3 deletions src/utils/format-message/formatGitMessage.ts
Original file line number Diff line number Diff line change
@@ -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'
)

Expand Down Expand Up @@ -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)
}

0 comments on commit 9b1ed6b

Please sign in to comment.