From b234326e8d366db58f8195d7d460b46c243a32db Mon Sep 17 00:00:00 2001 From: NetSkylz Date: Sat, 3 Jun 2023 15:18:01 +0200 Subject: [PATCH] feat(scope): add option to always use new scope --- README.md | 31 ++++++++++++++++--------------- package.nls.json | 1 + package.nls.zh-cn.json | 1 + src/lib/configuration.ts | 1 + src/lib/conventional-commits.ts | 11 ++++++++--- src/lib/prompts.ts | 16 ++++++++++++++++ 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index d01b929..f17e2f5 100644 --- a/README.md +++ b/README.md @@ -36,21 +36,22 @@ You can access VSCode Conventional Commits in two ways: ### Extension Configuration -| name | description | default | -| :-----------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------: | -| `conventionalCommits.autoCommit` | Control whether the extension should commit files after: forming the message or closing the editor tab.
When `#git.enableSmartCommit#` enabled and `#git.smartCommitChanges#` was set to `all`, It allows to commit all changes when there are no staged changes.
And set `#git.postCommitCommand#` to `sync` to run `git.sync` after commit. | true | -| `conventionalCommits.emojiFormat` | Specify which format will be shown in the `gitmoji`. | code | -| `conventionalCommits.gitmoji` | Control whether the extension should prompt for a `gitmoji`. | true | -| `conventionalCommits.lineBreak` | Specify which word will be treated as line breaks in the `body`.
Blank means no line breaks. | "" | -| `conventionalCommits.promptBody` | Control whether the extension should prompt for the `body` section. | true | -| `conventionalCommits.promptFooter` | Control whether the extension should prompt for the `footer` section. | true | -| `conventionalCommits.promptCI` | Control whether the extension should prompt for skipping CI run. | false | -| `conventionalCommits.promptScopes` | Control whether the extension should prompt for the `scope` section. | true | -| `conventionalCommits.scopes` | Specify available selections in the `scope` section. | [] | -| `conventionalCommits.showEditor` | Control whether the extension should show the commit message as a text document in a separate tab. | false | -| `conventionalCommits.showNewVersionNotes` | Control whether the extension should show the new version notes. | true | -| `conventionalCommits.silentAutoCommit` | Control that auto commit should be silent, without focusing source control panel. | false | -| `conventionalCommits.editor.keepAfterSave` | Control whether the extension should keep the editor tab open after saving the commit message. | false | +| name | description | default | +| :----------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----: | +| `conventionalCommits.autoCommit` | Control whether the extension should commit files after: forming the message or closing the editor tab.
When `#git.enableSmartCommit#` enabled and `#git.smartCommitChanges#` was set to `all`, It allows to commit all changes when there are no staged changes.
And set `#git.postCommitCommand#` to `sync` to run `git.sync` after commit. | true | +| `conventionalCommits.emojiFormat` | Specify which format will be shown in the `gitmoji`. | code | +| `conventionalCommits.gitmoji` | Control whether the extension should prompt for a `gitmoji`. | true | +| `conventionalCommits.lineBreak` | Specify which word will be treated as line breaks in the `body`.
Blank means no line breaks. | "" | +| `conventionalCommits.promptBody` | Control whether the extension should prompt for the `body` section. | true | +| `conventionalCommits.promptFooter` | Control whether the extension should prompt for the `footer` section. | true | +| `conventionalCommits.promptCI` | Control whether the extension should prompt for skipping CI run. | false | +| `conventionalCommits.promptScopes` | Control whether the extension should prompt for the `scope` section. | true | +| `conventionalCommits.scopes` | Specify available selections in the `scope` section. | [] | +| `conventionalCommits.alwaysUseNewScope` | Always use a new unsaved `scope` for each commit. | false | +| `conventionalCommits.showEditor` | Control whether the extension should show the commit message as a text document in a separate tab. | false | +| `conventionalCommits.showNewVersionNotes` | Control whether the extension should show the new version notes. | true | +| `conventionalCommits.silentAutoCommit` | Control that auto commit should be silent, without focusing source control panel. | false | +| `conventionalCommits.editor.keepAfterSave` | Control whether the extension should keep the editor tab open after saving the commit message. | false | ## Commit Workflow diff --git a/package.nls.json b/package.nls.json index f99256b..a506b9c 100644 --- a/package.nls.json +++ b/package.nls.json @@ -32,6 +32,7 @@ "extension.sources.prompt.scope.newItem.placeholder": "Create a new scope.", "extension.sources.prompt.scope.newItemWithoutSetting.label": "New scope (only use once)", "extension.sources.prompt.scope.newItemWithoutSetting.detail": "Use a new scope. (The scope will not be added in workspace `settings.json`.)", + "extension.sources.prompt.scope.alwaysUseNewUnsavedScope.placeholder": "Write the scope of the change", "extension.sources.prompt.gitmoji.placeholder": "Choose a gitmoji.", "extension.sources.prompt.gitmoji.noneItem.label": "None", "extension.sources.prompt.gitmoji.noneItem.detail": "No gitmoji.", diff --git a/package.nls.zh-cn.json b/package.nls.zh-cn.json index 8b95571..91d1adc 100644 --- a/package.nls.zh-cn.json +++ b/package.nls.zh-cn.json @@ -32,6 +32,7 @@ "extension.sources.prompt.scope.newItem.placeholder": "请输入作用域。", "extension.sources.prompt.scope.newItemWithoutSetting.label": "新作用域(仅用一次)", "extension.sources.prompt.scope.newItemWithoutSetting.detail": "使用新作用域。(不会加入工作区的 `settings.json`)", + "extension.sources.prompt.scope.alwaysUseNewUnsavedScope.placeholder": "写出变更范围", "extension.sources.prompt.gitmoji.placeholder": "请选择 gitmoji。", "extension.sources.prompt.gitmoji.noneItem.label": "无", "extension.sources.prompt.gitmoji.noneItem.detail": "无 gitmoji。", diff --git a/src/lib/configuration.ts b/src/lib/configuration.ts index 5935822..88c9ccc 100644 --- a/src/lib/configuration.ts +++ b/src/lib/configuration.ts @@ -23,6 +23,7 @@ export type Configuration = { promptFooter: boolean; promptCI: boolean; showNewVersionNotes: boolean; + alwaysUseNewUnsavedScope: boolean; 'editor.keepAfterSave': boolean; }; diff --git a/src/lib/conventional-commits.ts b/src/lib/conventional-commits.ts index a11b555..bebd966 100644 --- a/src/lib/conventional-commits.ts +++ b/src/lib/conventional-commits.ts @@ -101,7 +101,9 @@ async function getRepository({ } export default function createConventionalCommits() { - return async function conventionalCommits(repoUri?: VSCodeGit.Repository | vscode.Uri) { + return async function conventionalCommits( + repoUri?: VSCodeGit.Repository | vscode.Uri, + ) { try { output.info('Conventional commits started.'); @@ -121,11 +123,11 @@ export default function createConventionalCommits() { // 3. get repository let _repoUri = repoUri; - if (!(repoUri instanceof vscode.Uri) && (repoUri !== undefined)) { + if (!(repoUri instanceof vscode.Uri) && repoUri !== undefined) { _repoUri = repoUri.rootUri; } const repository = await getRepository({ - arg: (_repoUri), + arg: _repoUri, git: git, workspaceFolders: vscode.workspace.workspaceFolders, }); @@ -154,6 +156,9 @@ export default function createConventionalCommits() { promptBody: configuration.get('promptBody'), promptFooter: configuration.get('promptFooter'), promptCI: configuration.get('promptCI'), + alwaysUseNewUnsavedScope: configuration.get( + 'alwaysUseNewUnsavedScope', + ), }); output.info(`messageJSON:\n${JSON.stringify(commitMessage, null, 2)}`); const message = serialize(commitMessage); diff --git a/src/lib/prompts.ts b/src/lib/prompts.ts index a57789d..d64bba6 100644 --- a/src/lib/prompts.ts +++ b/src/lib/prompts.ts @@ -39,6 +39,7 @@ export default async function prompts({ promptBody, promptFooter, promptCI, + alwaysUseNewUnsavedScope, }: { gitmoji: boolean; showEditor: boolean; @@ -48,6 +49,7 @@ export default async function prompts({ promptBody: boolean; promptFooter: boolean; promptCI: boolean; + alwaysUseNewUnsavedScope: boolean; }): Promise { const commitMessage = new CommitMessage(); const conventionalCommitsTypes = getTypesByLocale(locale).types; @@ -101,6 +103,20 @@ export default async function prompts({ detail: getPromptLocalize('scope.noneItem.detail'), alwaysShow: true, }; + + if (alwaysUseNewUnsavedScope) { + return { + type: PROMPT_TYPES.INPUT_BOX, + name, + placeholder: getPromptLocalize( + 'scope.alwaysUseNewUnsavedScope.placeholder', + ), + validate(input: string) { + return commitlint.lintScope(input); + }, + }; + } + if (scopeEnum.length) { return { type: PROMPT_TYPES.QUICK_PICK,