@abhaydesu/git-pilot 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +157 -0
- package/package.json +25 -0
package/index.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { program } from "commander";
|
|
4
|
+
import { execa, execaCommand } from "execa";
|
|
5
|
+
import axios from "axios";
|
|
6
|
+
import inquirer from "inquirer";
|
|
7
|
+
import chalk from "chalk";
|
|
8
|
+
|
|
9
|
+
import ora from "ora";
|
|
10
|
+
|
|
11
|
+
const API_COMMIT_URL = "http://git-pilot-api.vercel.app/api/commit-message";
|
|
12
|
+
const API_COMMAND_URL ="http://git-pilot-api.vercel.app/api/git-command"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const getStagedDiff = async () => {
|
|
16
|
+
try {
|
|
17
|
+
const { stdout } = await execa("git", ["diff", "--staged"]);
|
|
18
|
+
return stdout;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
console.error(chalk.red("Error getting git diff:"), error?.message || error);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
program
|
|
27
|
+
.name("git-pilot")
|
|
28
|
+
.description("An AI-powered Git assistant in your command line.");
|
|
29
|
+
|
|
30
|
+
program
|
|
31
|
+
.command("commit")
|
|
32
|
+
.description("Generate an AI-powered commit message.")
|
|
33
|
+
.argument(
|
|
34
|
+
"<string>",
|
|
35
|
+
'The user\'s intent for the commit (e.g., "add a new feature")'
|
|
36
|
+
)
|
|
37
|
+
.action(async (intent) => {
|
|
38
|
+
const spinner = ora('Analyzing your staged changes...').start();
|
|
39
|
+
|
|
40
|
+
const diff = await getStagedDiff();
|
|
41
|
+
|
|
42
|
+
if (!diff) {
|
|
43
|
+
spinner.warn("No staged changes found. Please stage your files with `git add`.");
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
spinner.text = 'Generating commit message...';
|
|
49
|
+
|
|
50
|
+
const response = await axios.post(API_COMMIT_URL,
|
|
51
|
+
{ intent, diff},
|
|
52
|
+
{ timeout: 30000 }
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const suggestedMessage = response.data.message;
|
|
56
|
+
|
|
57
|
+
spinner.succeed();
|
|
58
|
+
|
|
59
|
+
console.log(chalk.green("\n--- Suggested Message ---"));
|
|
60
|
+
console.log(chalk.cyan(suggestedMessage));
|
|
61
|
+
console.log(chalk.green("-------------------------\n"));
|
|
62
|
+
|
|
63
|
+
const { confirm } = await inquirer.prompt([
|
|
64
|
+
{
|
|
65
|
+
type: "confirm",
|
|
66
|
+
name: "confirm",
|
|
67
|
+
message: "Commit with this message?",
|
|
68
|
+
default: true,
|
|
69
|
+
},
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
if (confirm) {
|
|
73
|
+
await execa("git", ["commit", "-m", suggestedMessage]);
|
|
74
|
+
console.log(chalk.bgGreen.black("Commit successful!"));
|
|
75
|
+
} else {
|
|
76
|
+
console.log(chalk.red("Commit aborted by user."));
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (spinner && spinner.isSpinning) spinner.fail('An error occurred.');
|
|
80
|
+
if (error.response) {
|
|
81
|
+
console.error(
|
|
82
|
+
chalk.red(
|
|
83
|
+
`API Error: ${error.response.status} - ${error.response.data.error}`
|
|
84
|
+
)
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
console.error(
|
|
88
|
+
chalk.red("An unexpected error occured: ", error.message)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
program
|
|
98
|
+
.command('run')
|
|
99
|
+
.description('Translate natural language into a Git command.')
|
|
100
|
+
.argument('<string>', 'The task you want to perform (e.g. "squash the last 3 commits')
|
|
101
|
+
.action(async (request) => {
|
|
102
|
+
try {
|
|
103
|
+
const spinner = ora('Fetching the right command...').start();
|
|
104
|
+
|
|
105
|
+
const response = await axios.post(
|
|
106
|
+
API_COMMAND_URL,
|
|
107
|
+
{ request },
|
|
108
|
+
{ timeout: 30000 }
|
|
109
|
+
);
|
|
110
|
+
const suggestedCommand = response.data.command;
|
|
111
|
+
|
|
112
|
+
if (suggestedCommand.startsWith('Error:')) {
|
|
113
|
+
spinner.fail(suggestedCommand);
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
spinner.succeed();
|
|
118
|
+
|
|
119
|
+
console.log(chalk.yellow.bold('\nWARNING: The suggested command will be executed in your shell.'));
|
|
120
|
+
console.log(chalk.yellow('Always review commands carefully before confirming.\n'));
|
|
121
|
+
|
|
122
|
+
console.log(chalk.green('\n------ Suggested Command ------'));
|
|
123
|
+
console.log(chalk.cyan(suggestedCommand));
|
|
124
|
+
console.log(chalk.green('-------------------------------\n'));
|
|
125
|
+
|
|
126
|
+
const { confirm } = await inquirer.prompt([
|
|
127
|
+
{
|
|
128
|
+
type: 'confirm',
|
|
129
|
+
name: 'confirm',
|
|
130
|
+
message: 'Execute this command?',
|
|
131
|
+
default: false,
|
|
132
|
+
},
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
if (confirm) {
|
|
136
|
+
await execaCommand(suggestedCommand, {stdio: 'inherit'});
|
|
137
|
+
console.log(chalk.bgGreen.black('Command executed successfully!'));
|
|
138
|
+
} else {
|
|
139
|
+
console.log(chalk.red('Execution aborted by user.'));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (spinner && spinner.isSpinning)
|
|
144
|
+
spinner.fail('An error occurred.');
|
|
145
|
+
|
|
146
|
+
if (error.response) {
|
|
147
|
+
console.error(
|
|
148
|
+
chalk.red(`API Error: ${error.response.status} - ${error.response.data.error || 'No message'}`));
|
|
149
|
+
} else {
|
|
150
|
+
console.error(chalk.red(`An unexpected error occurred: ${error.message}`));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
process.exit(1);
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
program.parse(process.argv);
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@abhaydesu/git-pilot",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"git-pilot": "./index.js"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"axios": "^1.12.2",
|
|
18
|
+
"chalk": "^5.6.2",
|
|
19
|
+
"commander": "^14.0.1",
|
|
20
|
+
"dotenv": "^17.2.2",
|
|
21
|
+
"execa": "^9.6.0",
|
|
22
|
+
"inquirer": "^12.9.6",
|
|
23
|
+
"ora": "^9.0.0"
|
|
24
|
+
}
|
|
25
|
+
}
|