Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(breakingchange): ✨ add breaking change configuration #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,30 @@
"type": "boolean",
"default": false,
"markdownDescription": "%extension.configuration.editor.keepAfterSave.markdownDescription%"
},
"conventionalCommits.detectBreakingChange": {
"type": "boolean",
"default": false,
"markdownDescription": "%extension.configuration.detectBreakingChange.markdownDescription%"
},
"conventionalCommits.promptBreakingChange": {
"type": "boolean",
"default": false,
"markdownDescription": "%extension.configuration.promptBreakingChange.markdownDescription%"
},
"conventionalCommits.breakingChangeFormat": {
"type": "string",
"default": "both",
"enum": [
"space",
"hyphen",
"both"
],
"markdownEnumDescriptions": [
"%extension.configuration.breakingChangeFormat.markdownEnumDescriptions.space%",
"%extension.configuration.breakingChangeFormat.markdownEnumDescriptions.hyphen%",
"%extension.configuration.breakingChangeFormat.markdownEnumDescriptions.both%"
]
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"extension.configuration.showEditor.markdownDescription": "Control whether the extension should show the commit message as a text document in a separate tab.",
"extension.configuration.showNewVersionNotes.markdownDescription": "Control whether the extension should show the new version notes.",
"extension.configuration.editor.keepAfterSave.markdownDescription": "Control whether the extension should allow keeping edit status after saving the commit message.",
"extension.configuration.detectBreakingChange.markdownDescription": "Control whether the extension should add optional `!` into commit message when have `BREAKING CHANGE: ` or `BREAKING-CHANGE: ` in `footer`.",
"extension.configuration.promptBreakingChange.markdownDescription": "Controls whether the extension should prompt for the `breaking change` section.",
"extension.configuration.breakingChangeFormat.markdownEnumDescriptions.space": "Display one prompt selection as `BREAKING CHANGE: `",
"extension.configuration.breakingChangeFormat.markdownEnumDescriptions.hyphen": "Display one prompt selection as `BREAKING-CHANGE: `",
"extension.configuration.breakingChangeFormat.markdownEnumDescriptions.both": "Display two prompts selection as `BREAKING CHANGE: ` and `BREAKING-CHANGE: `",
"extension.sources.repositoryNotFoundInPath": "Repository not found in path: ",
"extension.sources.repositoriesEmpty": "Please open a repository.",
"extension.sources.promptRepositoryPlaceholder": "Choose a repository.",
Expand Down
5 changes: 5 additions & 0 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"extension.configuration.showEditor.markdownDescription": "是否需要在新标签页用文本编辑器展示提交信息",
"extension.configuration.showNewVersionNotes.markdownDescription": "是否需要显示新版本说明。",
"extension.configuration.editor.keepAfterSave.markdownDescription": "是否需要在保存提交消息后保持编辑状态。",
"extension.configuration.detectBreakingChange.markdownDescription": "当检测到 `footer` 中有 `BREAKING-CHANGE: ` 或者 `BREAKING CHANGE: ` 时,是否需要在提交信息中添加可选项 `!`。",
"extension.configuration.promptBreakingChange.markdownDescription": "是否需要提示填写 `Breaking change`",
"extension.configuration.breakingChangeFormat.markdownEnumDescriptions.space": "显示提示选项 `BREAKING CHANGE: `",
"extension.configuration.breakingChangeFormat.markdownEnumDescriptions.hyphen": "显示提示选项 `BREAKING-CHANGE: `",
"extension.configuration.breakingChangeFormat.markdownEnumDescriptions.both": "同时显示两种提示选项 `BREAKING CHANGE: ` 和 `BREAKING-CHANGE: `",
"extension.sources.repositoryNotFoundInPath": "以下路径中未找到仓库:",
"extension.sources.repositoriesEmpty": "请打开一个仓库。",
"extension.sources.promptRepositoryPlaceholder": "请选择一个仓库。",
Expand Down
69 changes: 47 additions & 22 deletions src/lib/commit-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
export class CommitMessage {
private _type: string = '';
private _scope: string = '';
private _breakingChange: string = '';
private _gitmoji: string = '';
private _subject: string = '';
private _body: string = '';
Expand All @@ -26,6 +27,14 @@ export class CommitMessage {
this._scope = input.trim();
}

get breakingChange() {
return this._breakingChange;
}

set breakingChange(input: string) {
this._breakingChange = input.trim();
}

get gitmoji() {
return this._gitmoji;
}
Expand All @@ -49,7 +58,6 @@ export class CommitMessage {
set body(input: string) {
this._body = input.trim();
}

get footer() {
return this._footer;
}
Expand Down Expand Up @@ -77,34 +85,51 @@ export function serializeSubject(partialCommitMessage: {
return result;
}

export function serializeHeader(partialCommitMessage: {
type: string;
scope: string;
gitmoji: string;
subject: string;
export function serializeHeader({
partialCommitMessage,
detectBreakingChange,
}: {
partialCommitMessage: {
type: string;
scope: string;
breakingChange: string;
gitmoji: string;
subject: string;
};
detectBreakingChange: boolean;
}) {
let result = '';
result += partialCommitMessage.type;
const { scope } = partialCommitMessage;
if (scope) {
result += `(${scope})`;
}
result += ': ';
let result = '' + partialCommitMessage.type;

const { scope, breakingChange } = partialCommitMessage;
if (scope) result += `(${scope})`;

if (breakingChange && (breakingChange === '!' || detectBreakingChange)) {
result += `!: `;
} else result += ': ';

const subject = serializeSubject(partialCommitMessage);
if (subject) {
result += subject;
}
if (subject) result += subject;

return result;
}

export function serialize(commitMessage: CommitMessage) {
let message = serializeHeader(commitMessage);
const { body, footer } = commitMessage;
if (body) {
message += `\n\n${body}`;
export function serialize(
commitMessage: CommitMessage,
detectBreakingChange: boolean,
) {
let message = serializeHeader({
partialCommitMessage: commitMessage,
detectBreakingChange,
});
const { breakingChange, body, footer } = commitMessage;
if (body) message += `\n\n${body}`;

if (breakingChange && breakingChange != '!') {
message += `\n\n${breakingChange}`;
}
if (footer) {
message += `\n\n${footer}`;
message += breakingChange && breakingChange != '!' ? '\n' : '\n\n';
message += footer;
}
return message;
}
9 changes: 9 additions & 0 deletions src/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export enum EMOJI_FORMAT {
emoji = 'emoji',
}

export enum BREAKING_CHANGE_FORMAT {
hyphen = 'hyphen',
space = 'space',
both = 'both',
}

export type Configuration = {
autoCommit: boolean;
gitmoji: boolean;
Expand All @@ -22,6 +28,9 @@ export type Configuration = {
promptFooter: boolean;
showNewVersionNotes: boolean;
'editor.keepAfterSave': boolean;
detectBreakingChange: boolean;
promptBreakingChange: boolean;
breakingChangeFormat: BREAKING_CHANGE_FORMAT;
};

export function getConfiguration() {
Expand Down
16 changes: 13 additions & 3 deletions src/lib/conventional-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export default function createConventionalCommits() {
);

// 5. get message
const detectBreakingChange = configuration.get<boolean>(
'detectBreakingChange',
);
const commitMessage = await prompts({
gitmoji: configuration.get<boolean>('gitmoji'),
showEditor: configuration.get<boolean>('showEditor'),
Expand All @@ -151,10 +154,17 @@ export default function createConventionalCommits() {
promptScopes: configuration.get<boolean>('promptScopes'),
promptBody: configuration.get<boolean>('promptBody'),
promptFooter: configuration.get<boolean>('promptFooter'),
promptBreakingChange: configuration.get<boolean>(
'promptBreakingChange',
),
detectBreakingChange: detectBreakingChange,
breakingChangeFormat: configuration.get<boolean>(
'breakingChangeFormat',
),
});
output.info(`messageJSON:\n${JSON.stringify(commitMessage, null, 2)}`);
const message = serialize(commitMessage);
output.info(`message:\n${message}`);
output.info(`commitMessage: ${JSON.stringify(commitMessage, null, 2)}`);
let message = serialize(commitMessage, detectBreakingChange);
output.info(`message: ${message}`);

// 6. switch to scm and put message into message box
// or show the entire commit message in a separate tab
Expand Down
59 changes: 53 additions & 6 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export default async function prompts({
promptScopes,
promptBody,
promptFooter,
promptBreakingChange,
detectBreakingChange,
breakingChangeFormat,
}: {
gitmoji: boolean;
showEditor: boolean;
Expand All @@ -40,6 +43,9 @@ export default async function prompts({
promptScopes: boolean;
promptBody: boolean;
promptFooter: boolean;
promptBreakingChange: boolean;
detectBreakingChange: boolean;
breakingChangeFormat: boolean;
}): Promise<CommitMessage> {
const commitMessage = new CommitMessage();
const conventionalCommitsTypes = getTypesByLocale(locale).types;
Expand Down Expand Up @@ -114,14 +120,14 @@ export default async function prompts({
name,
placeholder,
configurationKey: keys.SCOPES as keyof configuration.Configuration,
noneItem,
newItem: {
label: getPromptLocalize('scope.newItem.label'),
description: '',
detail: getPromptLocalize('scope.newItem.detail'),
alwaysShow: true,
placeholder: getPromptLocalize('scope.newItem.placeholder'),
},
noneItem,
newItemWithoutSetting: {
label: getPromptLocalize('scope.newItemWithoutSetting.label'),
description: '',
Expand All @@ -146,6 +152,39 @@ export default async function prompts({
},
},
getScopePrompt(),
{
type: PROMPT_TYPES.BREAKING_CHANGE_QUICK_PICK,
name: 'breakingChange',
placeholder: getPromptLocalize('breakingChange.placeholder'),
noneItem: {
label: getPromptLocalize('breakingChange.noneItem.label'),
description: '',
detail: getPromptLocalize('breakingChange.noneItem.detail'),
alwaysShow: true,
},
pointItem: {
label: getPromptLocalize('breakingChange.pointItem.label'),
description: '',
detail: getPromptLocalize('breakingChange.pointItem.detail'),
alwaysShow: true,
},
spaceItem: {
label: getPromptLocalize('breakingChange.spaceItem.label'),
description: '',
detail: getPromptLocalize('breakingChange.spaceItem.detail'),
alwaysShow: true,
placeholder: getPromptLocalize('breakingChange.message.placeholder'),
},
hyphenItem: {
label: getPromptLocalize('breakingChange.hyphenItem.label'),
description: '',
detail: getPromptLocalize('breakingChange.hyphenItem.detail'),
alwaysShow: true,
placeholder: getPromptLocalize('breakingChange.message.placeholder'),
},
breakingChangeFormat: breakingChangeFormat,
format: lineBreakFormatter,
},
{
type: PROMPT_TYPES.QUICK_PICK,
name: 'gitmoji',
Expand All @@ -169,7 +208,7 @@ export default async function prompts({
name: 'subject',
placeholder: getPromptLocalize('subject.placeholder'),
validate(input: string) {
const { type, scope, gitmoji } = commitMessage;
const { type, scope, breakingChange, gitmoji } = commitMessage;
const serializedSubject = serializeSubject({
gitmoji,
subject: input,
Expand All @@ -188,10 +227,14 @@ export default async function prompts({

let headerError = commitlint.lintHeader(
serializeHeader({
type,
scope,
gitmoji,
subject: input,
partialCommitMessage: {
type,
scope,
breakingChange,
gitmoji,
subject: input,
},
detectBreakingChange: detectBreakingChange,
}),
);
if (headerError) {
Expand Down Expand Up @@ -241,6 +284,10 @@ export default async function prompts({

if (question.name === 'gitmoji' && !gitmoji) return false;

if (question.name === 'breakingChange') {
if (!promptBreakingChange) return false;
}

if (question.name === 'body') {
if (showEditor || !promptBody) return false;
}
Expand Down
Loading