-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
142 lines (116 loc) · 4.57 KB
/
index.ts
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
#!/usr/bin/env node
import inquirer from 'inquirer';
import inquirerFileTreeSelection from 'inquirer-file-tree-selection-prompt';
import { CliParser } from './src/cli-parser.js';
import { ConfigStore } from './src/config.js';
import { FeedbackServer } from './src/feedback-server.js';
import { AutoReviewer } from './src/index.js';
import { Utils } from './src/utils.js';
import { ReviewOptions } from './types';
import chalk from 'chalk';
inquirer.registerPrompt('file-tree-selection', inquirerFileTreeSelection);
const selectFiles = async () => {
try {
enum ReviewMode {
FILES_TO_BE_REVIEWED = "FILES_TO_BE_REVIEWED",
REVIEW_ENTIRE_CODEBASE = "REVIEW_ENTIRE_CODEBASE",
REVIEW_AGAINST_ANOTHER_BRANCH = "REVIEW_AGAINST_ANOTHER_BRANCH"
}
const selectMode = await inquirer.prompt([
{
type: "list",
name: 'reviewMode',
message: 'Select a code review mode',
choices: [
{ name: 'Select files to be reviewed', value: ReviewMode.FILES_TO_BE_REVIEWED },
{ name: 'Review entire code base', value: ReviewMode.REVIEW_ENTIRE_CODEBASE },
{ name: 'Review this branch against another branch (coming soon)', value: ReviewMode.REVIEW_AGAINST_ANOTHER_BRANCH, disabled: true }
],
}
])
if (selectMode.reviewMode === ReviewMode.FILES_TO_BE_REVIEWED) {
const { files } = await inquirer.prompt([
{
type: 'file-tree-selection',
name: 'files',
message: 'Select files',
root: '.',
multiple: true,
ignore: ['node_modules/*', '.git/*', 'dist/*', 'build/*', 'coverage/*', '.vscode/*', '.gitignore'],
},
]);
return startCodeReviewProcess({
files
})
}
if (selectMode.reviewMode === ReviewMode.REVIEW_AGAINST_ANOTHER_BRANCH) {
//TODO: you can want to view agains another branch for a specific file
const { branch } = await inquirer.prompt([
{
type: 'input',
name: 'branch',
message: 'Enter branch name',
},
]);
return startCodeReviewProcess({
branch
})
}
return startCodeReviewProcess()
// Perform the desired action with the selected files
} catch (error) {
console.error('Error:', error);
}
};
/**
* Displays a welcome message to the user
*/
const showWelcomeMessage = () => {
const welcomeText = `
=======================================================
Welcome to AutoReviewer - Your Code Review Helper
=======================================================
1. AutoReviewer will review and analyze your code
2. AutoReviewer will provide detailed feedback
3. With our git integration AutoReviewer will
automatically help you carry out code reviews faster
-------------------------------------------------------
`;
Utils.log(chalk.greenBright.bold(welcomeText));
}
showWelcomeMessage();
const autoReviewer = new AutoReviewer();
const config = new ConfigStore();
const feedbackServer = new FeedbackServer();
new CliParser(process.argv).executeCommand();
const startCodeReviewProcess = async (option?: ReviewOptions) => {
await autoReviewer.codeReview(option)
// const score = await autoReviewer.getScore();
// Utils.sleep(1000);
Utils.log(chalk.greenBright.bold('Code Review Completed successfully \n'));
// Utils.log(chalk.greenBright.bold(`Your code score is: ${score} / 10`));
Utils.log(chalk.greenBright.bold(`Please visit http://localhost:3123 to view your feedback`));
return feedbackServer.init();
}
if (config.get('openai_api_key')) {
(async () => {
try {
await selectFiles();
} catch (error: any) {
Utils.log(chalk.red.bold(`Error occurred: ${error.message}`));
}
})();
} else {
(async () => {
try {
await config.promptUserForApiKey()
Utils.sleep(1000);
Utils.log(chalk.greenBright.bold(`API Key saved successfully ☑️
Please run the command again to start the code review
`));
await selectFiles()
} catch (error: any) {
Utils.log(chalk.red.bold(`Error occurred: ${error.message}`));
}
})();
}