Skip to content

Commit

Permalink
feat: ✨ support auto commit
Browse files Browse the repository at this point in the history
resolve #3
  • Loading branch information
vivaxy committed Mar 27, 2020
1 parent 50813e9 commit c22919e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 42 deletions.
19 changes: 5 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,18 @@
"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."
}
}
}
},
"scripts": {
"vscode:prepublish": "yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"release": "standard-version && vsce publish && git push --follow-tags"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/configs/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* @author vivaxy
*/
export const conventionalCommits = 'conventionalCommits';
export const Conventional_Commits = 'Conventional Commits';
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Configuration>(names.conventionalCommits);
Expand Down
69 changes: 46 additions & 23 deletions src/lib/conventional-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<VSCodeGit.GitExtension>(
Expand All @@ -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}`,
);
}
}
16 changes: 16 additions & 0 deletions src/lib/output.ts
Original file line number Diff line number Diff line change
@@ -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);
}
24 changes: 21 additions & 3 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Answers> {
const questions: Prompt[] = [
{
type: PROMPT_TYPES.QUICK_PICK,
Expand Down Expand Up @@ -69,10 +78,19 @@ export default async function prompts() {
},
];

let answers: Record<string, string> = {};
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;
}

0 comments on commit c22919e

Please sign in to comment.