Skip to content

Commit 05ae340

Browse files
committed
feat(cz-git): add auto get issue id
@tip: git branch name = fix_541 => auto get defaultIssues = #541 BREAKING CHANGE : can see the config: https://github.com/Zhengqbbb/cz-git/blob/main/.commitlintrc.js
1 parent 26e7295 commit 05ae340

6 files changed

Lines changed: 68 additions & 27 deletions

File tree

‎.commitlintrc.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const fs = require('fs')
22
const path = require('path')
3-
const packages = fs.readdirSync(path.resolve(__dirname, 'packages/@cz-git'))
3+
const { execSync } = require('child_process');
4+
5+
// @tip: git branch name = fix_541 => auto get defaultIssues = #541
6+
const issue = execSync('git rev-parse --abbrev-ref HEAD')
7+
.toString()
8+
.trim()
9+
.split("_")[1]
10+
const packages = fs.readdirSync(path.resolve(__dirname, 'packages/@cz-git'));
411

512
/** @type {import('cz-git').UserConfig} */
613
module.exports = {
@@ -11,10 +18,11 @@ module.exports = {
1118
"subject-empty": [2, "never"],
1219
},
1320
prompt: {
14-
/** @see: https://github.com/Zhengqbbb/cz-git#options */
21+
// @see: https://github.com/Zhengqbbb/cz-git#options
1522
issuePrefixs: [
16-
{ value: "link", name: "link: Work in processing to ISSUES" },
23+
{ value: "link", name: "link: Work in processing to ISSUES" },
1724
{ value: "closed", name: "closed: ISSUES has been processed" }
18-
]
25+
],
26+
defaultIssues: () => !issue ? "" : `#${issue}`
1927
}
2028
};

‎packages/cz-git/src/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import autocompletePrompt from "inquirer-autocomplete-prompt";
1111
import { commitilintConfigLoader } from "@cz-git/loader";
1212
import { generateOptions, generateQuestions } from "./loader";
1313
import { buildCommit, editCommit, log } from "./until";
14-
import type { CommitizenType } from "./share";
14+
import type { CommitizenType, QualifiedConfig, UserConfig } from "./share";
1515

1616
export * from "./share";
1717

1818
export const prompter = (cz: CommitizenType, commit: (message: string) => void) => {
19-
commitilintConfigLoader().then((clConfig) => {
20-
const options = generateOptions(clConfig);
19+
commitilintConfigLoader().then((clConfig: QualifiedConfig) => {
20+
const options = generateOptions(clConfig as unknown as UserConfig);
2121
const questions = generateQuestions(options, cz);
2222
cz.registerPrompt("autocomplete", autocompletePrompt);
2323
cz.prompt(questions).then((answers) => {

‎packages/cz-git/src/loader.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// @ts-ignore
88
import { commitizenConfigLoader } from "@cz-git/loader";
9-
import { defaultConfig, Option } from "./share";
9+
import { defaultConfig, Option, UserConfig } from "./share";
1010
import {
1111
getMaxLength,
1212
getMinLength,
@@ -17,7 +17,8 @@ import {
1717
buildCommit,
1818
log,
1919
enumRuleIsActive,
20-
getEnumList
20+
getEnumList,
21+
getValueByCallBack
2122
} from "./until";
2223
import type { Answers, Config, CommitizenGitOptions } from "./share";
2324

@@ -38,16 +39,20 @@ const pkgConfig: Config = commitizenConfigLoader() ?? {};
3839

3940
/* eslint-disable prettier/prettier */
4041
/* prettier-ignore */
41-
export const generateOptions = (clConfig: any): CommitizenGitOptions => {
42-
const clPromptConfig = clConfig.prompt ?? {};
42+
export const generateOptions = (clConfig: UserConfig): CommitizenGitOptions => {
43+
let clPromptConfig = clConfig.prompt ?? {};
44+
clPromptConfig = getValueByCallBack(
45+
clPromptConfig,
46+
["defaultScope", "defaultSubject", "defaultBody", "defaultFooterPrefix", "defaultIssues"]
47+
)
4348

4449
return {
4550
messages: pkgConfig.messages ?? clPromptConfig.messages ?? defaultConfig.messages,
4651
types: pkgConfig.types ?? clPromptConfig.types ?? defaultConfig.types,
4752
useEmoji: pkgConfig.useEmoji ?? clPromptConfig.useEmoji ?? defaultConfig.useEmoji,
48-
scopes: pkgConfig.scopes ?? clPromptConfig.scopes ?? getEnumList(clConfig?.rules?.["scope-enum"]),
53+
scopes: pkgConfig.scopes ?? clPromptConfig.scopes ?? getEnumList(clConfig?.rules?.["scope-enum"] as any),
4954
scopeOverrides: pkgConfig.scopeOverrides ?? clPromptConfig.scopeOverrides ?? defaultConfig.scopeOverrides,
50-
allowCustomScopes: pkgConfig.allowCustomScopes ?? clPromptConfig.allowCustomScopes ?? !enumRuleIsActive(clConfig?.rules?.["scope-enum"]),
55+
allowCustomScopes: pkgConfig.allowCustomScopes ?? clPromptConfig.allowCustomScopes ?? !enumRuleIsActive(clConfig?.rules?.["scope-enum"] as any),
5156
allowEmptyScopes: pkgConfig.allowEmptyScopes ?? clPromptConfig.allowEmptyScopes ?? defaultConfig.allowEmptyScopes,
5257
customScopesAlign: pkgConfig.customScopesAlign ?? clPromptConfig.customScopesAlign ?? defaultConfig.customScopesAlign,
5358
customScopesAlias: pkgConfig.customScopesAlias ?? clPromptConfig.customScopesAlias ?? defaultConfig.customScopesAlias,
@@ -61,12 +66,13 @@ export const generateOptions = (clConfig: any): CommitizenGitOptions => {
6166
emptyIssuePrefixsAlias: pkgConfig.emptyIssuePrefixsAlias ?? clPromptConfig.emptyIssuePrefixsAlias ?? defaultConfig.emptyIssuePrefixsAlias,
6267
customIssuePrefixsAlias: pkgConfig.customIssuePrefixsAlias ?? clPromptConfig.customIssuePrefixsAlias ?? defaultConfig.customIssuePrefixsAlias,
6368
confirmColorize: pkgConfig.confirmColorize ?? clPromptConfig.confirmColorize ?? defaultConfig.confirmColorize,
64-
maxHeaderLength: CZ_MAN_HEADER_LENGTH ? parseInt(CZ_MAN_HEADER_LENGTH) : getMaxLength(clConfig?.rules?.["header-max-length"]),
65-
maxSubjectLength: CZ_MAN_SUBJECT_LENGTH ? parseInt(CZ_MAN_SUBJECT_LENGTH) : getMaxLength(clConfig?.rules?.["subject-max-length"]),
66-
minSubjectLength: CZ_MIN_SUBJECT_LENGTH ? parseInt(CZ_MIN_SUBJECT_LENGTH) : getMinLength(clConfig?.rules?.["subject-min-length"]),
69+
maxHeaderLength: CZ_MAN_HEADER_LENGTH ? parseInt(CZ_MAN_HEADER_LENGTH) : getMaxLength(clConfig?.rules?.["header-max-length"] as any),
70+
maxSubjectLength: CZ_MAN_SUBJECT_LENGTH ? parseInt(CZ_MAN_SUBJECT_LENGTH) : getMaxLength(clConfig?.rules?.["subject-max-length"] as any),
71+
minSubjectLength: CZ_MIN_SUBJECT_LENGTH ? parseInt(CZ_MIN_SUBJECT_LENGTH) : getMinLength(clConfig?.rules?.["subject-min-length"] as any),
6772
defaultScope: CZ_SCOPE ?? clPromptConfig.defaultScope ?? defaultConfig.defaultScope,
6873
defaultSubject: CZ_SUBJECT ?? clPromptConfig.defaultSubject ?? defaultConfig.defaultSubject,
6974
defaultBody: CZ_BODY ?? clPromptConfig.defaultBody ?? defaultConfig.defaultBody,
75+
defaultFooterPrefix: clPromptConfig.defaultFooterPrefix ?? defaultConfig.defaultFooterPrefix,
7076
defaultIssues: CZ_ISSUES ?? clPromptConfig.defaultIssues ?? defaultConfig.defaultIssues
7177
}
7278
}
@@ -76,6 +82,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
7682
log("err", "Error [types] Option");
7783
return false;
7884
}
85+
7986
return [
8087
{
8188
type: "autocomplete",
@@ -220,6 +227,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
220227
{
221228
type: "input",
222229
name: "footer",
230+
default: options.defaultIssues || undefined,
223231
when(answers: Answers) {
224232
return (answers.footerPrefix as string | boolean) !== false;
225233
},

‎packages/cz-git/src/share/options.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { QuestionsType } from "../loader";
88
import type { CommitlintUserConfig } from "./commitlint";
99

1010
export interface UserConfig extends CommitlintUserConfig {
11-
prompt: CommitizenGitOptions;
11+
prompt?: CommitizenGitOptions;
1212
}
1313

1414
export type Config = Omit<Partial<typeof defaultConfig>, "scopes"> & {
@@ -21,6 +21,7 @@ export type Config = Omit<Partial<typeof defaultConfig>, "scopes"> & {
2121
defaultScope?: string;
2222
defaultSubject?: string;
2323
defaultBody?: string;
24+
defaultFooterPrefix?: string;
2425
defaultIssues?: string;
2526
};
2627

@@ -86,6 +87,8 @@ export interface Option {
8687
value: string;
8788
}
8889

90+
export type StringCallback = () => string;
91+
8992
export interface TypesOption extends Option {
9093
/**
9194
* @description: Submit emoji commit string
@@ -232,24 +235,29 @@ export interface CommitizenGitOptions {
232235
* @description: default value show scope custom prompt
233236
* @example: When you want to use default, just keybord <Enter> it
234237
*/
235-
defaultScope?: string;
238+
defaultScope?: string | StringCallback;
236239

237240
/**
238241
* @description: default value show subject prompt
239242
* @example: When you want to use default, just keybord <Enter> it
240243
*/
241-
defaultSubject?: string;
244+
defaultSubject?: string | StringCallback;
242245

243246
/**
244247
* @description: default value show body and BREAKINGCHANGES prompt
245248
* @example: When you want to use default, just keybord <Enter> it
246249
*/
247-
defaultBody?: string;
250+
defaultBody?: string | StringCallback;
248251
/**
249252
* @description: default value show issuePrefixs custom prompt
250253
* @example: When you want to use default, just keybord <Enter> it
251254
*/
252-
defaultIssues?: string;
255+
defaultFooterPrefix?: string | StringCallback;
256+
/**
257+
* @description: default value show issue foot prompt
258+
* @example: When you want to use default, just keybord <Enter> it
259+
*/
260+
defaultIssues?: string | StringCallback;
253261
}
254262

255263
/* eslint-disable prettier/prettier */
@@ -301,7 +309,8 @@ export const defaultConfig = Object.freeze({
301309
minSubjectLength: 0,
302310
scopeOverrides: undefined,
303311
defaultBody: "",
304-
defaultIssues: "",
305312
defaultScope: "",
306-
defaultSubject: ""
313+
defaultSubject: "",
314+
defaultFooterPrefix: "",
315+
defaultIssues: ""
307316
} as CommitizenGitOptions);

‎packages/cz-git/src/until/rules.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function ruleIsDisabled(rule: Rule): rule is Readonly<[RuleConfigSeverity
2727
* @example: ruleIsActive([2]) => true
2828
*/
2929
export function ruleIsActive<T extends Rule>(
30-
rule: T
30+
rule: T | undefined
3131
): rule is Exclude<T, Readonly<[RuleConfigSeverity.Disabled]>> {
3232
if (rule && Array.isArray(rule)) {
3333
return rule[0] > RuleConfigSeverity.Disabled;
@@ -64,14 +64,14 @@ export function ruleIsApplicable(
6464
}
6565

6666
export function enumRuleIsActive(
67-
rule: Rule
67+
rule: Rule | undefined
6868
): rule is Readonly<[RuleConfigSeverity.Warning | RuleConfigSeverity.Error, "always", string[]]> {
6969
return (
7070
ruleIsActive(rule) && ruleIsApplicable(rule) && Array.isArray(rule[2]) && rule[2].length > 0
7171
);
7272
}
7373

74-
export function getEnumList(rule: Rule): string[] {
74+
export function getEnumList(rule: Rule | undefined): string[] {
7575
return rule && Array.isArray(rule) && Array.isArray(rule[2]) ? rule[2] : [];
7676
}
7777

‎packages/cz-git/src/until/until.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { wrap } from "./wrap";
88
// @ts-ignore
99

10-
import { Answers, CommitizenGitOptions, Option, ScopesType } from "../share";
10+
import { Answers, CommitizenGitOptions, Option, ScopesType, StringCallback } from "../share";
1111

1212
export function log(type: "info" | "warm" | "err", msg: string) {
1313
const colorMapping = {
@@ -195,3 +195,19 @@ export const buildCommit = (answers: Answers, options: CommitizenGitOptions, col
195195
}
196196
return result;
197197
};
198+
199+
export const getValueByCallBack = (
200+
target: CommitizenGitOptions,
201+
targetKey: Array<
202+
"defaultScope" | "defaultSubject" | "defaultBody" | "defaultFooterPrefix" | "defaultIssues"
203+
>
204+
): CommitizenGitOptions => {
205+
if (targetKey.length === 0) return target;
206+
targetKey.forEach((key) => {
207+
if (!target[key]) return;
208+
if (typeof target[key] === "function" && typeof target[key] !== "string") {
209+
return (target[key] = (target?.[key] as StringCallback)?.call(undefined));
210+
}
211+
});
212+
return target;
213+
};

0 commit comments

Comments
 (0)