Skip to content

Commit

Permalink
feat(cz-git): add multiple scopes mode
Browse files Browse the repository at this point in the history
need turn on option: `enableMultipleScopes`

link #14
  • Loading branch information
Zhengqbbb committed May 11, 2022
1 parent 3eaab45 commit 89470d9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
13 changes: 11 additions & 2 deletions packages/cz-git/src/generator/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ const getSingleParams = (answers: Answers, options: CommitizenGitOptions) => {
mapping.singleScope = scopeList[singleIndex].value;
}
// eslint-disable-next-line prettier/prettier
if (isSingleItem(options.allowCustomIssuePrefixs, options.allowEmptyIssuePrefixs, options.issuePrefixs)) {
if (
isSingleItem(
options.allowCustomIssuePrefixs,
options.allowEmptyIssuePrefixs,
options.issuePrefixs
)
) {
mapping.singeIssuePrefix = options.issuePrefixs?.[singleIndex].value || "";
}
return mapping;
Expand Down Expand Up @@ -80,9 +86,12 @@ export const generateMessage = (
width: options.breaklineNumber
};
const { singleScope, singeIssuePrefix } = getSingleParams(answers, options);
const scope = Array.isArray(answers.scope)
? answers.scope.join(options.scopeEnumSeparator)
: answers.scope;
const head =
addType(answers.type ?? "", colorize) +
addScope(singleScope || answers.scope, colorize) +
addScope(singleScope || scope, colorize) +
addEmoji(answers.type ?? "", options) +
addSubject(answers.subject, colorize);
const body = wrap(answers.body ?? "", wrapOptions);
Expand Down
4 changes: 3 additions & 1 deletion packages/cz-git/src/generator/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export const generateOptions = (clConfig: UserConfig): CommitizenGitOptions => {
useEmoji: pkgConfig.useEmoji ?? clPromptConfig.useEmoji ?? defaultConfig.useEmoji,
scopes: pkgConfig.scopes ?? clPromptConfig.scopes ?? getEnumList(clConfig?.rules?.["scope-enum"] as any),
scopeOverrides: pkgConfig.scopeOverrides ?? clPromptConfig.scopeOverrides ?? defaultConfig.scopeOverrides,
enableMultipleScopes: pkgConfig.enableMultipleScopes ?? clPromptConfig.enableMultipleScopes ?? defaultConfig.enableMultipleScopes,
scopeEnumSeparator: pkgConfig.scopeEnumSeparator ?? clPromptConfig.scopeEnumSeparator ?? defaultConfig.scopeEnumSeparator,
allowCustomScopes: pkgConfig.allowCustomScopes ?? clPromptConfig.allowCustomScopes ?? !enumRuleIsActive(clConfig?.rules?.["scope-enum"] as any),
allowEmptyScopes: pkgConfig.allowEmptyScopes ?? clPromptConfig.allowEmptyScopes ?? !emptyRuleIsActive(clConfig?.rules?.["scope-empty"] as any),
customScopesAlign: pkgConfig.customScopesAlign ?? clPromptConfig.customScopesAlign ?? defaultConfig.customScopesAlign,
Expand Down Expand Up @@ -81,4 +83,4 @@ export const generateOptions = (clConfig: UserConfig): CommitizenGitOptions => {
defaultFooterPrefix: clPromptConfig.defaultFooterPrefix ?? defaultConfig.defaultFooterPrefix,
defaultIssues: CZ_ISSUES ?? clPromptConfig.defaultIssues ?? defaultConfig.defaultIssues
}
}
}
3 changes: 2 additions & 1 deletion packages/cz-git/src/generator/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
}
},
{
type: "autocomplete",
type: options.enableMultipleScopes ? "search-checkbox" : "autocomplete",
name: "scope",
message: options.messages?.scope,
separator: options.scopeEnumSeparator,
source: (answer: Answers, input: string) => {
let scopeSource: Option[] = [];
scopeSource = handleStandardScopes(
Expand Down
4 changes: 3 additions & 1 deletion packages/cz-git/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* TODO: add more test to protect code
*/

import { SearchCheckbox } from "@cz-git/inquirer";
import { commitilintConfigLoader } from "@cz-git/loader";
// @ts-ignore
import autocompletePrompt from "inquirer-autocomplete-prompt";
import { commitilintConfigLoader } from "@cz-git/loader";
import { generateOptions, generateQuestions, generateMessage } from "./generator";
import { editCommit, log } from "./shared";
import type { CommitizenType, QualifiedConfig, UserConfig } from "./shared/types";
Expand All @@ -20,6 +21,7 @@ export const prompter = (cz: CommitizenType, commit: (message: string) => void)
const options = generateOptions(clConfig as unknown as UserConfig);
const questions = generateQuestions(options, cz);
cz.registerPrompt("autocomplete", autocompletePrompt);
cz.registerPrompt("search-checkbox", SearchCheckbox);
cz.prompt(questions).then((answers) => {
switch (answers.confirmCommit) {
case "edit":
Expand Down
16 changes: 15 additions & 1 deletion packages/cz-git/src/shared/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type Answers = {
/**
* @default: Denote the SCOPE of this change (optional):
*/
scope?: string;
scope?: string | string[];
/**
* @default: Denote the SCOPE of this change:
*/
Expand Down Expand Up @@ -137,6 +137,18 @@ export interface CommitizenGitOptions {
*/
scopeOverrides?: { [type: string]: ScopesType };

/**
* @description: Whether to enable scope multiple mode
* @default: false
*/
enableMultipleScopes?: boolean;

/**
* @description: Multiple choice scope separator
* @default: ","
*/
scopeEnumSeparator?: string;

/**
* @description: Whether to show "custom" when selecting scopes
* @note it auto check rule "scope-enum" set the option with `@commitlint`
Expand Down Expand Up @@ -319,6 +331,8 @@ export const defaultConfig = Object.freeze({
typesAppend: [],
useEmoji: false,
scopes: [],
enableMultipleScopes: false,
scopeEnumSeparator: ",",
allowCustomScopes: true,
allowEmptyScopes: true,
customScopesAlign: "bottom",
Expand Down
1 change: 1 addition & 0 deletions packages/cz-git/src/shared/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const getMaxSubjectLength = (
options: CommitizenGitOptions
) => {
let optionMaxLength = Infinity;
if (Array.isArray(scope)) scope = scope.join(options.scopeEnumSeparator);
const typeLength = type?.length ? type.length : 0;
const scopeLength = scope ? scope.length + 2 : 0;
const emojiLength = options.useEmoji ? getEmojiStrLength(options, type) : 0;
Expand Down

0 comments on commit 89470d9

Please sign in to comment.