-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
36 lines (33 loc) · 1.3 KB
/
test.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
const axios = require('axios');
async function fetchFilesFromGithub(owner, repo, path = '', branch = 'master') {
let filesList = [];
try {
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}?ref=${branch}`;
const response = await axios.get(url, {
headers: {
Accept: 'application/vnd.github.v3+json',
}
});
if (response.status === 200) {
const files = response.data;
for (const file of files) {
if (file.type === 'file') {
// console.log(file);
filesList.push({ content: ((await axios.get(file.git_url)).content).toString("base64"), url: file.download_url });
} else if (file.type === 'dir') {
await fetchFilesFromGithub(owner, repo, file.path, branch);
}
}
return filesList;
} else {
console.error(`Failed to fetch files. Status code: ${response.status}`);
}
} catch (error) {
console.error('Error fetching files:', error.message);
}
}
// Example usage:
const owner = 'ganeshkbhat';
const repo = 'require-urls';
const branch = 'master';
fetchFilesFromGithub(owner, repo, '', branch);