-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy-git-add.mjs
executable file
·103 lines (92 loc) · 3.4 KB
/
lazy-git-add.mjs
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
#!/usr/bin/env node
import inquirer from 'inquirer';
import { execSync } from 'child_process';
// Function to get the current git status in a concise format
function getGitStatus() {
try {
const statusOutput = execSync('git status --porcelain').toString();
return statusOutput.split('\n').filter(Boolean); // Split by newlines and filter out empty lines
} catch (error) {
console.error('Error getting git status:', error);
process.exit(1);
}
}
// Function to get the list of currently staged files
function getStagedFiles() {
try {
const stagedOutput = execSync('git diff --cached --name-only').toString();
return stagedOutput.split('\n').filter(Boolean); // Split by newlines and filter out empty lines
} catch (error) {
console.error('Error getting staged files:', error);
process.exit(1);
}
}
// Function to print the current git status in a readable format
function printGitStatus() {
try {
console.log('\nCurrent Git Status:');
const status = execSync('git status').toString();
console.log(status);
} catch (error) {
console.error('Error printing git status:', error);
process.exit(1);
}
}
// Main function to run the interactive prompt and handle staging/unstaging
async function run() {
try {
const statusLines = getGitStatus(); // Get the list of files with their status
const stagedFiles = getStagedFiles(); // Get the list of currently staged files
// Prepare choices for the inquirer prompt
const choices = statusLines.map(line => {
const status = line.substring(0, 2).trim(); // Extract the status code
const filename = line.substring(3).trim(); // Extract the filename
const label = `${status === '??' ? 'Untracked' : 'Modified'}: ${filename}`;
return {
name: label, // Display label for the prompt
value: filename, // Value to return when selected
short: filename, // Short name to display in the prompt
checked: stagedFiles.includes(filename) // Check if the file is already staged
};
});
// Prompt the user to select files to stage
const answers = await inquirer.prompt([
{
type: 'checkbox',
message: 'Select files to stage (press Control + C to exit):',
name: 'filesToStage',
choices,
pageSize: 15 // Adjust the number of choices displayed at once
}
]);
const selectedFiles = answers.filesToStage; // Get the list of files selected for staging
if (selectedFiles.length) {
// Stage the selected files
try {
const addCommand = `git add ${selectedFiles.join(' ')}`;
execSync(addCommand);
console.log('Selected files have been staged.');
} catch (error) {
console.error('Error staging files:', error);
}
} else {
console.log('No files were selected.');
}
// Unstage files that were not selected
const filesToUnstage = stagedFiles.filter(file => !selectedFiles.includes(file));
if (filesToUnstage.length) {
try {
const resetCommand = `git reset ${filesToUnstage.join(' ')}`;
execSync(resetCommand);
console.log('Unselected files have been unstaged.');
} catch (error) {
console.error('Error unstaging files:', error);
}
}
// Display the current git status after staging/unstaging files
printGitStatus();
} catch (error) {
console.error('Error running script:', error);
}
}
run();