-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·72 lines (60 loc) · 2.39 KB
/
index.js
File metadata and controls
executable file
·72 lines (60 loc) · 2.39 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
#!/usr/bin/env node
// Imports
import {Command} from 'commander';
import simpleGit from 'simple-git';
import inquirer from 'inquirer';
import path from 'path';
import process from 'process';
import "colors";
import dotenv from 'dotenv';
dotenv.config();
// Path Control
// __dirname is not available in es modules, so derive it
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { askUser } from './utils/inquirer.js';
import {actionsMap} from './actions/actionsMap.js';
import {generateAICommitMessage} from './utils/commitHelper.js';
// full URL of the current module file
const __filename = fileURLToPath(import.meta.url);
// getting absolute path of the dir containing this file.
const __dirname = dirname(__filename);
// cwd is the current working directory where the command is run
const defPath = process.cwd();
const program = new Command();
const git = simpleGit();
const prompt = inquirer.prompt;
console.log(`Current project directory: ${defPath.yellow}`);
export const gitHelper = async (defaultRemoteRepo = "origin") => {
try {
const currentBranch = (await git.branch()).current;
const {suggestedMsgAI} = await generateAICommitMessage({git});
const answers = await askUser(git, prompt, currentBranch, suggestedMsgAI);
const { action, dir, targetBranch, commitMsg, confirmStatus} = answers;
if(confirmStatus) {
console.log(`✅ Action confirmed: ${action}`);
} else {
console.log('❌ Action cancelled by user.');
return;
}
// Detect current branch automatically
const branch = targetBranch || currentBranch;
const targetPath = action === "commit_changes" ? path.resolve(defPath, dir) : defPath;
// selecting action to execute
const selectedAction = actionsMap[action];
if(!selectedAction) throw new Error(`⚠️ Unknown action: ${action}`);
await selectedAction({git, branch, commitMsg, defaultRemoteRepo, targetPath})
} catch (error) {
console.error('❌ Git operation failed:', error.message);
}
}
gitHelper();
// pushAll
//
// every program. should be a separate function for specific purpose
// Purpose -simplify git operations
// 1. commit and push all changes
// 2. create and push new branch
// Issues on the way -
// 1. Adjust the path for shebang
// 2. Would be nice to add auto-commit with openai, and manual option too