Skip to content

Commit 2444724

Browse files
committed
feat(cz-git): support both head and subject max rules
link #1
1 parent ad7c16c commit 2444724

5 files changed

Lines changed: 79 additions & 19 deletions

File tree

‎.commitlintrc.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
extends: ["@commitlint/config-conventional"],
44
rules: {
55
"scope-enum": [2, "always", [ "cz-git", "guide", "config" ]],
6-
"subject-max-length": [2, 'always', 100],
6+
"header-max-length": [2, 'always', 100],
77
"subject-empty": [2, "never"],
88
},
99
prompt: {
@@ -32,7 +32,7 @@ module.exports = {
3232
{ value: "chore", name: "chore: Other changes that don't modify src or test files", emoji: ":hammer:" },
3333
{ value: "revert", name: "revert: Reverts a previous commit", emoji: ":rewind:" }
3434
],
35-
useEmoji: true,
35+
useEmoji: false,
3636
scopes: [{ value: "cz-git", name: "cz-git: core control" }, { name: "guide" } , 'config'],
3737
allowCustomScopes: true,
3838
allowBreakingChanges: ['feat', 'fix'],

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"repository": {
2020
"type": "git",
21-
"url": "git+https://github.com/Zhengqbbb/cz-git.git"
21+
"url": "https://github.com/Zhengqbbb/cz-git"
2222
},
2323
"license": "MIT",
2424
"author": "Zhengqbbb <zhengqbbb@gmail.com> (https://github.com/Zhengqbbb)",

‎src/loader.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* @description: generate commitizen config option(generateOptions) | generate commitizen questions(generateQuestions)
33
* @author: @Zhengqbbb (zhengqbbb@gmail.com)
44
* @license: MIT
5+
* TODO: fix getMaxSubjectLength add header max length
6+
* TODO: add custom skip option to higher custom
7+
* TODO: add breaklineChar option
8+
* TODO: add end prompt color option
59
*/
610

711
// @ts-ignore
@@ -27,6 +31,7 @@ const {
2731
CZ_SUBJECT,
2832
CZ_BODY,
2933
CZ_ISSUES,
34+
CZ_MAN_HEADER_LENGTH,
3035
CZ_MAN_SUBJECT_LENGTH,
3136
CZ_MIN_SUBJECT_LENGTH,
3237
} = process.env;
@@ -49,6 +54,7 @@ export const generateOptions = (clConfig: any): CommitizenGitOptions => {
4954
skipQuestions: pkgConfig.skipQuestions ?? clPromptConfig.skipQuestions ?? defaultConfig.skipQuestions,
5055
issuePrefixs: pkgConfig.issuePrefixs ?? clPromptConfig.issuePrefixs ?? defaultConfig.issuePrefixs,
5156
confirmNoColor: pkgConfig.confirmNoColor ?? clPromptConfig.confirmNoColor ?? defaultConfig.confirmNoColor,
57+
maxHeaderLength: CZ_MAN_HEADER_LENGTH ? parseInt(CZ_MAN_HEADER_LENGTH) : getMaxLength(clConfig?.rules?.["header-max-length"]),
5258
maxSubjectLength: CZ_MAN_SUBJECT_LENGTH ? parseInt(CZ_MAN_SUBJECT_LENGTH) : getMaxLength(clConfig?.rules?.["subject-max-length"]),
5359
minSubjectLength: CZ_MIN_SUBJECT_LENGTH ? parseInt(CZ_MIN_SUBJECT_LENGTH) : getMinLength(clConfig?.rules?.["subject-min-length"]),
5460
defaultScope: CZ_SCOPE ?? clPromptConfig.defaultScope ?? defaultConfig.defaultScope,
@@ -83,11 +89,8 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
8389
scopes = scopes.concat(handleScopes(options.scopes));
8490
}
8591
if (options.allowCustomScopes || scopes.length === 0) {
86-
// TODO: add align option
8792
scopes = scopes.concat([
88-
// TODO: option
8993
new cz.Separator(""),
90-
// TODO: option
9194
{ value: false, name: "empty" },
9295
{ value: "custom", name: "custom" }
9396
]);
@@ -224,7 +227,6 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
224227
],
225228
default: 0,
226229
message(answers: Answers) {
227-
// TODO: COLOR
228230
const SEP =
229231
"\u001B[1;90m###--------------------------------------------------------###\u001B[0m";
230232
console.info(`\n${SEP}\n${buildCommit(answers, options, true)}\n${SEP}\n`);

‎src/share.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ export type Config = Omit<Partial<typeof defaultConfig>, "scopes"> & {
1515
scopes: ScopesType;
1616
disableScopeLowerCase?: boolean;
1717
disableSubjectLowerCase?: boolean;
18-
maxHeaderWidth?: number;
19-
maxLineWidth?: number;
18+
maxHeaderLength?: number;
19+
maxSubjectLength?: number;
20+
minSubjectLength?: number;
2021
defaultScope?: string;
2122
defaultSubject?: string;
2223
defaultBody?: string;
@@ -157,6 +158,13 @@ export interface CommitizenGitOptions {
157158
*/
158159
confirmNoColor?: boolean;
159160

161+
/**
162+
* @description: Force set max header length | Equivalent setting maxSubjectLength.
163+
* @note: It will overWrite commitlint rule set And effect maxSubjectLength
164+
* @use: when you not use commitlint
165+
*/
166+
maxHeaderLength?: number;
167+
160168
/**
161169
* @description: Force set max subject length.
162170
* @note: It will overWrite commitlint rule set
@@ -231,6 +239,7 @@ export const defaultConfig = Object.freeze({
231239
skipQuestions: [],
232240
issuePrefixs: [{ value: "closed", name: "closed: ISSUES has been processed" }],
233241
confirmNoColor: false,
242+
maxHeaderLength: Infinity,
234243
maxSubjectLength: Infinity,
235244
minSubjectLength: 0,
236245
scopeOverrides: undefined,

‎src/until.ts‎

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,21 @@ export type Rule =
1616
| Readonly<[RuleConfigSeverity, RuleConfigCondition, unknown]>;
1717

1818
/**
19-
* Check if a rule definition is active
20-
* @param rule to check
21-
* @return if the rule definition is active
19+
* @description: rule is Disabled
20+
* @example: ruleIsDisabled([0]) => true
21+
* @example: ruleIsDisabled([2]) => false
22+
*/
23+
export function ruleIsDisabled(rule: Rule): rule is Readonly<[RuleConfigSeverity.Disabled]> {
24+
if (rule && Array.isArray(rule) && rule[0] === RuleConfigSeverity.Disabled) {
25+
return true;
26+
}
27+
return false;
28+
}
29+
30+
/**
31+
* @description: rule is use
32+
* @example: ruleIsActive([0]) => false
33+
* @example: ruleIsActive([2]) => true
2234
*/
2335
export function ruleIsActive<T extends Rule>(
2436
rule: T
@@ -30,9 +42,21 @@ export function ruleIsActive<T extends Rule>(
3042
}
3143

3244
/**
33-
* Check if a rule definition is applicable
34-
* @param rule to check
35-
* @return if the rule definition is applicable
45+
* @description: rule is can ignore
46+
*/
47+
export function ruleIsNotApplicable(
48+
rule: Rule
49+
): rule is
50+
| Readonly<[RuleConfigSeverity, "never"]>
51+
| Readonly<[RuleConfigSeverity, "never", unknown]> {
52+
if (rule && Array.isArray(rule)) {
53+
return rule[1] === "never";
54+
}
55+
return false;
56+
}
57+
58+
/**
59+
* @description: rule is effect
3660
*/
3761
export function ruleIsApplicable(
3862
rule: Rule
@@ -45,8 +69,20 @@ export function ruleIsApplicable(
4569
return false;
4670
}
4771

72+
export function enumRuleIsActive(
73+
rule: Rule
74+
): rule is Readonly<[RuleConfigSeverity.Warning | RuleConfigSeverity.Error, "always", string[]]> {
75+
return (
76+
ruleIsActive(rule) && ruleIsApplicable(rule) && Array.isArray(rule[2]) && rule[2].length > 0
77+
);
78+
}
79+
80+
export function getEnumList(rule: Rule): string[] {
81+
return Array.isArray(rule[2]) ? rule[2] : [];
82+
}
83+
4884
/**
49-
* @example: getMaxLength(rules['max-line-length'])
85+
* @example: getMaxLength(rules['max-header-length'] => 100)
5086
*/
5187
export function getMaxLength(rule?: Rule): number {
5288
if (rule && ruleIsActive(rule) && ruleIsApplicable(rule) && typeof rule[2] === "number") {
@@ -55,6 +91,9 @@ export function getMaxLength(rule?: Rule): number {
5591
return Infinity;
5692
}
5793

94+
/**
95+
* @example: getMinLength(rules['min-header-length'] => 2)
96+
*/
5897
export function getMinLength(rule?: Rule): number {
5998
if (rule && ruleIsActive(rule) && ruleIsApplicable(rule) && typeof rule[2] === "number") {
6099
return rule[2];
@@ -109,13 +148,23 @@ export const getMaxSubjectLength = (
109148
scope: Answers["scope"],
110149
options: CommitizenGitOptions
111150
) => {
151+
let optionMaxLength = Infinity;
152+
const maxHeaderLength = options?.maxHeaderLength ? options?.maxHeaderLength : Infinity;
153+
const maxSubjectLength = options?.maxSubjectLength ? options?.maxSubjectLength : Infinity;
154+
if (options?.maxHeaderLength === 0 || options?.maxSubjectLength === 0) {
155+
return 0;
156+
} else if (maxHeaderLength === Infinity) {
157+
optionMaxLength = maxSubjectLength !== Infinity ? maxSubjectLength : Infinity;
158+
} else {
159+
optionMaxLength = maxHeaderLength < maxSubjectLength ? maxHeaderLength : maxSubjectLength;
160+
}
112161
return (
113-
(options?.maxSubjectLength ? options?.maxSubjectLength : 100) -
162+
optionMaxLength -
114163
(type?.length ? type.length : 0) -
115-
// `: `
116-
2 -
117164
// `()`
118165
(scope ? scope.length + 2 : 0) -
166+
// `: `
167+
2 -
119168
(options.useEmoji ? getEmojiStrLength(options, type) : 0)
120169
);
121170
};

0 commit comments

Comments
 (0)