-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommit.js
More file actions
113 lines (102 loc) · 2.66 KB
/
commit.js
File metadata and controls
113 lines (102 loc) · 2.66 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
import generateCommitMessage from './generateCommitMessage.js';
import {execa} from 'execa';
import readline from 'readline';
import React from 'react';
import {Box, render, Text, useApp} from 'ink';
import SelectInput from 'ink-select-input';
import Logo from './logo.js';
async function askForCommitMessage(flags, model) {
const prompt = await generateCommitMessage(flags, model);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const SelectSuggestedCommit = () => {
const {exit} = useApp();
const handleSelect = item => {
if (item.value) {
execa('git', ['commit', '-m', prompt])
.then(() => {
console.log('Changes committed successfully!');
})
.catch(error => {
console.error('Failed to commit changes:', error);
});
} else {
console.log('Changes not committed.');
}
rl.close();
exit();
};
const items = [
{
label: 'No',
value: false,
},
{
label: 'Yes',
value: true,
},
];
return (
<Logo>
<Box flexDirection="column">
<Text>{`Suggested commit message: ${prompt}\nDo you want to proceed?`}</Text>
<SelectInput items={items} onSelect={handleSelect} />
</Box>
</Logo>
);
};
if (prompt) {
render(<SelectSuggestedCommit />);
} else {
console.log('No changes to commit...');
rl.close();
}
}
export async function initGit() {
try {
await execa('git', ['restore', '--staged', '.']);
} catch (error) {
console.error(error);
}
}
// git status to see if there are any changes
// if there's any changes add the first file in the list of changes
let firstFilePath = '';
export async function gitStatus() {
try {
const {stdout: status} = await execa('git', ['status', '--porcelain']);
if (status) {
// get the first file path in the list of changes
const lines = status.split('\n');
const filePaths = lines
.map(line => line.split(' ').slice(2).join(' ').trim())
.filter(filePath => filePath !== '')
.concat(
lines
.filter(line => line.startsWith('??'))
.map(line => line.split(' ').slice(1).join(' ').trim()),
);
// git add the first file in the list of changes
firstFilePath = filePaths[0];
await execa('git', ['add', firstFilePath]);
console.log(`${firstFilePath} has been added to the staging area.`);
} else {
console.log('No changes to commit.');
return false;
}
} catch (error) {
console.error(error);
}
}
// get the diff of the staged changes
export async function gitDiff() {
try {
const {stdout: gitDiff} = await execa('git', ['diff', '--staged']);
return gitDiff;
} catch (error) {
console.error(error);
}
}
export default askForCommitMessage;