generated from acid-info/logos-sites-content-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fetch-content.js
87 lines (73 loc) · 2.85 KB
/
fetch-content.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
const https = require('https');
const fs = require('fs');
const path = require('path');
async function fetchFromGitHub(url, callback) {
https.get(url, { headers: { 'User-Agent': 'Node.js' } }, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
callback(null, JSON.parse(data));
});
}).on('error', (err) => {
callback(err, null);
});
}
async function fetchDirectoryContents(dirUrl, basePath, prefixToRemove) {
fetchFromGitHub(dirUrl, async (err, files) => {
if (err) {
console.error('Error fetching files:', err.message);
return;
}
for (const file of files) {
const relativePath = file.path.replace(new RegExp(`^${prefixToRemove}`), '');
const filePath = path.join(basePath, relativePath);
if (file.type === 'file') {
await downloadAndSaveFile(file.download_url, filePath);
} else if (file.type === 'dir') {
await fetchDirectoryContents(file.url, basePath, prefixToRemove); // Recursive call for subdirectories
}
}
});
}
async function downloadAndSaveFile(url, filePath) {
const fullFilePath = path.join(__dirname, filePath);
https.get(url, (res) => {
const directory = path.dirname(fullFilePath);
// Ensure the directory exists
fs.mkdirSync(directory, { recursive: true });
const fileStream = fs.createWriteStream(fullFilePath);
res.pipe(fileStream);
fileStream.on('finish', () => {
fileStream.close();
console.log('Downloaded and saved:', filePath);
});
}).on('error', (err) => {
console.error('Error downloading file:', err.message);
});
}
const repositories = [
{
baseUrl: 'https://api.github.com/repos/waku-org/nwaku/contents/docs/benchmarks',
baseSavePath: '/docs/research/benchmarks/',
prefixToRemove: "docs/benchmarks/",
categoryFileContent: "{ \"label\": \"Benchmarks\", \"collapsed\": false }"
},
{
baseUrl: 'https://api.github.com/repos/waku-org/research/contents/docs',
baseSavePath: '/docs/research/research-and-studies/',
prefixToRemove: "docs/",
categoryFileContent: "{ \"label\": \"Research and Studies\", \"collapsed\": false }"
}
];
repositories.forEach(repo => {
fs.rmSync(path.join(__dirname, repo.baseSavePath), { recursive: true, force: true });
});
repositories.forEach(repo => {
fetchDirectoryContents(repo.baseUrl, repo.baseSavePath, repo.prefixToRemove, repo.categoryFileContent).then(() => {
const dir = path.join(__dirname, repo.baseSavePath);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, "_category_.json"), repo.categoryFileContent);
});
});