Skip to content

Commit 242e887

Browse files
authored
fix(cz-git): trim colorized scope and subject (#253)
1 parent 771fdb2 commit 242e887

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎packages/cz-git/__tests__/generateMessage.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { describe, expect, it } from 'vitest'
22
import { generateMessage } from '../src/generator'
33
import type { CommitizenGitOptions } from '../src/shared'
44

5+
function stripAnsi(value: string) {
6+
const ansiCodes = [
7+
'\u001B[32m',
8+
'\u001B[33m',
9+
'\u001B[36m',
10+
'\u001B[39m',
11+
'\u001B[0m',
12+
]
13+
return ansiCodes.reduce((result, code) => result.split(code).join(''), value)
14+
}
15+
516
/**
617
* @description generateMessage Test
718
*/
@@ -53,6 +64,17 @@ describe('generateMessage()', () => {
5364
}
5465
expect(generateMessage(answers, options)).toEqual('feat: add a new feature')
5566
})
67+
68+
it('colorized custom scope should trim spaces before output', () => {
69+
const options = {}
70+
const answers = {
71+
type: 'feat',
72+
scope: '___CUSTOM___',
73+
customScope: 'app ',
74+
subject: 'add a new feature',
75+
}
76+
expect(stripAnsi(generateMessage(answers, options, true))).toEqual('feat(app): add a new feature')
77+
})
5678
})
5779

5880
describe('subject', () => {
@@ -98,6 +120,16 @@ describe('generateMessage()', () => {
98120
}
99121
expect(generateMessage(answers, options)).toEqual('feat(app): add a new feature :sparkles:')
100122
})
123+
124+
it('colorized subject should trim spaces before output', () => {
125+
const options = {}
126+
const answers = {
127+
type: 'feat',
128+
scope: 'app',
129+
subject: 'add a new feature ',
130+
}
131+
expect(stripAnsi(generateMessage(answers, options, true))).toEqual('feat(app): add a new feature')
132+
})
101133
})
102134

103135
describe('body', () => {

‎packages/cz-git/src/generator/message.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ function addType(type: string, colorize?: boolean) {
6868
function addScope(scope?: string, colorize?: boolean) {
6969
if (!scope)
7070
return ''
71-
scope = colorize ? style.yellow(scope) : scope
72-
return scope?.trim()
71+
const trimmedScope = scope.trim()
72+
if (!trimmedScope)
73+
return ''
74+
return colorize ? style.yellow(trimmedScope) : trimmedScope
7375
}
7476

7577
/** @add if markBreaking or flag add mark "!" */
@@ -104,10 +106,13 @@ function addEmoji(emojiCode: string, tempAlign: string, emojiAlign?: string) {
104106
function addSubject(subject?: string, colorize?: boolean, themeColorCode?: string) {
105107
if (!subject)
106108
return ''
109+
const trimmedSubject = subject.trim()
110+
if (!trimmedSubject)
111+
return ''
107112
if (!colorize)
108-
return subject.trim()
113+
return trimmedSubject
109114
else
110-
return useThemeCode(subject, themeColorCode).trim()
115+
return useThemeCode(trimmedSubject, themeColorCode)
111116
}
112117

113118
/** @add if footer or colorize return trimed footer */

0 commit comments

Comments
 (0)