Skip to content

Commit 980f163

Browse files
committed
feat(cz-git): scopes config support value to add descrition for selector
BREAKING CHANGE : e.g: { value: "cz-git", name: "cz-git: core control" } link #1
1 parent 2ebd7fe commit 980f163

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎src/loader.ts‎

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

77
// @ts-ignore
88
import { configLoader } from "commitizen";
9-
import { defaultConfig } from "./share";
9+
import { defaultConfig, Option } from "./share";
1010
import {
1111
getMaxLength,
1212
getMinLength,
1313
getProcessSubject,
1414
getMaxSubjectLength,
15+
handleScopes,
1516
buildCommit,
1617
log
1718
} from "./until";
@@ -75,20 +76,20 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
7576
name: "scope",
7677
message: options.messages?.scope,
7778
source: (answer: Answers, input: string) => {
78-
let scopes: Array<{ name: string }> = [];
79+
let scopes: Option[] = [];
7980
if (options.scopeOverrides && answer.type && options.scopeOverrides[answer.type]) {
80-
scopes = scopes.concat(options.scopeOverrides[answer.type]);
81+
scopes = scopes.concat(handleScopes(options.scopeOverrides[answer.type]));
8182
} else if (Array.isArray(options.scopes)) {
82-
scopes = scopes.concat(options.scopes);
83+
scopes = scopes.concat(handleScopes(options.scopes));
8384
}
8485
if (options.allowCustomScopes || scopes.length === 0) {
8586
// TODO: add align option
8687
scopes = scopes.concat([
8788
// TODO: option
8889
new cz.Separator(""),
8990
// TODO: option
90-
{ name: "empty", value: false },
91-
{ name: "custom", value: "custom" }
91+
{ value: false, name: "empty" },
92+
{ value: "custom", name: "custom" }
9293
]);
9394
}
9495
return scopes?.filter((item) => (input ? item.name?.includes(input) : true)) || true;
@@ -203,7 +204,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
203204
default: options.defaultIssues || undefined,
204205
when(answers: Answers) {
205206
return answers.footerPrefixsSelect === "custom";
206-
},
207+
}
207208
},
208209
{
209210
type: "input",

‎src/share.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface UserConfig extends CommitlintUserConfig {
1212
}
1313

1414
export type Config = Omit<Partial<typeof defaultConfig>, "scopes"> & {
15-
scopes: TypesOption[];
15+
scopes: ScopesType;
1616
disableScopeLowerCase?: boolean;
1717
disableSubjectLowerCase?: boolean;
1818
maxHeaderWidth?: number;
@@ -66,12 +66,14 @@ export type Answers = {
6666
confirmCommit?: string;
6767
};
6868

69+
export type ScopesType = string[] | Array<{ name: string; value?: string }>;
70+
6971
export interface CommitizenType {
7072
registerPrompt: (type: string, plugin: unknown) => void;
7173
prompt: (qs: QuestionsType) => Promise<Answers>;
7274
}
7375

74-
interface Option {
76+
export interface Option {
7577
/**
7678
* @description: show prompt name
7779
*/
@@ -111,13 +113,13 @@ export interface CommitizenGitOptions {
111113
/**
112114
* @description: Provides a select of prompt to select module scopes
113115
*/
114-
scopes?: Array<{ name: string }>;
116+
scopes?: ScopesType;
115117

116118
/**
117119
* @description: Provides an overriding select of prompt to select module scopes under specific typs
118120
* @example: [test] => provide select e2eTest unitTest
119121
*/
120-
scopeOverrides?: { [type: string]: Array<{ name: string }> };
122+
scopeOverrides?: { [type: string]: ScopesType };
121123

122124
/**
123125
* @description: Whether to not selectable skipping when appearing or customizing when selecting scopes
@@ -222,7 +224,7 @@ export const defaultConfig = Object.freeze({
222224
{ value: "revert", name: "revert: Reverts a previous commit", emoji: ":rewind:" }
223225
],
224226
useEmoji: false,
225-
scopes: [{ name: 'READEME' }, { name: 'theme' }, { name: 'module' }, { name: 'plugin' }],
227+
scopes: [],
226228
allowCustomScopes: true,
227229
allowBreakingChanges: ['feat', 'fix'],
228230
upperCaseSubject: false,

‎src/until.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import wrap from "word-wrap";
88
// @ts-ignore
99
import editor from "editor";
1010
import { open as tempOpen } from "temp";
11-
import { Answers, CommitizenGitOptions } from "./share";
11+
import { Answers, CommitizenGitOptions, Option, ScopesType } from "./share";
1212

1313
export type Rule =
1414
| Readonly<[RuleConfigSeverity.Disabled]>
@@ -114,6 +114,21 @@ export const getMaxSubjectLength = (
114114
);
115115
};
116116

117+
/**
118+
* @description: handle scope configuration option into standard options
119+
* @param {ScopesType}
120+
* @returns {Option[]}
121+
*/
122+
export const handleScopes = (scopes: ScopesType): Option[] => {
123+
return scopes.map((scope) => {
124+
return typeof scope === "string"
125+
? { name: scope, value: scope }
126+
: !scope.value
127+
? { value: scope.name, ...scope }
128+
: { value: scope.value, name: scope.name };
129+
});
130+
};
131+
117132
const addType = (type: string, options: CommitizenGitOptions, color?: boolean) => {
118133
type = color ? `\u001B[33m${type}\u001B[0m` : type;
119134
if (options.useEmoji) {

0 commit comments

Comments
 (0)