From c22919ef3fa0c6449f82b1c7af800ef97e8c38c7 Mon Sep 17 00:00:00 2001 From: vivaxy Date: Fri, 27 Mar 2020 20:58:54 +0800 Subject: [PATCH] feat: :sparkles: support auto commit resolve #3 --- package.json | 19 +++------ src/configs/names.ts | 1 + src/extension.ts | 4 ++ src/lib/configuration.ts | 4 +- src/lib/conventional-commits.ts | 69 ++++++++++++++++++++++----------- src/lib/output.ts | 16 ++++++++ src/lib/prompts.ts | 24 ++++++++++-- 7 files changed, 95 insertions(+), 42 deletions(-) create mode 100644 src/lib/output.ts diff --git a/package.json b/package.json index 8cc0d68..f826970 100644 --- a/package.json +++ b/package.json @@ -55,20 +55,10 @@ "configuration": { "title": "VSCode Conventional Commits configuration", "properties": { - "conventionalCommits.automaticOperations": { - "type": "string", - "enum": [ - "none", - "addAndCommit", - "addCommitAndSync" - ], - "default": "addCommitAndSync", - "description": "Custom automatic operations after finishing a commit message.", - "enumDescriptions": [ - "Only put commit message in the Source Control Message input", - "Perform `git add` and `git commit`", - "Perform `git add`, `git commit` and git sync (`git pull` or `git push`)" - ] + "conventionalCommits.autoCommit": { + "type": "boolean", + "default": true, + "description": "Automatically commit files. Enable `git.smartCommit` to commit all changes when there are no staged changes. Set `git.postCommitCommand` to `sync` to run `git.sync` after commit." } } } @@ -76,6 +66,7 @@ "scripts": { "vscode:prepublish": "yarn run compile", "compile": "tsc -p ./", + "watch": "tsc -watch -p ./", "release": "standard-version && vsce publish && git push --follow-tags" }, "devDependencies": { diff --git a/src/configs/names.ts b/src/configs/names.ts index 92e11e1..03fd2a5 100644 --- a/src/configs/names.ts +++ b/src/configs/names.ts @@ -3,3 +3,4 @@ * @author vivaxy */ export const conventionalCommits = 'conventionalCommits'; +export const Conventional_Commits = 'Conventional Commits'; diff --git a/src/extension.ts b/src/extension.ts index 0edf8cc..1139e34 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,11 @@ import * as vscode from 'vscode'; import conventionalCommits from './lib/conventional-commits'; +import * as output from './lib/output'; +import * as names from './configs/names'; export function activate(context: vscode.ExtensionContext) { + output.initialize(); + output.appendLine(`${names.conventionalCommits} started`); const disposable = vscode.commands.registerCommand( 'extension.conventionalCommits', conventionalCommits, diff --git a/src/lib/configuration.ts b/src/lib/configuration.ts index 0a88d1c..2201040 100644 --- a/src/lib/configuration.ts +++ b/src/lib/configuration.ts @@ -6,10 +6,10 @@ import * as vscode from 'vscode'; import * as names from '../configs/names'; export type Configuration = { - automaticOperations: 'none' | 'addAndCommit' | 'addCommitAndSync'; + autoCommit: boolean; }; -export function getConfiguration(): Configuration { +export default function getConfiguration(): Configuration { const config = vscode.workspace .getConfiguration() .get(names.conventionalCommits); diff --git a/src/lib/conventional-commits.ts b/src/lib/conventional-commits.ts index c187ded..718411e 100644 --- a/src/lib/conventional-commits.ts +++ b/src/lib/conventional-commits.ts @@ -4,7 +4,10 @@ */ import * as vscode from 'vscode'; import * as VSCodeGit from '../vendors/git'; -import prompts from './prompts'; +import prompts, { Answers } from './prompts'; +import getConfiguration from './configuration'; +import * as names from '../configs/names'; +import * as output from './output'; function getGitAPI(): VSCodeGit.API | void { const vscodeGit = vscode.extensions.getExtension( @@ -15,29 +18,49 @@ function getGitAPI(): VSCodeGit.API | void { } } -export default async function conventionalCommits() { - const git = getGitAPI(); - if (!git) { - vscode.window.showErrorMessage('vscode.git is not enabled'); - return; +function formatAnswers(answers: Answers) { + let message = ''; + message += answers.type.trim(); + if (answers.scope) { + message += `(${answers.scope})`; + } + message += ': '; + if (answers.gitmoji) { + message += `${answers.gitmoji} `; } - const answers = await prompts(); - vscode.commands.executeCommand('workbench.view.scm'); - git.repositories.forEach(function (repo) { - let message = ''; - message += answers.type.trim(); - if (answers.scope) { - message += `(${answers.scope})`; + message += answers.subject.trim(); + message += '\n\n'; + message += answers.body.trim(); + message += '\n\n'; + message += answers.footer.trim(); + return message; +} + +export default async function conventionalCommits() { + try { + const git = getGitAPI(); + if (!git) { + throw new Error('vscode.git is not enabled'); + } + const answers = await prompts(); + const commitMessage = formatAnswers(answers); + const configuration = getConfiguration(); + vscode.commands.executeCommand('workbench.view.scm'); + // TODO: find current working directory + const repo = git.repositories.find(function (repo) { + return repo.rootUri.fsPath === vscode.workspace.rootPath; + }); + if (!repo) { + throw new Error(`repo not found in path: ${vscode.workspace.rootPath}`); } - message += ': '; - if (answers.gitmoji) { - message += `${answers.gitmoji} `; + repo.inputBox.value = commitMessage; + output.appendLine(`autoCommit: ${configuration.autoCommit}`); + if (configuration.autoCommit) { + await vscode.commands.executeCommand('git.commit'); } - message += answers.subject.trim(); - message += '\n\n'; - message += answers.body.trim(); - message += '\n\n'; - message += answers.footer.trim(); - repo.inputBox.value = message; - }); + } catch (e) { + vscode.window.showErrorMessage( + `${names.Conventional_Commits}: ${e.message}`, + ); + } } diff --git a/src/lib/output.ts b/src/lib/output.ts new file mode 100644 index 0000000..00b1fd1 --- /dev/null +++ b/src/lib/output.ts @@ -0,0 +1,16 @@ +/** + * @since 2020-03-27 08:00 + * @author vivaxy + */ +import * as vscode from 'vscode'; +import * as names from '../configs/names'; + +let output: vscode.OutputChannel; + +export function initialize() { + output = vscode.window.createOutputChannel(names.Conventional_Commits); +} + +export function appendLine(message: string) { + output.appendLine(message); +} diff --git a/src/lib/prompts.ts b/src/lib/prompts.ts index fddc9f9..8eca72b 100644 --- a/src/lib/prompts.ts +++ b/src/lib/prompts.ts @@ -7,7 +7,16 @@ const conventionalCommitsTypes = require('conventional-commit-types'); import gitmojis from '../vendors/gitmojis'; import promptTypes, { PROMPT_TYPES, Prompt } from './prompts/prompt-types'; -export default async function prompts() { +export type Answers = { + type: string; + scope: string; + gitmoji: string; + subject: string; + body: string; + footer: string; +}; + +export default async function prompts(): Promise { const questions: Prompt[] = [ { type: PROMPT_TYPES.QUICK_PICK, @@ -69,10 +78,19 @@ export default async function prompts() { }, ]; - let answers: Record = {}; + const answers: Answers = { + type: '', + scope: '', + gitmoji: '', + subject: '', + body: '', + footer: '', + }; for (const question of questions) { - answers[question.name] = await promptTypes[question.type](question); + answers[question.name as keyof Answers] = await promptTypes[question.type]( + question, + ); } return answers; }