-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitwizard.js
More file actions
executable file
·246 lines (222 loc) · 7.67 KB
/
commitwizard.js
File metadata and controls
executable file
·246 lines (222 loc) · 7.67 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
import Enquirer from "enquirer";
import fs from "fs-extra";
import { execSync } from "child_process";
import path from "path";
import { fileURLToPath } from "url";
// Import the prompt method from Enquirer
const { prompt } = Enquirer;
// Get version number from package.json
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageJson = fs.readJSONSync(path.join(__dirname, "package.json"));
// Function to initialize a default .commitwizardrc file
const initCommitWizardConfig = () => {
const configPath = path.resolve(process.cwd(), ".commitwizardrc");
if (fs.existsSync(configPath)) {
console.log(".commitwizardrc already exists in this directory.");
return;
}
const defaultConfig = {
categories: [
{
label: "chore",
description: "Changes to the build process or auxiliary tools",
},
{ label: "ci", description: "Continuous Integration and deployment" },
{ label: "docs", description: "Documentation only changes" },
{ label: "feat", description: "A new feature" },
{ label: "fix", description: "A bug fix" },
{ label: "perf", description: "Performance updates and optimization" },
{
label: "refactor",
description:
"A code change that neither fixes a bug nor adds a feature",
},
{ label: "remove", description: "Removing files or unnecessary code" },
{
label: "revert",
description: "Undo changes that have been committed to the repository",
},
{
label: "style",
description: "Changes that do not affect the meaning of the code",
},
{
label: "test",
description: "Adding tests or correcting existing tests",
},
{
label: "update",
description: "Small updates that do not change functionality",
},
],
};
try {
fs.writeJSONSync(configPath, defaultConfig, { spaces: 2 });
console.log(".commitwizardrc file has been generated successfully.");
} catch (error) {
console.error("Error generating .commitwizardrc file:", error.message);
}
};
// Handle command-line arguments for version, config, log, undo, and amend
if (process.argv.includes("-v") || process.argv.includes("--version")) {
console.log(`CommitWizard CLI version: ${packageJson.version}`);
process.exit(0);
} else if (process.argv.includes("--config")) {
initCommitWizardConfig();
process.exit(0);
} else if (process.argv.includes("--log")) {
showCommitHistory();
process.exit(0);
} else if (process.argv.includes("--undo")) {
undoLastCommit();
process.exit(0);
} else if (process.argv.includes("--amend")) {
amendLastCommit();
process.exit(0);
}
// Function to show commit history
const showCommitHistory = () => {
try {
const log = execSync("git log --oneline -n 10").toString();
console.log("Recent commits:");
console.log(log);
} catch (error) {
console.error("Error fetching commit history:", error.message);
}
};
// Function to undo the last commit (soft reset)
const undoLastCommit = () => {
try {
execSync("git reset --soft HEAD~1", { stdio: "inherit" });
console.log("Last commit has been undone, and changes are still staged.");
} catch (error) {
console.error("Error undoing the last commit:", error.message);
}
};
// Function to amend the last commit
const amendLastCommit = () => {
try {
execSync("git commit --amend", { stdio: "inherit" });
console.log("Last commit has been amended.");
} catch (error) {
console.error("Error amending the last commit:", error.message);
}
};
// Check if there are changes to commit
const checkForChanges = () => {
try {
const status = execSync("git status --porcelain").toString().trim();
return status.length > 0; // If there is output, it means there are changes
} catch (error) {
console.error("Error checking Git status:", error.message);
process.exit(1);
}
};
const loadCommitCategories = () => {
const configPath = path.resolve(process.cwd(), ".commitwizardrc");
if (fs.existsSync(configPath)) {
try {
const config = fs.readJSONSync(configPath);
if (Array.isArray(config.categories) && config.categories.length > 0) {
return config.categories.map((category) => ({
name: category.label,
message: `[${category.label}]: ${category.description}`,
}));
}
} catch (error) {
console.error("Error reading .commitwizardrc file:", error.message);
}
}
// Default categories if configuration file is not found or has errors
return [
{
name: "chore",
message: "[chore]: Changes to the build process or auxiliary tools",
},
{ name: "ci", message: "[ci]: Continuous Integration and deployment" },
{ name: "docs", message: "[docs]: Documentation only changes" },
{ name: "feat", message: "[feat]: A new feature" },
{ name: "fix", message: "[fix]: A bug fix" },
{ name: "perf", message: "[perf]: Performance updates and optimization" },
{
name: "refactor",
message:
"[refactor]: A code change that neither fixes a bug nor adds a feature",
},
{ name: "remove", message: "[remove]: Removing files or unnecessary code" },
{
name: "revert",
message:
"[revert]: Undo changes that have been committed to the repository",
},
{
name: "style",
message: "[style]: Changes that do not affect the meaning of the code",
},
{
name: "test",
message: "[test]: Adding tests or correcting existing tests",
},
{
name: "update",
message: "[update]: Small updates that do not change functionality",
},
];
};
async function runCommitWizard() {
// Check if there are any changes to commit
if (!checkForChanges()) {
console.log(
"No changes to commit. Please make some changes before running CommitWizard."
);
process.exit(0);
}
try {
const commitCategories = loadCommitCategories();
// Prompt for commit category
const categoryResponse = await prompt({
type: "select",
name: "category",
message: "Select the type of change that you're committing:",
choices: commitCategories,
});
// Prompt for commit message
const messageResponse = await prompt({
type: "input",
name: "message",
message: "Write your commit message:",
});
// Repeated prompts for multi-line description
let description = "";
let addMore = true;
while (addMore) {
const descriptionResponse = await prompt({
type: "input",
name: "line",
message:
"Write a commit description (optional): \n- To skip, leave blank & press Enter. Or, \n- Write a description & press Enter for more lines. Or, \n- Paste multiple lines at once & press Enter to finish. \n ",
});
if (descriptionResponse.line) {
description += `${descriptionResponse.line}\n`;
} else {
addMore = false; // End the loop if no input is provided
}
}
const category = categoryResponse.category;
const message = messageResponse.message;
// Escape double quotes in the message and description
const escapedMessage = message.replace(/"/g, '\\"');
const escapedDescription = description.trim().replace(/"/g, '\\"');
// Combine short message and description
const fullCommitMessage = escapedDescription
? `[${category}]: ${escapedMessage}\n\n${escapedDescription}`
: `[${category}]: ${escapedMessage}`;
// Use double quotes to wrap the full commit message
execSync(`git commit -m "${fullCommitMessage}"`, { stdio: "inherit" });
} catch (error) {
console.error("Error:", error.message);
}
}
runCommitWizard();