-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·223 lines (192 loc) · 6.52 KB
/
index.js
File metadata and controls
executable file
·223 lines (192 loc) · 6.52 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
#!/usr/bin/env node
const inquirer = require('inquirer');
const chalk = require('chalk').default;
const git = require('simple-git')();
const { execSync } = require('child_process');
const commitTypes = [
{ name: 'feat: A new feature', value: 'feat' },
{ name: 'fix: A bug fix', value: 'fix' },
{ name: 'docs: Documentation changes', value: 'docs' },
{ name: 'chore: Maintenance tasks', value: 'chore' },
{ name: 'style: Code style changes', value: 'style' },
{ name: 'refactor: Code refactoring', value: 'refactor' },
{ name: 'test: Adding tests', value: 'test' }
];
// Function to validate commit message using commitlint
function validateCommitMessage(message) {
try {
// Use commitlint to validate the message
execSync(`echo "${message}" | npx commitlint`, { stdio: 'pipe' });
return { isValid: true, error: null };
} catch (error) {
return {
isValid: false,
error: error.stdout?.toString() || error.message
};
}
}
// Function to format commitlint error for better readability
function formatCommitlintError(error) {
const lines = error.split('\n');
const relevantLines = lines.filter(line =>
line.includes('✖') || line.includes('⧗') || line.trim().startsWith('-')
);
return relevantLines.join('\n');
}
// Add this function to show staged files and allow modification
async function handleStagingArea() {
try {
const status = await git.status();
// Check if status object has the expected properties
if (!status) {
console.log(chalk.yellow('⚠ Unable to read git status.'));
return null;
}
const staged = status.staged || [];
const notStaged = status.not_staged || [];
const files = status.files || [];
if (staged.length === 0 && notStaged.length === 0 && files.length === 0) {
console.log(chalk.yellow('⚠ No changes to commit.'));
return null;
}
if (staged.length === 0 && (notStaged.length > 0 || files.length > 0)) {
console.log(chalk.yellow('No files staged. Current changes:'));
// Show unstaged changes
if (notStaged.length > 0) {
notStaged.forEach(file => {
console.log(chalk.gray(` 📄 ${file}`));
});
}
// Also show other files if available
if (files.length > 0 && notStaged.length === 0) {
files.forEach(file => {
if (file.working_dir !== ' ' && file.working_dir !== '?') {
console.log(chalk.gray(` 📄 ${file.path} (${file.working_dir})`));
}
});
}
const { shouldStageAll } = await inquirer.prompt([
{
type: 'confirm',
name: 'shouldStageAll',
message: 'Stage all changes for commit?',
default: true
}
]);
if (shouldStageAll) {
await git.add('.');
console.log(chalk.green('✓ All changes staged.'));
} else {
// Use files array for selection
const filesToSelect = notStaged.length > 0 ? notStaged : files.map(f => f.path);
const { filesToStage } = await inquirer.prompt([
{
type: 'checkbox',
name: 'filesToStage',
message: 'Select files to stage:',
choices: filesToSelect.map(file => ({
name: typeof file === 'string' ? file : file.path,
value: typeof file === 'string' ? file : file.path
}))
}
]);
if (filesToStage.length > 0) {
await git.add(filesToStage);
console.log(chalk.green(`✓ Staged ${filesToStage.length} files.`));
} else {
console.log(chalk.yellow('No files staged. Commit cancelled.'));
return null;
}
}
}
return await git.status();
} catch (error) {
console.log(chalk.yellow('⚠ Could not check git status:', error.message));
return null;
}
}
async function run() {
try {
console.log(chalk.blue('🚀 Git Commit Wizard\n'));
// Handle staging area first
const status = await handleStagingArea();
if (!status) {
return;
}
// Then proceed with commit message creation
const answers = await inquirer.prompt([
{
type: 'list',
name: 'type',
message: 'Select commit type:',
choices: commitTypes
},
{
type: 'input',
name: 'message',
message: 'Enter commit message:',
validate: (input) => {
const message = input.trim();
if (!message) return 'Message cannot be empty!';
// Basic validation before commitlint
if (message.length < 3) return 'Message too short!';
return true;
}
}
]);
const commitMsg = `${answers.type}: ${answers.message.trim()}`;
// Validate with commitlint
console.log(chalk.blue('\n🔍 Validating commit message...'));
const validation = validateCommitMessage(commitMsg);
if (!validation.isValid) {
console.log(chalk.red('✖ Commitlint validation failed:'));
console.log(chalk.yellow(formatCommitlintError(validation.error)));
// Ask if they want to proceed anyway
const { proceed } = await inquirer.prompt([
{
type: 'confirm',
name: 'proceed',
message: 'Commit message does not follow conventions. Commit anyway?',
default: false
}
]);
if (!proceed) {
console.log(chalk.blue('Commit cancelled.'));
process.exit(0);
}
} else {
console.log(chalk.green('✓ Commit message validated!'));
}
console.log(chalk.green(`\n📝 Generated: "${commitMsg}"`));
// Final confirmation
const { confirmCommit } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmCommit',
message: 'Proceed with commit?',
default: true
}
]);
if (!confirmCommit) {
console.log(chalk.blue('Commit cancelled.'));
process.exit(0);
}
const commitResult = await git.commit(commitMsg);
console.log(chalk.green('✔ Committed successfully!'));
// Show commit summary
if (commitResult.commit) {
console.log(chalk.gray(`Commit hash: ${commitResult.commit.substr(0, 8)}`));
}
return commitResult;
} catch (err) {
console.error(chalk.red('✖ Error:', err.message));
console.log(chalk.gray('Full error:', err));
process.exit(1);
}
}
// Export for testing
module.exports = { run, validateCommitMessage, handleStagingArea };
// Only run if called directly
if (require.main === module) {
run();
}