Skip to content

Commit d026ace

Browse files
committed
fix: default value if type-enum abs
1 parent 2800fed commit d026ace

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

src/prompts/type-maker.test.ts

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Level, Rules } from '@commitlint/load';
2+
import { types } from 'conventional-commit-types';
3+
24
import { filterFactory, choicesFactory, validatorFactory, typeMaker } from './type-maker';
35

46
jest.mock('conventional-commit-types');
@@ -36,28 +38,87 @@ describe('type-maker', () => {
3638
});
3739

3840
describe('choicesFactory', () => {
39-
describe('should return undefined if type-enum undefined', () => {
40-
const result = choicesFactory({}, {});
41+
describe('should return commitTypes as choices if type-enum undefined', () => {
42+
const result = choicesFactory({}, types);
4143

42-
expect(result).toBeUndefined();
44+
expect(result).toEqual([
45+
{
46+
name: 'feat: A new feature',
47+
short: 'feat',
48+
value: 'feat'
49+
},
50+
{
51+
name: 'fix: A bug fix',
52+
short: 'fix',
53+
value: 'fix'
54+
},
55+
{
56+
name: 'docs: Documentation only changes',
57+
short: 'docs',
58+
value: 'docs'
59+
},
60+
{
61+
name:
62+
'style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)',
63+
short: 'style',
64+
value: 'style'
65+
},
66+
{
67+
name: 'refactor: A code change that neither fixes a bug nor adds a feature',
68+
short: 'refactor',
69+
value: 'refactor'
70+
},
71+
{
72+
name: 'perf: A code change that improves performance',
73+
short: 'perf',
74+
value: 'perf'
75+
},
76+
{
77+
name: 'test: Adding missing tests or correcting existing tests',
78+
short: 'test',
79+
value: 'test'
80+
},
81+
{
82+
name:
83+
'build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)',
84+
short: 'build',
85+
value: 'build'
86+
},
87+
{
88+
name:
89+
'ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)',
90+
short: 'ci',
91+
value: 'ci'
92+
},
93+
{
94+
name: "chore: Other changes that don't modify src or test files",
95+
short: 'chore',
96+
value: 'chore'
97+
},
98+
{
99+
name: 'revert: Reverts a previous commit',
100+
short: 'revert',
101+
value: 'revert'
102+
}
103+
]);
43104
});
44105

45106
describe('should return choices if type-enum exits', () => {
46107
const result = choicesFactory({ 'type-enum': [Level.Error, 'always', ['foo', 'bar', 'baz']] }, {});
47108

48109
expect(result).toEqual([
49110
{
50-
name: 'foo ',
111+
name: 'foo: ',
51112
short: 'foo',
52113
value: 'foo'
53114
},
54115
{
55-
name: 'bar ',
116+
name: 'bar: ',
56117
short: 'bar',
57118
value: 'bar'
58119
},
59120
{
60-
name: 'baz ',
121+
name: 'baz: ',
61122
short: 'baz',
62123
value: 'baz'
63124
}
@@ -75,17 +136,17 @@ describe('type-maker', () => {
75136

76137
expect(result).toEqual([
77138
{
78-
name: 'foo Fooey',
139+
name: 'foo: Fooey',
79140
short: 'foo',
80141
value: 'foo'
81142
},
82143
{
83-
name: 'bar Barey',
144+
name: 'bar: Barey',
84145
short: 'bar',
85146
value: 'bar'
86147
},
87148
{
88-
name: 'baz Bazey',
149+
name: 'baz: Bazey',
89150
short: 'baz',
90151
value: 'baz'
91152
}
@@ -105,22 +166,22 @@ describe('type-maker', () => {
105166

106167
expect(result).toEqual([
107168
{
108-
name: 'foo Fooey',
169+
name: 'foo : Fooey',
109170
short: 'foo',
110171
value: 'foo'
111172
},
112173
{
113-
name: 'bar Barey',
174+
name: 'bar : Barey',
114175
short: 'bar',
115176
value: 'bar'
116177
},
117178
{
118-
name: 'baz Bazey',
179+
name: 'baz : Bazey',
119180
short: 'baz',
120181
value: 'baz'
121182
},
122183
{
123-
name: 'very-long Longey',
184+
name: 'very-long: Longey',
124185
short: 'very-long',
125186
value: 'very-long'
126187
}

src/prompts/type-maker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,20 @@ export function choicesFactory(rules: Rules, commitTypes: CommitType) {
4949
if (typeEnum && typeEnum.length > 0) {
5050
const longest = getLongest(typeEnum);
5151
choices = typeEnum.map(value => ({
52-
name: `${value.padEnd(longest)} ${commitTypes[value]?.description ?? ''}`,
52+
name: `${value.padEnd(longest)}: ${commitTypes[value]?.description ?? ''}`,
5353
value: value,
5454
short: value
5555
}));
5656
}
5757

58-
return choices;
58+
return (
59+
choices ||
60+
Object.keys(commitTypes).map(commitType => ({
61+
name: `${commitType}: ${commitTypes[commitType].description ?? ''}`,
62+
value: commitType,
63+
short: commitType
64+
}))
65+
);
5966
}
6067

6168
export function typeMaker(questions: Question[], rules: Rules): Question[] {

0 commit comments

Comments
 (0)