-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
70 lines (58 loc) · 2.32 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Command, EnumType } from "@cliffy/command";
import { getGitDiff } from "./git/GitClient.ts";
import { colors } from "@cliffy/ansi/colors";
import { CommitMessageStrategyFactory } from "./commit/CommitMessageStrategyFactory.ts";
import { getCommitMessage } from "./infra/HttpClient.ts";
import { Logger } from "./infra/Logger.ts";
import { writeText } from "https://deno.land/x/copy_paste/mod.ts";
export enum CommitMessageStrategyArgs {
CONVENTIONAL_COMMITS = "conventional-commits",
GITMOJI = "gitmoji",
}
const commitMessageStrategy = new EnumType(CommitMessageStrategyArgs);
await new Command()
.name("aicommit")
.version("1.0.0-beta")
.description("Generate your commit messages with AI")
.type("message-strategy", commitMessageStrategy)
.option(
"-s, --strategy <strategy:message-strategy>",
"Specify the commit message strategy",
{
default: CommitMessageStrategyArgs.CONVENTIONAL_COMMITS,
required: true,
},
)
.option(
"-p, --extra-prompt <extra-prompt:string>",
"Specify and additional extra prompt for context",
{ required: false },
)
.arguments("<git-directory:file>")
.action(async ({ strategy: strategyArg, extraPrompt }, directory) => {
Logger.info(
"Querying git diff for directory",
colors.italic(directory),
`using ${strategyArg} strategy...`,
);
const { diff, operationTimeMs } = await getGitDiff(directory);
if (diff.trim().length === 0) {
Logger.error(
"No staged changes were found. Did you forget to stage your changes before running aicommit?",
);
return Deno.exit();
}
Logger.info(
`Successfully received git diff in ${operationTimeMs}ms. Fetching AI response...`,
);
const strategy = CommitMessageStrategyFactory.getStrategy(strategyArg);
const prompt = strategy.getPrompt(diff, extraPrompt);
const { commitMessage, durationMs } = await getCommitMessage(prompt);
Logger.info(
`Received AI commit message in ${durationMs}ms.`,
);
console.log(commitMessage);
await writeText(commitMessage);
Logger.info("Copied commit message to clipboard");
})
.parse(Deno.args);