-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgit.js
158 lines (136 loc) · 4.24 KB
/
mgit.js
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const os = require('os');
const inquirer = require('inquirer');
const chalk = require('chalk');
const { spawnSync } = require('child_process');
/**
* Displays usage instructions.
*/
function displayUsage() {
console.log('Usage: node mgit.js [-all] <command> [args...]');
console.log('Commands:');
console.log(' git <args...>');
console.log(' gh <args...>');
console.log('Examples:');
console.log(' node mgit.js git status');
console.log(' node mgit.js gh pr list');
console.log(' node mgit.js -all git pull origin main');
}
const reposFile = path.join(os.homedir(), '.mgit-repos');
let reposData;
try {
const data = fs.readFileSync(reposFile, 'utf8');
reposData = JSON.parse(data);
} catch (err) {
console.error(chalk.red('Error: Failed to load .mgit-repos file.'));
console.error('Please ensure that .mgit-repos exists in your home directory and is correctly formatted.');
process.exit(1);
}
if (!reposData.repositories || !Array.isArray(reposData.repositories)) {
console.error(chalk.red('Error: .mgit-repos file is incorrectly formatted.'));
console.error('It should contain a "repositories" array with repository definitions.');
process.exit(1);
}
const args = process.argv.slice(2);
if (args.length === 0) {
console.error(chalk.red('Error: No command provided.'));
displayUsage();
process.exit(1);
}
let isAll = false;
if (args[0] === '-all') {
isAll = true;
args.shift();
}
// Extract the command and its arguments
const [cmd, ...cmdArgs] = args;
// Validate that only 'git' or 'gh' commands are allowed
if (!['git', 'gh'].includes(cmd)) {
console.error(chalk.red(`Error: Unsupported command "${cmd}".`));
console.error('Only "git" and "gh" commands are allowed for safety.');
displayUsage();
process.exit(1);
}
(async () => {
let repositoriesToUse;
if (cmd === 'cd') {
await handleCd(cmdArgs);
return;
}
if (isAll) {
repositoriesToUse = reposData.repositories;
} else {
repositoriesToUse = await selectRepositories(reposData.repositories);
}
if (repositoriesToUse.length === 0) {
console.error(chalk.red('Error: No repositories selected.'));
process.exit(1);
}
await executeCommandInRepositories(repositoriesToUse, cmd, cmdArgs);
})();
async function selectRepositories(repositories) {
const choices = repositories.map((repo, index) => ({
name: repo.name,
value: index,
}));
const answers = await inquirer.prompt([
{
type: 'checkbox',
name: 'selectedRepos',
message: 'Select repositories:',
choices: choices,
},
]);
return answers.selectedRepos.map((index) => repositories[index]);
}
async function executeCommandInRepositories(repositories, command, args) {
for (const repo of repositories) {
console.log(chalk.blue.bold(`\n===== Repository: ${repo.name} =====`));
try {
// Execute the command with preserved color output
const result = spawnSync(command, args, {
cwd: repo.path,
stdio: 'inherit',
shell: false, // Disable shell to prevent shell-specific security issues
});
if (result.error) {
console.error(chalk.red(`Error executing command in ${repo.name}: ${result.error.message}`));
} else if (result.status !== 0) {
console.error(chalk.red(`Command exited with code ${result.status} in ${repo.name}`));
}
} catch (err) {
console.error(chalk.red(`Error executing command in ${repo.name}: ${err.message}`));
}
}
}
async function handleCd(args) {
const key = args[0];
let repo;
if (key) {
repo = reposData.repositories.find(
(r) => r.name.toLowerCase() === key.toLowerCase()
);
if (!repo) {
console.error(chalk.red(`Error: Repository with key "${key}" not found.`));
process.exit(1);
}
} else {
const choices = reposData.repositories.map((repo, index) => ({
name: repo.name,
value: index,
}));
const answers = await inquirer.prompt([
{
type: 'list',
name: 'selectedRepo',
message: 'Select a repository to cd into:',
choices: choices,
},
]);
repo = reposData.repositories[answers.selectedRepo];
}
// Output the repository path
console.log(repo.path);
}