-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli-wrapper.js
More file actions
executable file
Β·200 lines (170 loc) Β· 6 KB
/
Copy pathcli-wrapper.js
File metadata and controls
executable file
Β·200 lines (170 loc) Β· 6 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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import os from 'os';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import chalk from 'chalk';
import { getStagedDiff, commitChanges } from './git.js';
import { generateCommitMessage } from './ai.js';
import { confirmCommitMessage, getManualCommitMessage } from './prompt.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load environment variables
dotenv.config();
/**
* Get VS Code settings for Commity extension
* @returns {Object|null} Settings object or null if not found
*/
function getVSCodeSettings() {
try {
const platform = os.platform();
let settingsPath;
if (platform === 'darwin') {
// macOS
settingsPath = path.join(os.homedir(), 'Library', 'Application Support', 'Code', 'User', 'settings.json');
} else if (platform === 'win32') {
// Windows
settingsPath = path.join(os.homedir(), 'AppData', 'Roaming', 'Code', 'User', 'settings.json');
} else {
// Linux
settingsPath = path.join(os.homedir(), '.config', 'Code', 'User', 'settings.json');
}
if (fs.existsSync(settingsPath)) {
const settingsContent = fs.readFileSync(settingsPath, 'utf8');
const settings = JSON.parse(settingsContent);
if (settings.commity) {
return settings.commity;
}
}
return null;
} catch (error) {
console.log(chalk.gray('Could not read VS Code settings:', error.message));
return null;
}
}
/**
* Get API key from various sources
* @returns {string|null} API key or null if not found
*/
function getApiKey() {
// First, try environment variable
if (process.env.OPENAI_API_KEY && process.env.OPENAI_API_KEY !== 'sk-your-openai-api-key-here') {
return process.env.OPENAI_API_KEY;
}
// Then, try VS Code settings
const vsCodeSettings = getVSCodeSettings();
if (vsCodeSettings && vsCodeSettings.openaiApiKey) {
return vsCodeSettings.openaiApiKey;
}
return null;
}
/**
* Get model from various sources
* @returns {string} Model name
*/
function getModel() {
// First, try environment variable
if (process.env.OPENAI_MODEL) {
return process.env.OPENAI_MODEL;
}
// Then, try VS Code settings
const vsCodeSettings = getVSCodeSettings();
if (vsCodeSettings && vsCodeSettings.openaiModel) {
return vsCodeSettings.openaiModel;
}
// Default
return 'gpt-4o-mini';
}
/**
* Check if emojis are enabled
* @returns {boolean} Whether emojis are enabled
*/
function isEmojisEnabled() {
// First, try environment variable
if (process.env.COMMITY_ENABLE_EMOJIS !== undefined) {
return process.env.COMMITY_ENABLE_EMOJIS === 'true';
}
// Then, try VS Code settings
const vsCodeSettings = getVSCodeSettings();
if (vsCodeSettings && vsCodeSettings.enableEmojis !== undefined) {
return vsCodeSettings.enableEmojis;
}
// Default
return true;
}
async function main() {
try {
console.log(chalk.blue('π€ Commity - AI Commit Tool\n'));
// Get API key from various sources
const apiKey = getApiKey();
if (!apiKey) {
console.log(chalk.red('β OpenAI API key not found.'));
console.log(chalk.yellow('Please configure your API key in one of these ways:'));
console.log(chalk.gray(''));
console.log(chalk.gray('1. VS Code Extension (Recommended):'));
console.log(chalk.gray(' - Install the Commity VS Code extension'));
console.log(chalk.gray(' - Go to Settings β Commity β OpenAI API Key'));
console.log(chalk.gray(' - Enter your API key'));
console.log(chalk.gray(''));
console.log(chalk.gray('2. Environment Variable:'));
console.log(chalk.gray(' - Set OPENAI_API_KEY in your .env file'));
console.log(chalk.gray(''));
console.log(chalk.gray('3. Get your API key from: https://platform.openai.com/api-keys\n'));
process.exit(1);
}
// Set environment variables for the AI module
process.env.OPENAI_API_KEY = apiKey;
process.env.OPENAI_MODEL = getModel();
// Show source of configuration
const vsCodeSettings = getVSCodeSettings();
if (vsCodeSettings) {
console.log(chalk.gray('π Using VS Code extension settings'));
} else {
console.log(chalk.gray('π Using environment variables'));
}
console.log('');
// Get staged changes
console.log(chalk.gray('π Reading staged changes...'));
const diff = await getStagedDiff();
if (!diff || diff.trim() === '') {
console.log(chalk.yellow('β οΈ No staged changes found.'));
console.log(chalk.gray('Use "git add <files>" to stage changes first.\n'));
process.exit(0);
}
// Generate commit message using AI
console.log(chalk.gray('π§ Generating commit message with AI...'));
const aiMessage = await generateCommitMessage(diff);
if (!aiMessage) {
console.log(chalk.red('β Failed to generate commit message.'));
process.exit(1);
}
// Display the suggested message
console.log(chalk.green('\n㪠Suggested commit message:'));
console.log(chalk.white(`"${aiMessage}"\n`));
// Ask for confirmation
const userChoice = await confirmCommitMessage();
if (userChoice === 'yes') {
// Commit with AI-generated message
await commitChanges(aiMessage);
console.log(chalk.green('β
Committed successfully!'));
} else if (userChoice === 'edit') {
// Allow manual editing
const manualMessage = await getManualCommitMessage(aiMessage);
if (manualMessage && manualMessage.trim()) {
await commitChanges(manualMessage);
console.log(chalk.green('β
Committed with your custom message!'));
} else {
console.log(chalk.yellow('β οΈ Commit cancelled - no message provided.'));
}
} else {
// User chose to cancel
console.log(chalk.yellow('β οΈ Commit cancelled.'));
}
} catch (error) {
console.error(chalk.red('β Error:'), error.message);
process.exit(1);
}
}
// Run the CLI tool
main();