Skip to content

Commit 51f79c8

Browse files
committed
feat(cz-git): add defaultType options
it will pin type item the top of the types list (if match item value) link #30
1 parent 86e95a7 commit 51f79c8

File tree

7 files changed

+56
-23
lines changed

7 files changed

+56
-23
lines changed

docs/guide/options-show.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,27 @@ Try running `emoji=1 cz` to **enable output emoji mode in the current session**
104104

105105
---
106106

107+
<br/>
108+
<br/>
109+
<br/>
110+
107111
:::tip
108112
Using ==default value== can produce many ways to make the tool more suitable for you or your team's habits, [⇒ see the recipes](/guide/recipes.html#default)
109113
:::
110114

115+
## defaultType
116+
117+
- **description** : pin type item the top of the types list (match item value)
118+
- **type** : `string`
119+
- **default** : `""`
120+
111121
## defaultScope
112122

113123
- **description** : Whether to use display default value in **custom scope**
114124
- **type** : `string`
115125
- **default** : `""`
116126
- **use** : when you want the default value to appear on the command line just press the "Enter" key
117-
118-
:::tip
119-
If `defaultScope` matches the scope list item `value`.
120-
It will be pinned to the top of scope list.
121-
:::
127+
<Badge type="tip" text="Tip" vertical="middle" /> pin scope item the top of the scope list (match item value)
122128

123129
## defaultSubject
124130

docs/zh/guide/options-show.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,28 @@ export ___X_CMD_THEME_COLOR_CODE="38;5;043"
104104

105105
---
106106

107+
<br/>
108+
<br/>
109+
<br/>
110+
107111
:::tip
108112
使用==默认值==可以产生很多种玩法让工具更契合你或团队的习惯,[⇒ 查看小窍门](/zh/guide/recipes.html#default)
109113
:::
110114

115+
## defaultType
116+
117+
- **描述** : 如果 defaultType 与在选择范围列表项中的 value 相匹配就会进行星标置顶操作。
118+
- **类型** : `string`
119+
- **默认** : `""`
120+
111121
## defaultScope
112122

113123
- **描述** : 在 **自定义范围** 中是否使用显示默认值
114124
- **类型** : `string`
115125
- **默认** : `""`
116126
- **使用** : 当你想要命令行中出现的默认值只需要按下 "Enter" 键即可
117127

118-
:::tip
119-
如果 `defaultScope` 与在选择范围列表项中的 `value` 相匹配就会进行星标置顶操作。
120-
:::
128+
<Badge type="tip" text="提示" vertical="middle" /> 如果 defaultScope 与在选择范围列表项中的 value 相匹配就会进行星标置顶操作。
121129

122130
## defaultSubject
123131

packages/cz-git/src/generator/option.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const generateOptions = (config: UserConfig): CommitizenGitOptions => {
5353
maxHeaderLength: promptConfig.maxHeaderLength ?? getMaxLength(config?.rules?.["header-max-length"] as any),
5454
maxSubjectLength: promptConfig.maxSubjectLength ?? getMaxLength(config?.rules?.["subject-max-length"] as any),
5555
minSubjectLength: promptConfig.minSubjectLength ?? getMinLength(config?.rules?.["subject-min-length"] as any),
56+
defaultType: promptConfig.defaultType ?? defaultConfig.defaultType,
5657
defaultScope: promptConfig.defaultScope ?? defaultConfig.defaultScope,
5758
defaultSubject: promptConfig.defaultSubject ?? defaultConfig.defaultSubject,
5859
defaultBody: promptConfig.defaultBody ?? defaultConfig.defaultBody,

packages/cz-git/src/generator/question.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
import type { Answers, CommitizenGitOptions, Option } from "../shared";
88
import {
9+
isSingleItem,
10+
getCurrentScopes,
911
getProcessSubject,
1012
getMaxSubjectLength,
13+
handlePinListTop,
1114
handleStandardScopes,
1215
handleCustomTemplate,
13-
log,
14-
isSingleItem,
15-
getCurrentScopes
16+
log
1617
} from "../shared";
1718
import { generateMessage } from "./message";
1819
import { fuzzyFilter, style } from "@cz-git/inquirer";
@@ -33,7 +34,10 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
3334
message: options.messages?.type,
3435
themeColorCode: options?.themeColorCode,
3536
source: (_: unknown, input: string) => {
36-
const typeSource = options.types?.concat(options.typesAppend || []) || [];
37+
const typeSource = handlePinListTop(
38+
options.types?.concat(options.typesAppend || []) || [],
39+
options.defaultType
40+
);
3741
return fuzzyFilter(input, typeSource, "value");
3842
}
3943
},

packages/cz-git/src/shared/types/options.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,13 @@ export interface CommitizenGitOptions {
285285
minSubjectLength?: number;
286286

287287
/**
288-
* @description: default value show scope custom prompt
288+
* @description: pin type item the top of the types list (match item value)
289+
*/
290+
defaultType?: string;
291+
292+
/**
293+
* @description: Whether to use display default value in custom scope
294+
* @tip pin scope item the top of the scope list (match item value)
289295
* @example: When you want to use default, just keybord <Enter> it
290296
*/
291297
defaultScope?: string;
@@ -369,8 +375,9 @@ export const defaultConfig = Object.freeze({
369375
minSubjectLength: 0,
370376
scopeOverrides: undefined,
371377
scopeFilters: [".DS_Store"],
372-
defaultBody: "",
378+
defaultType: "",
373379
defaultScope: "",
380+
defaultBody: "",
374381
defaultSubject: "",
375382
defaultFooterPrefix: "",
376383
defaultIssues: ""

packages/cz-git/src/shared/utils/util.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ export const getMaxSubjectLength = (
5454
return countLength(optionMaxLength, typeLength, scopeLength, emojiLength);
5555
};
5656

57+
export const handlePinListTop = (
58+
arr: {
59+
name: string;
60+
value: any;
61+
}[],
62+
defaultValue?: string
63+
) => {
64+
if (!defaultValue || defaultValue === "") return arr;
65+
const index = arr.findIndex((i) => i.value === defaultValue);
66+
if (!~index) return arr;
67+
return [arr[index], ...arr.slice(0, index), ...arr.slice(index + 1)];
68+
};
69+
5770
const filterCustomEmptyByOption = (
5871
target: {
5972
name: string;
@@ -88,14 +101,8 @@ export const handleCustomTemplate = (
88101
if (!Array.isArray(target)) {
89102
return result;
90103
} else if (defaultValue !== "") {
91-
// put the defaultValue to the top
92-
const targetIndex = target.findIndex((i) => i.value === defaultValue);
93-
if (targetIndex !== -1)
94-
target = [
95-
target[targetIndex],
96-
...target.slice(0, targetIndex),
97-
...target.slice(targetIndex + 1)
98-
];
104+
// pin the defaultValue to the top
105+
target = handlePinListTop(target, defaultValue);
99106
}
100107
// prettier-ignore
101108
switch (align) {

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"extends": "./tsconfig.base.json",
33
"references": [
4-
{ "path": "./packages/cz-git/tsconfig.json" },
54
{ "path": "./packages/@cz-git/plugin-loader/tsconfig.json" },
65
{ "path": "./packages/@cz-git/plugin-inquirer/tsconfig.json" },
6+
{ "path": "./packages/cz-git/tsconfig.json" },
77
],
88
"files": []
99
}

0 commit comments

Comments
 (0)