Skip to content

Commit

Permalink
feat(cz-git): add alias option can directly submit the defined comm…
Browse files Browse the repository at this point in the history
…it message

link #43
  • Loading branch information
Zhengqbbb committed Jun 30, 2022
1 parent 3372e37 commit 692a582
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 64 deletions.
9 changes: 9 additions & 0 deletions packages/cz-git/src/generator/message.ts
Expand Up @@ -8,6 +8,15 @@ import { style } from "@cz-git/inquirer";
import { getCurrentScopes, handleStandardScopes, isSingleItem, wrap } from "../shared";
import type { Answers, CommitizenGitOptions } from "../shared";

export const getAliasMessage = (config: CommitizenGitOptions, alias?: string) => {
if (!alias || typeof config?.alias?.[alias] !== "string") {
throw new Error(`${style.red(`>>> The alias "${alias}" is not defined`)}`);
}
console.log(`${style.green(`>>> Using "${alias}" commit message alias:`)}`);
console.log(`${style.gray(config.alias[alias])}`);
return config.alias[alias];
};

const getSingleParams = (answers: Answers, options: CommitizenGitOptions) => {
const singleIndex = 0;
const mapping = {
Expand Down
1 change: 1 addition & 0 deletions packages/cz-git/src/generator/option.ts
Expand Up @@ -21,6 +21,7 @@ export const generateOptions = (config: UserConfig): CommitizenGitOptions => {
const promptConfig = config.prompt ?? {};

return {
alias: promptConfig.alias ?? defaultConfig.alias,
messages: promptConfig.messages ?? defaultConfig.messages,
themeColorCode: ___X_CMD_THEME_COLOR_CODE || promptConfig.themeColorCode || defaultConfig.themeColorCode,
types: promptConfig.types ?? defaultConfig.types,
Expand Down
8 changes: 7 additions & 1 deletion packages/cz-git/src/index.ts
Expand Up @@ -8,7 +8,7 @@
import { SearchList, SearchCheckbox, CompleteInput } from "@cz-git/inquirer";
import { configLoader } from "@cz-git/loader";
import { editCommit, log } from "./shared";
import { generateOptions, generateQuestions, generateMessage } from "./generator";
import { generateOptions, generateQuestions, generateMessage, getAliasMessage } from "./generator";
import type { CommitizenType } from "./shared";

export * from "./shared/types";
Expand All @@ -22,6 +22,12 @@ export const prompter = (
) => {
configLoader({ configPath }).then((config) => {
const options = generateOptions(config);

if ("cz_alias" in process.env) {
commit(getAliasMessage(options, process.env["cz_alias"]));
return;
}

const questions = generateQuestions(options, cz);
cz.registerPrompt("search-list", SearchList);
cz.registerPrompt("search-checkbox", SearchCheckbox);
Expand Down
136 changes: 73 additions & 63 deletions packages/cz-git/src/shared/types/options.ts
Expand Up @@ -102,6 +102,15 @@ export interface TypesOption extends Option {
}

export interface CommitizenGitOptions {
/**
* @description: define commonly used commit message alias
* @default: { fd: "docs: fix typos" }
* @use commitizen CLI: "cz_alias=fd cz"
* @use czg CLI: "czg --alias=fd" | "czg :fd"
* @note use commitizen CLI will meet process not exit. cz-git can't resolve it.
*/
alias?: Record<string, string>;

/**
* @description: Customize prompt questions
*/
Expand Down Expand Up @@ -345,66 +354,67 @@ export interface CommitizenGitOptions {
/* eslint-disable prettier/prettier */
/* prettier-ignore */
export const defaultConfig = Object.freeze({
messages: {
type: "Select the type of change that you're committing:",
scope: "Denote the SCOPE of this change (optional):",
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',
markBreaking: 'Is any BREAKING CHANGE (add "!" in header) (optional) ?',
breaking: 'List any BREAKING CHANGES (optional). Use "|" to break new line:\n',
footerPrefixsSelect: "Select the ISSUES type of change (optional):",
customFooterPrefixs: "Input ISSUES prefix:",
footer: "List any ISSUES AFFECTED by this change. E.g.: #31, #34:\n",
confirmCommit: "Are you sure you want to proceed with the commit above?"
},
types: [
{ value: "feat", name: "feat: A new feature", emoji: ":sparkles:" },
{ value: "fix", name: "fix: A bug fix", emoji: ":bug:" },
{ value: "docs", name: "docs: Documentation only changes", emoji: ":memo:" },
{ value: "style", name: "style: Changes that do not affect the meaning of the code", emoji: ":lipstick:" },
{ value: "refactor", name: "refactor: A code change that neither fixes a bug nor adds a feature", emoji: ":recycle:" },
{ value: "perf", name: "perf: A code change that improves performance", emoji: ":zap:" },
{ value: "test", name: "test: Adding missing tests or correcting existing tests", emoji: ":white_check_mark:" },
{ value: "build", name: "build: Changes that affect the build system or external dependencies", emoji: ":package:" },
{ value: "ci", name: "ci: Changes to our CI configuration files and scripts", emoji: ":ferris_wheel:" },
{ value: "chore", name: "chore: Other changes that don't modify src or test files", emoji: ":hammer:" },
{ value: "revert", name: "revert: Reverts a previous commit", emoji: ":rewind:" }
],
typesAppend: [],
themeColorCode: "",
useEmoji: false,
emojiAlign: "center",
scopes: [],
enableMultipleScopes: false,
scopeEnumSeparator: ",",
allowCustomScopes: true,
allowEmptyScopes: true,
customScopesAlign: "bottom",
customScopesAlias: "custom",
emptyScopesAlias: "empty",
upperCaseSubject: false,
markBreakingChangeMode: false,
allowBreakingChanges: ['feat', 'fix'],
breaklineNumber: 100,
breaklineChar: "|",
skipQuestions: [],
issuePrefixs: [{ value: "closed", name: "closed: ISSUES has been processed" }],
customIssuePrefixsAlign: "top",
emptyIssuePrefixsAlias: "skip",
customIssuePrefixsAlias: "custom",
allowCustomIssuePrefixs: true,
allowEmptyIssuePrefixs: true,
confirmColorize: true,
maxHeaderLength: Infinity,
maxSubjectLength: Infinity,
minSubjectLength: 0,
scopeOverrides: undefined,
scopeFilters: [".DS_Store"],
defaultType: "",
defaultScope: "",
defaultBody: "",
defaultSubject: "",
defaultFooterPrefix: "",
defaultIssues: ""
} as CommitizenGitOptions);
alias: { fd: "docs: fix typos" },
messages: {
type: "Select the type of change that you're committing:",
scope: "Denote the SCOPE of this change (optional):",
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',
markBreaking: 'Is any BREAKING CHANGE (add "!" in header) (optional) ?',
breaking: 'List any BREAKING CHANGES (optional). Use "|" to break new line:\n',
footerPrefixsSelect: "Select the ISSUES type of change (optional):",
customFooterPrefixs: "Input ISSUES prefix:",
footer: "List any ISSUES AFFECTED by this change. E.g.: #31, #34:\n",
confirmCommit: "Are you sure you want to proceed with the commit above?"
},
types: [
{ value: "feat", name: "feat: A new feature", emoji: ":sparkles:" },
{ value: "fix", name: "fix: A bug fix", emoji: ":bug:" },
{ value: "docs", name: "docs: Documentation only changes", emoji: ":memo:" },
{ value: "style", name: "style: Changes that do not affect the meaning of the code", emoji: ":lipstick:" },
{ value: "refactor", name: "refactor: A code change that neither fixes a bug nor adds a feature", emoji: ":recycle:" },
{ value: "perf", name: "perf: A code change that improves performance", emoji: ":zap:" },
{ value: "test", name: "test: Adding missing tests or correcting existing tests", emoji: ":white_check_mark:" },
{ value: "build", name: "build: Changes that affect the build system or external dependencies", emoji: ":package:" },
{ value: "ci", name: "ci: Changes to our CI configuration files and scripts", emoji: ":ferris_wheel:" },
{ value: "chore", name: "chore: Other changes that don't modify src or test files", emoji: ":hammer:" },
{ value: "revert", name: "revert: Reverts a previous commit", emoji: ":rewind:" }
],
typesAppend: [],
themeColorCode: "",
useEmoji: false,
emojiAlign: "center",
scopes: [],
enableMultipleScopes: false,
scopeEnumSeparator: ",",
allowCustomScopes: true,
allowEmptyScopes: true,
customScopesAlign: "bottom",
customScopesAlias: "custom",
emptyScopesAlias: "empty",
upperCaseSubject: false,
markBreakingChangeMode: false,
allowBreakingChanges: ['feat', 'fix'],
breaklineNumber: 100,
breaklineChar: "|",
skipQuestions: [],
issuePrefixs: [{ value: "closed", name: "closed: ISSUES has been processed" }],
customIssuePrefixsAlign: "top",
emptyIssuePrefixsAlias: "skip",
customIssuePrefixsAlias: "custom",
allowCustomIssuePrefixs: true,
allowEmptyIssuePrefixs: true,
confirmColorize: true,
maxHeaderLength: Infinity,
maxSubjectLength: Infinity,
minSubjectLength: 0,
scopeOverrides: undefined,
scopeFilters: [".DS_Store"],
defaultType: "",
defaultScope: "",
defaultBody: "",
defaultSubject: "",
defaultFooterPrefix: "",
defaultIssues: ""
} as CommitizenGitOptions);

0 comments on commit 692a582

Please sign in to comment.