Skip to content

Commit

Permalink
feat(cz-git): support single item intelligent filter ouput of scope a…
Browse files Browse the repository at this point in the history
…nd issueprefix

if scope list or issueprefix list only one item
And set allowCustom and allowEmpty is false
will filter question and finally will output the item.

link #12
  • Loading branch information
Zhengqbbb committed Apr 26, 2022
1 parent 65a2086 commit 73f688c
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { buildCommit } from "../lib/shared";
import { generateMessage } from "../lib/generator";

/**
* @description: buildCommit Test
* @description: generateMessage Test
*/
describe("buildCommit()", () => {
describe("generateMessage()", () => {
const answers = {
type: "feat",
scope: "app",
Expand All @@ -12,23 +12,23 @@ describe("buildCommit()", () => {

test("subject with default subject should be standard separator", () => {
const options = {};
expect(buildCommit(answers, options)).toEqual("feat(app): add a new feature");
expect(generateMessage(answers, options)).toEqual("feat(app): add a new feature");
});

test("subject with emoji options should be standard separator", () => {
const options = {
types: [{ value: "feat", name: "feat: A new feature", emoji: ":sparkles:" }],
useEmoji: true
};
expect(buildCommit(answers, options)).toEqual("feat(app): :sparkles: add a new feature");
expect(generateMessage(answers, options)).toEqual("feat(app): :sparkles: add a new feature");
});

test("subject with emoji options and lost type should be standard separator", () => {
const options = {
types: [{ value: "feat", name: "feat: A new feature" }],
useEmoji: true
};
expect(buildCommit(answers, options)).toEqual("feat(app): add a new feature");
expect(generateMessage(answers, options)).toEqual("feat(app): add a new feature");
});

test("body breaking line should be with breaklineNumber", () => {
Expand All @@ -42,7 +42,7 @@ describe("buildCommit()", () => {
subject: "add a new feature",
body: "test breaklineNumber test breaklineNumber"
};
expect(buildCommit(answers, options)).toEqual(
expect(generateMessage(answers, options)).toEqual(
`feat(app): add a new feature
test breaklineNumber
Expand Down
105 changes: 105 additions & 0 deletions packages/cz-git/src/generator/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* @description: generate commit message(generateMessage)
* @author: @Zhengqbbb (zhengqbbb@gmail.com)
* @license: MIT
*/

import {
Answers,
CommitizenGitOptions,
getCurrentScopes,
handleStandardScopes,
isSingleItem
} from "../shared";
import { wrap } from "../shared";

const getSingleParams = (answers: Answers, options: CommitizenGitOptions) => {
const singleIndex = 0;
const mapping = {
singleScope: "",
singeIssuePrefix: ""
};
const scopeList = handleStandardScopes(
getCurrentScopes(options.scopes, options.scopeOverrides, answers.type)
);
if (isSingleItem(options.allowCustomScopes, options.allowCustomIssuePrefixs, scopeList)) {
mapping.singleScope = scopeList[singleIndex].value;
}
// eslint-disable-next-line prettier/prettier
if (isSingleItem(options.allowCustomIssuePrefixs, options.allowEmptyIssuePrefixs, options.issuePrefixs)) {
mapping.singeIssuePrefix = options.issuePrefixs?.[singleIndex].value || "";
}
return mapping;
};

const addType = (type: string, colorize?: boolean) =>
colorize ? `\u001B[32m${type}\u001B[0m` : type;

const addScope = (scope?: string, colorize?: boolean) => {
const separator = ":";
if (!scope) return separator;
scope = colorize ? `\u001B[33m${scope}\u001B[0m` : scope;
return `(${scope.trim()})${separator}`;
};

const addEmoji = (type: string, options: CommitizenGitOptions): string => {
if (options.useEmoji && type !== "") {
const itemSource = options.types?.concat(options.typesAppend || []) || [];
const item = itemSource.find((i) => i.value === type);
return item?.emoji ? ` ${item.emoji} ` : " ";
} else {
return " ";
}
};

const addSubject = (subject?: string, colorize?: boolean) => {
if (!subject) return "";
subject = colorize ? `\u001B[36m${subject}\u001B[0m` : subject;
return subject.trim();
};

const addBreaklinesIfNeeded = (value: string, breaklineChar = "|") =>
value.split(breaklineChar).join("\n").valueOf();

const addFooter = (footer: string, footerPrefix = "", colorize?: boolean) => {
if (footerPrefix === "") {
return colorize ? `\n\n\u001B[32m${footer}\u001B[0m` : `\n\n${footer}`;
}
return colorize
? `\n\n\u001B[32m${footerPrefix} ${footer}\u001B[0m`
: `\n\n${footerPrefix} ${footer}`;
};

export const generateMessage = (
answers: Answers,
options: CommitizenGitOptions,
colorize = false
) => {
const wrapOptions = {
trim: true,
newLine: "\n",
indent: "",
width: options.breaklineNumber
};
const { singleScope, singeIssuePrefix } = getSingleParams(answers, options);
const head =
addType(answers.type ?? "", colorize) +
addScope(singleScope || answers.scope, colorize) +
addEmoji(answers.type ?? "", options) +
addSubject(answers.subject, colorize);
const body = wrap(answers.body ?? "", wrapOptions);
const breaking = wrap(answers.breaking ?? "", wrapOptions);
const footer = wrap(answers.footer ?? "", wrapOptions);

let result = head;
if (body) {
result += `\n\n${addBreaklinesIfNeeded(body, options.breaklineChar)}`;
}
if (breaking) {
result += `\n\nBREAKING CHANGE :\n${addBreaklinesIfNeeded(breaking, options.breaklineChar)}`;
}
if (footer) {
result += addFooter(footer, answers.footerPrefix || singeIssuePrefix, colorize);
}
return result;
};
1 change: 1 addition & 0 deletions packages/cz-git/src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./build";
export * from "./option";
export * from "./question";
6 changes: 0 additions & 6 deletions packages/cz-git/src/generator/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
* @license: MIT
*/

/**
* @description: generate commitizen config option(generateOptions) | generate commitizen questions(generateQuestions)
* @author: @Zhengqbbb (zhengqbbb@gmail.com)
* @license: MIT
*/

import { commitizenConfigLoader } from "@cz-git/loader";
import type { Config, CommitizenGitOptions, UserConfig } from "../shared";
import {
Expand Down
26 changes: 16 additions & 10 deletions packages/cz-git/src/generator/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
getMaxSubjectLength,
handleStandardScopes,
handleCustomTemplate,
buildCommit,
log,
isSingleItem,
getCurrentScopes
} from "../shared";
import { generateMessage } from "./build";

export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
if (!Array.isArray(options.types) || options.types.length === 0) {
Expand Down Expand Up @@ -53,7 +53,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
);
return scopes?.filter((item) => (input ? item.name?.includes(input) : true)) || true;
},
when(answer: Answers) {
when: (answer: Answers) => {
return !isSingleItem(
options.allowCustomScopes,
options.allowEmptyScopes,
Expand All @@ -68,19 +68,19 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
name: "scope",
message: options.messages?.customScope,
default: options.defaultScope || undefined,
validate(input: string) {
validate: (input: string) => {
if (options.allowEmptyScopes) return true;
return input.length ? true : "\u001B[1;31m[ERROR] scope is required\u001B[0m";
},
when(answers: Answers) {
when: (answers: Answers) => {
return answers.scope === "___CUSTOM___";
}
},
{
type: "input",
name: "subject",
message: options.messages?.subject,
validate(subject: string, answers: Answers) {
validate: (subject: string, answers: Answers) => {
const processedSubject = getProcessSubject(subject);
if (processedSubject.length === 0)
return "\u001B[1;31m[ERROR] subject is required\u001B[0m";
Expand Down Expand Up @@ -120,7 +120,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {

return `${tooltipColor}[${tooltip}]\u001B[0m\n ${subjectColor}${subject}\u001B[0m`;
},
filter(subject: string) {
filter: (subject: string) => {
const upperCaseSubject = options.upperCaseSubject || false;

return (
Expand All @@ -141,7 +141,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
name: "breaking",
message: options.messages?.breaking,
default: options.defaultBody || undefined,
when(answers: Answers) {
when: (answers: Answers) => {
if (
options.allowBreakingChanges &&
answers.type &&
Expand All @@ -168,14 +168,20 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
options.allowEmptyIssuePrefixs
);
return issues?.filter((item) => (input ? item.name?.includes(input) : true)) || true;
}
},
when: () =>
!isSingleItem(
options.allowCustomIssuePrefixs,
options.allowEmptyIssuePrefixs,
options.issuePrefixs
)
},
{
type: "input",
name: "footerPrefix",
message: options.messages?.customFooterPrefixs,
default: options.defaultIssues || undefined,
when(answers: Answers) {
when: (answers: Answers) => {
return answers.footerPrefix === "___CUSTOM___";
}
},
Expand All @@ -202,7 +208,7 @@ export const generateQuestions = (options: CommitizenGitOptions, cz: any) => {
? "\u001B[1;90m###--------------------------------------------------------###\u001B[0m"
: "###--------------------------------------------------------###";
console.info(
`\n${SEP}\n${buildCommit(answers, options, options.confirmColorize)}\n${SEP}\n`
`\n${SEP}\n${generateMessage(answers, options, options.confirmColorize)}\n${SEP}\n`
);
return options.messages?.confirmCommit;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cz-git/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// @ts-ignore
import autocompletePrompt from "inquirer-autocomplete-prompt";
import { commitilintConfigLoader } from "@cz-git/loader";
import { generateOptions, generateQuestions } from "./generator";
import { buildCommit, editCommit, log } from "./shared";
import { generateOptions, generateQuestions, generateMessage } from "./generator";
import { editCommit, log } from "./shared";
import type { CommitizenType, QualifiedConfig, UserConfig } from "./shared/types";

export * from "./shared/types";
Expand All @@ -27,7 +27,7 @@ export const prompter = (cz: CommitizenType, commit: (message: string) => void)
break;

case "yes":
commit(buildCommit(answers, options));
commit(generateMessage(answers, options));
break;

default:
Expand Down
7 changes: 4 additions & 3 deletions packages/cz-git/src/shared/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import cnst from "constants";
import rimraf from "rimraf";
import { spawn } from "child_process";
import { Answers, CommitizenGitOptions } from "../types";
import { buildCommit, log } from "./util";
import { log } from "./util";
import { generateMessage } from "../../generator";

/**
* @description: fork by "temp/open" v0.9.4
Expand Down Expand Up @@ -201,7 +202,7 @@ export const editCommit = (
) => {
tempOpen(undefined, (err, info) => {
if (!err) {
fs.writeSync(info.fd, buildCommit(answers, options));
fs.writeSync(info.fd, generateMessage(answers, options));
fs.close(info.fd, () => {
editor(info.path, (code: number) => {
if (code === 0) {
Expand All @@ -212,7 +213,7 @@ export const editCommit = (
} else {
log(
"warm",
`Editor exit non zero. Commit message was:\n${buildCommit(answers, options)}`
`Editor exit non zero. Commit message was:\n${generateMessage(answers, options)}`
);
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/cz-git/src/shared/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./editor";
export * from "./util";
export * from "./rule";
export * from "./wrap";
Loading

0 comments on commit 73f688c

Please sign in to comment.