-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·153 lines (128 loc) · 4.03 KB
/
index.ts
File metadata and controls
executable file
·153 lines (128 loc) · 4.03 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bun
import { input, search, select } from "@inquirer/prompts";
import { gitmojis, type Gitmoji } from "./gitmojis";
type ConventionalType = {
value: string;
name: string;
description: string;
};
type GitmojiChoice = {
value: Gitmoji;
name: string;
description: string;
short: string;
};
const conventionalTypes: ConventionalType[] = [
{ value: "feat", name: "feat", description: "A new feature" },
{ value: "fix", name: "fix", description: "A bug fix" },
{ value: "docs", name: "docs", description: "Documentation only changes" },
{ value: "style", name: "style", description: "Formatting changes; no code behavior changes" },
{ value: "refactor", name: "refactor", description: "A code change that neither fixes a bug nor adds a feature" },
{ value: "perf", name: "perf", description: "A code change that improves performance" },
{ value: "test", name: "test", description: "Add or update tests" },
{ value: "build", name: "build", description: "Build system or external dependency changes" },
{ value: "ci", name: "ci", description: "CI configuration and workflow changes" },
{ value: "chore", name: "chore", description: "Other maintenance tasks" },
{ value: "revert", name: "revert", description: "Revert a previous commit" }
];
function buildTypeChoices(): ConventionalType[] {
return conventionalTypes.map((type) => ({
value: type.value,
name: type.name,
description: type.description
}));
}
function scoreGitmoji(gitmoji: Gitmoji, term?: string): number {
if (!term) {
return 0;
}
const query = term.trim().toLowerCase();
const haystack = [
gitmoji.emoji,
gitmoji.code,
gitmoji.name,
gitmoji.description
].join(" ").toLowerCase();
if (gitmoji.description.toLowerCase().startsWith(query)) {
return 4;
}
if (gitmoji.code.toLowerCase().includes(query) || gitmoji.name.toLowerCase().includes(query)) {
return 3;
}
if (gitmoji.description.toLowerCase().includes(query)) {
return 2;
}
if (haystack.includes(query)) {
return 1;
}
return -1;
}
function buildGitmojiChoices(term?: string): GitmojiChoice[] {
return gitmojis
.map((gitmoji, index) => ({
gitmoji,
index,
score: scoreGitmoji(gitmoji, term)
}))
.filter((entry) => entry.score >= 0)
.sort((left, right) => {
if (right.score !== left.score) {
return right.score - left.score;
}
return left.index - right.index;
})
.map(({ gitmoji }) => ({
value: gitmoji,
name: `${gitmoji.emoji} ${gitmoji.code}`,
description: gitmoji.description,
short: `${gitmoji.emoji} ${gitmoji.code}`
}));
}
function formatCommitMessage(
type: string,
gitmoji: Gitmoji,
scope: string,
description: string
): string {
const normalizedScope = scope.trim();
const normalizedDescription = description.trim();
const typeWithScope = normalizedScope ? `${type}(${normalizedScope})` : type;
return `${typeWithScope}: ${gitmoji.emoji} ${normalizedDescription}`;
}
async function main(): Promise<void> {
const type = await select({
message: "Select a conventional commit type",
choices: buildTypeChoices(),
pageSize: 11
});
const selectedGitmoji = await search({
message: "Select a gitmoji (search by description, code, or name)",
source: async (term) => buildGitmojiChoices(term),
pageSize: 10,
validate: (value) => Boolean(value) || "Select a gitmoji."
});
const scope = await input({
message: "Scope (optional)",
default: "",
transformer: (value) => value.trim()
});
const description = await input({
message: "Description",
validate: (value) => {
if (!value.trim()) {
return "Description is required.";
}
return true;
}
});
const commitMessage = formatCommitMessage(type, selectedGitmoji, scope, description);
console.log("");
console.log(commitMessage);
}
main().catch((error: unknown) => {
if (error instanceof Error && error.name === "ExitPromptError") {
process.exit(130);
}
console.error(error);
process.exit(1);
});