-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
213 lines (202 loc) · 5.85 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
import axios from "axios";
import inquirer from "inquirer";
import chalk from "chalk";
import { exec } from "node:child_process";
// Fetch repositories based on the search term
async function fetchRepos (searchTerm) {
try {
const response = await axios.get(
`https://api.github.com/search/repositories?q=${searchTerm}`,
);
return {
entries: response.data.items,
length: response.data.total_count,
};
} catch (error) {
console.log(
chalk.bgBlueBright("Unexpected Error occured while fetching repos..."),
);
process.exit(1);
}
}
// Render a single repository
function renderRepo(repo) {
const { name, description, html_url } = repo;
console.log(
chalk.bgYellow(chalk.black("repo name :")),
"\t",
chalk.cyanBright(name),
"\n",
);
console.log(
chalk.bgBlueBright(chalk.black("Description :")),
"\t",
chalk.bold(description),
"\n",
);
console.log(
chalk.bgGreen(chalk.black("URL :")),
"\t",
chalk.yellow(chalk.underline(html_url)),
"\n",
);
}
// Prompt the user to select a repository from the list
async function promptRepoSelection(repos) {
const choices = repos.map((repo) => ({ name: repo.full_name, value: repo }));
return await inquirer.prompt([
{
type: "rawlist",
name: "selectedRepo",
message: "Select a repository:",
loop: false,
choices,
},
]);
}
// Fetch the contents of the selected repository as an object
// When response statusText is 'OK', the content object will be `fetchRepoContentsResponse().data`
// If response status code is >= 400, the `fetchRepoContentsResponse().data` will be error.response.data
async function fetchRepoContentsResponse(repoFullName) {
try {
const response = await axios.get(
`https://api.github.com/repos/${repoFullName}/contents`,
);
if (response.statusText === "OK") {
return {
data: response.data,
status: Number(response.status),
};
}
} catch (error) {
console.log(
chalk.bgRedBright(chalk.black(`${error.response.data.message}\n`)),
);
return {
data: error.response.data,
status: Number(error.response.status),
};
}
}
// Render the repository contents as a folder structure
function renderRepoContents(contents, indent = " ") {
try {
contents.forEach((item) => {
if (item.type === "dir") {
console.log(
`${indent}`,
chalk.bgCyanBright(chalk.black(`${item.name}`)),
chalk.blueBright("/"),
);
/* axios.get(item.url).then((response) => {
renderRepoContents(response.data, indent + " ");
}); */
} else {
console.log(`${indent}${item.name}`);
}
});
} catch (error) {
console.error(
chalk.bgBlue("Unexpected Error occured while rendering contents"),
);
}
}
const main = async () => {
// Prompt the user for a search term
const promptUser = await inquirer.prompt([
{
type: "input",
name: "searchTerm",
message: chalk.cyan("Enter the search term:"),
},
]);
const userInput = promptUser.searchTerm;
const repos = await fetchRepos(userInput);
if (repos.length < 1) {
console.log(`${chalk.red(`${repos.length} Repositories found!`)} `);
console.log("Nothing to show");
process.exit(1);
}
const { selectedRepo } = await promptRepoSelection(repos.entries);
renderRepo(selectedRepo);
const askUser = await inquirer.prompt([
{
type: "input",
name: "want",
message: `${chalk.cyan(`view the repository contents?`)} (${chalk.green('y')}/${chalk.red('n')}) [default=${chalk.red('n')}] `,
},
]);
if (askUser.want === "yes" || askUser.want === "y" || askUser.want === "Y") {
const repoContentsResponse = await fetchRepoContentsResponse(
selectedRepo.full_name,
);
if (repoContentsResponse.status < 400) {
console.log(chalk.green(`Contents of ${selectedRepo.full_name}:\n`));
renderRepoContents(repoContentsResponse.data);
} else {
console.log(
`request returned ${repoContentsResponse.status} status response.\n`,
);
}
}
const askAgain = await inquirer.prompt([
{
type: "input",
name: "want",
message: `${chalk.cyan(`clone the repository?`)} (${chalk.green('y')}/${chalk.red('n')}) [default=${chalk.red('n')}] `,
},
]);
if (
askAgain.want === "yes" ||
askAgain.want === "y" ||
askAgain.want === "Y"
) {
const lastQuestion = await inquirer.prompt([
{
type: "input",
name: "choice",
message: `${chalk.cyan('clone into a specific directory?')} (${chalk.green('y')}/${chalk.red('n')}) [default=${chalk.red('n')}] `,
},
]);
if (
lastQuestion.choice === "yes" ||
lastQuestion.choice === "Y" ||
lastQuestion.choice === "y"
) {
const directoryName = await inquirer.prompt([
{
type: "input",
name: "input",
message:
chalk.cyan(`Enter the directory for cloning ${chalk.yellow('(WARNING: directory must NOT ALREADY exist!)')} -> `),
},
]);
console.log(chalk.bgMagentaBright(chalk.black("Cloning Initaited!\n")));
exec(
`git clone https://github.com/${selectedRepo.full_name}.git ${directoryName.input}`,
(error, stdout, stderr) => {
if (error) {
console.log(chalk.red(error));
process.exit(1);
}
console.log(stdout);
},
);
} else {
console.log(chalk.bgMagentaBright(chalk.black("Cloning Initaited!\n")));
exec(
`git clone https://github.com/${selectedRepo.full_name}.git`,
(error, stdout, stderr) => {
if (error) {
console.log(chalk.red(error));
process.exit(1);
} else {
console.log(stdout);
}
},
);
}
}
};
main();