Skip to content

Commit

Permalink
feat(cz-git): add allowEmptyScopes, allowCustomScopes to control disable
Browse files Browse the repository at this point in the history
- feat: allowCustomScopes and allowCustomScopes control disable
- feat: scopes can auto load value
from `commitlint`

link #1
  • Loading branch information
Zhengqbbb committed Feb 24, 2022
1 parent be839b3 commit e0becb6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
24 changes: 15 additions & 9 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @description: generate commitizen config option(generateOptions) | generate commitizen questions(generateQuestions)
* @author: @Zhengqbbb (zhengqbbb@gmail.com)
* @license: MIT
* TODO: add custom skip option to higher custom
* TODO: add more test to protect code
*/

// @ts-ignore
Expand All @@ -13,10 +13,12 @@ import {
getMinLength,
getProcessSubject,
getMaxSubjectLength,
handleScopes,
handleStandardScopes,
handleCustomTemplate,
buildCommit,
log
log,
enumRuleIsActive,
getEnumList
} from "./until";
import type { Answers, Config, CommitizenGitOptions } from "./share";

Expand All @@ -39,13 +41,15 @@ const pkgConfig: Config = configLoader.load() ?? {};
/* prettier-ignore */
export const generateOptions = (clConfig: any): CommitizenGitOptions => {
const clPromptConfig = clConfig.prompt ?? {};

return {
messages: pkgConfig.messages ?? clPromptConfig.messages ?? defaultConfig.messages,
types: pkgConfig.types ?? clPromptConfig.types ?? defaultConfig.types,
useEmoji: pkgConfig.useEmoji ?? clPromptConfig.useEmoji ?? defaultConfig.useEmoji,
scopes: pkgConfig.scopes ?? clPromptConfig.scopes ?? defaultConfig.scopes,
scopes: pkgConfig.scopes ?? clPromptConfig.scopes ?? getEnumList(clConfig?.rules?.["scope-enum"]),
scopeOverrides: pkgConfig.scopeOverrides ?? clPromptConfig.scopeOverrides ?? defaultConfig.scopeOverrides,
allowCustomScopes: pkgConfig.allowCustomScopes ?? clPromptConfig.allowCustomScopes ?? defaultConfig.allowCustomScopes,
allowCustomScopes: pkgConfig.allowCustomScopes ?? clPromptConfig.allowCustomScopes ?? !enumRuleIsActive(clConfig?.rules?.["scope-enum"]),
allowEmptyScopes: pkgConfig.allowEmptyScopes ?? clPromptConfig.allowEmptyScopes ?? defaultConfig.allowEmptyScopes,
customScopesAlign: pkgConfig.customScopesAlign ?? clPromptConfig.customScopesAlign ?? defaultConfig.customScopesAlign,
customScopesAlias: pkgConfig.customScopesAlias ?? clPromptConfig.customScopesAlias ?? defaultConfig.customScopesAlias,
emptyScopesAlias: pkgConfig.emptyScopesAlias ?? clPromptConfig.emptyScopesAlias ?? defaultConfig.emptyScopesAlias,
Expand Down Expand Up @@ -88,16 +92,18 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
source: (answer: Answers, input: string) => {
let scopes: Option[] = [];
if (options.scopeOverrides && answer.type && options.scopeOverrides[answer.type]) {
scopes = handleScopes(options.scopeOverrides[answer.type]);
scopes = handleStandardScopes(options.scopeOverrides[answer.type]);
} else if (Array.isArray(options.scopes)) {
scopes = handleScopes(options.scopes);
scopes = handleStandardScopes(options.scopes);
}
scopes = handleCustomTemplate(
scopes,
cz,
options.customScopesAlign,
options.emptyScopesAlias,
options.customScopesAlias
options.customScopesAlias,
options.allowCustomScopes,
options.allowEmptyScopes
);
return scopes?.filter((item) => (input ? item.name?.includes(input) : true)) || true;
}
Expand Down Expand Up @@ -239,7 +245,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
return options.messages?.confirmCommit;
}
}
].filter((i) => !options.skipQuestions?.includes(i.name));
].filter((i) => !options.skipQuestions?.includes(i.name as "scope" | "body" | "breaking" | "footer" | "footerPrefix"));
};

type GenerateQuestionsType = typeof generateQuestions;
Expand Down
25 changes: 19 additions & 6 deletions src/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,32 @@ export interface CommitizenGitOptions {

/**
* @description: Provides a select of prompt to select module scopes
* @note: it auto import value from rule "scope-enum" with `@commitlint`
* @use: want to add scopes description or when you not use commitlint
*/
scopes?: ScopesType;

/**
* @description: Provides an overriding select of prompt to select module scopes under specific typs
* @note: use this option should set `scopes` option to realize distinguish
* @example: [test] => provide select e2eTest unitTest
*/
scopeOverrides?: { [type: string]: ScopesType };

/**
* @description: Whether to not selectable skipping when appearing or customizing when selecting scopes
* @description: Whether to show customizing when selecting scopes
* @note: it auto check rule "scope-enum" set the option with `@commitlint`
* @use: when you not use commitlint
* @default true
*/
allowCustomScopes?: boolean;

/**
* @description: Whether to show empty when selecting scopes
* @default true
*/
allowEmptyScopes?: boolean;

/**
* @default: "bottom"
*/
Expand Down Expand Up @@ -191,27 +202,28 @@ export interface CommitizenGitOptions {

/**
* @description: List of questions you want to skip
* @default: []
* @example: ['body']
*/
skipQuestions?: string[];
skipQuestions?: Array<"scope" | "body" | "breaking" | "footerPrefix" | "footer">;

/**
* @description: Force set max header length | Equivalent setting maxSubjectLength.
* @note: It will overWrite commitlint rule set And effect maxSubjectLength
* @note: it auto check rule "header-max-length" set the option with `@commitlint`.
* @use: when you not use commitlint
*/
maxHeaderLength?: number;

/**
* @description: Force set max subject length.
* @note: It will overWrite commitlint rule set
* @note: it auto check rule "subject-max-length" set the option with `@commitlint`.
* @use: when you not use commitlint
*/
maxSubjectLength?: number;

/**
* @description: Force set header width.
* @note: It will overWrite commitlint rule set
* @note: it auto check rule "subject-min-length" set the option with `@commitlint`.
* @use: when you not use commitlint
*/
minSubjectLength?: number;
Expand Down Expand Up @@ -249,7 +261,7 @@ export const defaultConfig = Object.freeze({
customScope: "Denote the SCOPE of this change:",
subject: "Write a SHORT, IMPERATIVE tense description of the change:\n",
body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
breaking: "List any BREAKING CHANGES (optional):\n",
breaking: 'List any BREAKING CHANGES (optional). Use "|" to break new line:\n',
footerPrefixsSelect: "Select the ISSUES type of changeList by this change (optional):",
customFooterPrefixs: "Input ISSUES Prefix:",
footer: "List any ISSUES by this change. E.g.: #31, #34, #I972S:\n",
Expand All @@ -271,6 +283,7 @@ export const defaultConfig = Object.freeze({
useEmoji: false,
scopes: [],
allowCustomScopes: true,
allowEmptyScopes: true,
customScopesAlign: "bottom",
customScopesAlias: "custom",
emptyScopesAlias: "empty",
Expand Down
27 changes: 22 additions & 5 deletions src/until.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function enumRuleIsActive(
}

export function getEnumList(rule: Rule): string[] {
return Array.isArray(rule[2]) ? rule[2] : [];
return rule && Array.isArray(rule) && Array.isArray(rule[2]) ? rule[2] : [];
}

/**
Expand Down Expand Up @@ -169,6 +169,21 @@ export const getMaxSubjectLength = (
);
};

const filterCustomEmptyByOption = (
target: {
name: string;
value: any;
}[],
allowCustom = true,
allowEmpty = true
) => {
if (!Array.isArray(target) || target.length === 3 || target.length === 4) {
return allowCustom ? target : target.filter((i) => i.value !== "___CUSTOM___");
}
target = allowCustom ? target : target.filter((i) => i.value !== "___CUSTOM___");
return allowEmpty ? target : target.filter((i) => i.value !== false);
};

/**
* @description: add separator custom empty
*/
Expand All @@ -177,14 +192,16 @@ export const handleCustomTemplate = (
cz: any,
align = "top",
emptyAlias = "empty",
customAlias = "custom"
customAlias = "custom",
allowCustom = true,
allowEmpty = true
) => {
let result: Array<{ name: string; value: any }> = [
{ name: emptyAlias, value: false },
{ name: customAlias, value: "___CUSTOM___" },
new cz.Separator()
];
if (!Array.isArray(target) || target.length === 0) {
if (!Array.isArray(target)) {
return result;
}
switch (align) {
Expand All @@ -208,15 +225,15 @@ export const handleCustomTemplate = (
result = result.concat(target);
break;
}
return result;
return filterCustomEmptyByOption(result, allowCustom, allowEmpty);
};

/**
* @description: handle scope configuration option into standard options
* @param {ScopesType}
* @returns {Option[]}
*/
export const handleScopes = (scopes: ScopesType): Option[] => {
export const handleStandardScopes = (scopes: ScopesType): Option[] => {
return scopes.map((scope) => {
return typeof scope === "string"
? { name: scope, value: scope }
Expand Down

0 comments on commit e0becb6

Please sign in to comment.