Skip to content

Commit 5be93a5

Browse files
committed
feat(cz-git): add align and alias option for selector
BREAKING CHANGE : - `customScopesAlign` and `customIssuePrefixsAlign`: can change align in selet - `emptyScopesAlias`, `customScopesAlias`, `emptyIssuePrefixsAlias`, `customIssuePrefixsAlias`: can change name of the prompt show link #1
1 parent 531c63d commit 5be93a5

4 files changed

Lines changed: 118 additions & 26 deletions

File tree

‎.commitlintrc.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module.exports = {
3535
],
3636
useEmoji: false,
3737
scopes: [{ value: "cz-git", name: "cz-git: core control" }, { name: "guide" } , 'config'],
38+
customScopesAlign: "bottom",
39+
emptyScopesAlias: "empty",
40+
customScopesAlias: "custom",
3841
allowCustomScopes: true,
3942
allowBreakingChanges: ['feat', 'fix'],
4043
breaklineChar: "|",
@@ -43,6 +46,9 @@ module.exports = {
4346
{ value: "link", name: "link: processing to ISSUES" },
4447
{ value: "closed", name: "closed: ISSUES has been processed" }
4548
],
49+
customIssuePrefixsAlign: "top",
50+
emptyIssuePrefixsAlias: "skip",
51+
customIssuePrefixsAlias: "custom",
4652
confirmColorize: true
4753
}
4854
};

‎src/loader.ts‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
getProcessSubject,
1515
getMaxSubjectLength,
1616
handleScopes,
17+
handleCustomTemplate,
1718
buildCommit,
1819
log
1920
} from "./until";
@@ -45,11 +46,17 @@ export const generateOptions = (clConfig: any): CommitizenGitOptions => {
4546
scopes: pkgConfig.scopes ?? clPromptConfig.scopes ?? defaultConfig.scopes,
4647
scopeOverrides: pkgConfig.scopeOverrides ?? clPromptConfig.scopeOverrides ?? defaultConfig.scopeOverrides,
4748
allowCustomScopes: pkgConfig.allowCustomScopes ?? clPromptConfig.allowCustomScopes ?? defaultConfig.allowCustomScopes,
49+
customScopesAlign: pkgConfig.customScopesAlign ?? clPromptConfig.customScopesAlign ?? defaultConfig.customScopesAlign,
50+
customScopesAlias: pkgConfig.customScopesAlias ?? clPromptConfig.customScopesAlias ?? defaultConfig.customScopesAlias,
51+
emptyScopesAlias: pkgConfig.emptyScopesAlias ?? clPromptConfig.emptyScopesAlias ?? defaultConfig.emptyScopesAlias,
4852
upperCaseSubject: pkgConfig.upperCaseSubject ?? clPromptConfig.upperCaseSubject ?? defaultConfig.upperCaseSubject,
4953
allowBreakingChanges: pkgConfig.allowBreakingChanges ?? clPromptConfig.allowBreakingChanges ?? defaultConfig.allowBreakingChanges,
5054
breaklineChar: pkgConfig.breaklineChar ?? clPromptConfig.breaklineChar ?? defaultConfig.breaklineChar,
5155
skipQuestions: pkgConfig.skipQuestions ?? clPromptConfig.skipQuestions ?? defaultConfig.skipQuestions,
5256
issuePrefixs: pkgConfig.issuePrefixs ?? clPromptConfig.issuePrefixs ?? defaultConfig.issuePrefixs,
57+
customIssuePrefixsAlign: pkgConfig.customIssuePrefixsAlign ?? clPromptConfig.customIssuePrefixsAlign ?? defaultConfig.customIssuePrefixsAlign,
58+
emptyIssuePrefixsAlias: pkgConfig.emptyIssuePrefixsAlias ?? clPromptConfig.emptyIssuePrefixsAlias ?? defaultConfig.emptyIssuePrefixsAlias,
59+
customIssuePrefixsAlias: pkgConfig.customIssuePrefixsAlias ?? clPromptConfig.customIssuePrefixsAlias ?? defaultConfig.customIssuePrefixsAlias,
5360
confirmColorize: pkgConfig.confirmColorize ?? clPromptConfig.confirmColorize ?? defaultConfig.confirmColorize,
5461
maxHeaderLength: CZ_MAN_HEADER_LENGTH ? parseInt(CZ_MAN_HEADER_LENGTH) : getMaxLength(clConfig?.rules?.["header-max-length"]),
5562
maxSubjectLength: CZ_MAN_SUBJECT_LENGTH ? parseInt(CZ_MAN_SUBJECT_LENGTH) : getMaxLength(clConfig?.rules?.["subject-max-length"]),
@@ -81,17 +88,17 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
8188
source: (answer: Answers, input: string) => {
8289
let scopes: Option[] = [];
8390
if (options.scopeOverrides && answer.type && options.scopeOverrides[answer.type]) {
84-
scopes = scopes.concat(handleScopes(options.scopeOverrides[answer.type]));
91+
scopes = handleScopes(options.scopeOverrides[answer.type]);
8592
} else if (Array.isArray(options.scopes)) {
86-
scopes = scopes.concat(handleScopes(options.scopes));
87-
}
88-
if (options.allowCustomScopes || scopes.length === 0) {
89-
scopes = scopes.concat([
90-
new cz.Separator(""),
91-
{ value: false, name: "empty" },
92-
{ value: "custom", name: "custom" }
93-
]);
93+
scopes = handleScopes(options.scopes);
9494
}
95+
scopes = handleCustomTemplate(
96+
scopes,
97+
cz,
98+
options.customScopesAlign,
99+
options.emptyScopesAlias,
100+
options.customScopesAlias
101+
);
95102
return scopes?.filter((item) => (input ? item.name?.includes(input) : true)) || true;
96103
}
97104
},
@@ -101,7 +108,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
101108
message: options.messages?.customScope,
102109
default: options.defaultScope || undefined,
103110
when(answers: Answers) {
104-
return answers.scope === "custom";
111+
return answers.scope === "___CUSTOM___";
105112
}
106113
},
107114
{
@@ -186,14 +193,13 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
186193
name: "footerPrefix",
187194
message: options.messages?.footerPrefixsSelect,
188195
source: (_: Answers, input: string) => {
189-
let issues: Array<{ name: string; value: string }> = [
190-
{ name: "skip", value: false },
191-
{ name: "custom", value: "custom" },
192-
new cz.Separator("")
193-
];
194-
if (Array.isArray(options.issuePrefixs)) {
195-
issues = issues.concat(options.issuePrefixs);
196-
}
196+
const issues = handleCustomTemplate(
197+
options.issuePrefixs as Option[],
198+
cz,
199+
options.customIssuePrefixsAlign,
200+
options.emptyIssuePrefixsAlias,
201+
options.customIssuePrefixsAlias
202+
)
197203
return issues?.filter((item) => (input ? item.name?.includes(input) : true)) || true;
198204
}
199205
},
@@ -203,7 +209,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
203209
message: options.messages?.customFooterPrefixs,
204210
default: options.defaultIssues || undefined,
205211
when(answers: Answers) {
206-
return answers.footerPrefix === "custom";
212+
return answers.footerPrefix === "___CUSTOM___";
207213
}
208214
},
209215
{
@@ -227,7 +233,9 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
227233
const SEP = options.confirmColorize
228234
? "\u001B[1;90m###--------------------------------------------------------###\u001B[0m"
229235
: "###--------------------------------------------------------###";
230-
console.info(`\n${SEP}\n${buildCommit(answers, options, options.confirmColorize)}\n${SEP}\n`);
236+
console.info(
237+
`\n${SEP}\n${buildCommit(answers, options, options.confirmColorize)}\n${SEP}\n`
238+
);
231239
return options.messages?.confirmCommit;
232240
}
233241
}

‎src/share.ts‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ export interface CommitizenGitOptions {
129129
*/
130130
allowCustomScopes?: boolean;
131131

132+
/**
133+
* @default: "bottom"
134+
*/
135+
customScopesAlign?: "top" | "bottom" | "top-bottom" | "bottom-top";
136+
137+
/**
138+
* @default: "custom"
139+
*/
140+
customScopesAlias?: string;
141+
142+
/**
143+
* @default: "empty"
144+
*/
145+
emptyScopesAlias?: string;
146+
132147
/**
133148
* @description: Subject is need upper case first.
134149
* @default false
@@ -147,24 +162,39 @@ export interface CommitizenGitOptions {
147162
*/
148163
breaklineChar?: string;
149164

150-
/**
151-
* @description: List of questions you want to skip
152-
* @example: ['body']
153-
*/
154-
skipQuestions?: string[];
155-
156165
/**
157166
* @description: Provides a select issue prefix box in footer
158167
* @default: issuePrefixs: [{ value: "closed", name: "ISSUES has been processed" }]
159168
*/
160169
issuePrefixs?: Option[];
161170

171+
/**
172+
* @default: "top"
173+
*/
174+
customIssuePrefixsAlign?: "top" | "bottom" | "top-bottom" | "bottom-top";
175+
176+
/**
177+
* @default: "skip"
178+
*/
179+
emptyIssuePrefixsAlias?: string;
180+
181+
/**
182+
* @default: "custom"
183+
*/
184+
customIssuePrefixsAlias?: string;
185+
162186
/**
163187
* @description: Prompt final determination whether to display the color
164188
* @default: true
165189
*/
166190
confirmColorize?: boolean;
167191

192+
/**
193+
* @description: List of questions you want to skip
194+
* @example: ['body']
195+
*/
196+
skipQuestions?: string[];
197+
168198
/**
169199
* @description: Force set max header length | Equivalent setting maxSubjectLength.
170200
* @note: It will overWrite commitlint rule set And effect maxSubjectLength
@@ -241,11 +271,17 @@ export const defaultConfig = Object.freeze({
241271
useEmoji: false,
242272
scopes: [],
243273
allowCustomScopes: true,
274+
customScopesAlign: "bottom",
275+
customScopesAlias: "custom",
276+
emptyScopesAlias: "empty",
244277
allowBreakingChanges: ['feat', 'fix'],
245278
upperCaseSubject: false,
246279
breaklineChar: "|",
247280
skipQuestions: [],
248281
issuePrefixs: [{ value: "closed", name: "closed: ISSUES has been processed" }],
282+
customIssuePrefixsAlign: "top",
283+
emptyIssuePrefixsAlias: "skip",
284+
customIssuePrefixsAlias: "custom",
249285
confirmColorize: true,
250286
maxHeaderLength: Infinity,
251287
maxSubjectLength: Infinity,

‎src/until.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,48 @@ export const getMaxSubjectLength = (
169169
);
170170
};
171171

172+
/**
173+
* @description: add separator custom empty
174+
*/
175+
export const handleCustomTemplate = (
176+
target: Array<{ name: string; value: string }>,
177+
cz: any,
178+
align = "top",
179+
emptyAlias = "empty",
180+
customAlias = "custom"
181+
) => {
182+
let result: Array<{ name: string; value: any }> = [
183+
{ name: emptyAlias, value: false },
184+
{ name: customAlias, value: "___CUSTOM___" },
185+
new cz.Separator()
186+
];
187+
if (!Array.isArray(target) || target.length === 0) {
188+
return result;
189+
}
190+
switch (align) {
191+
case "top":
192+
result = result.concat(target);
193+
break;
194+
case "bottom":
195+
result = target.concat(result.reverse());
196+
break;
197+
case "top-bottom":
198+
result = [{ name: emptyAlias, value: false }, new cz.Separator()]
199+
.concat(target)
200+
.concat([new cz.Separator(), { name: customAlias, value: "___CUSTOM___" }]);
201+
break;
202+
case "bottom-top":
203+
result = result = [{ name: customAlias, value: "___CUSTOM___" }, new cz.Separator()]
204+
.concat(target)
205+
.concat([new cz.Separator(), { name: emptyAlias, value: false }]);
206+
break;
207+
default:
208+
result = result.concat(target);
209+
break;
210+
}
211+
return result;
212+
};
213+
172214
/**
173215
* @description: handle scope configuration option into standard options
174216
* @param {ScopesType}

0 commit comments

Comments
 (0)