forked from DeepSourceCorp/good-first-issue
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync.js
71 lines (66 loc) · 2.2 KB
/
sync.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
import fs from 'fs';
import path from 'path';
import https from 'https';
import { put, list } from '@vercel/blob';
const dirToSync = './data';
const filesToSync = ['generated.json', 'tags.json'];
/**
* Downloads a file from a given URL to a specified destination with retries.
* @param {string} url - The URL of the file to download.
* @param {string} dest - The destination where the file should be saved.
* @param {number} retries - Number of retry attempts.
* @return {Promise} Resolves when the file is downloaded successfully.
*/
async function downloadFile(url, dest, retries = 3) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
await new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest, { flags: 'w' });
https
.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to download file: status ${response.statusCode}`));
return;
}
response.pipe(file).on('finish', resolve).on('error', reject);
})
.on('error', reject);
});
console.log(`Downloaded ${url} to ${dest}`);
return;
} catch (error) {
console.error(`Attempt ${attempt} failed: ${error.message}`);
if (attempt === retries) throw new Error(`Failed to download ${url} after ${retries} attempts`);
}
}
}
/**
* Sync files to Vercel Blob Storage.
*/
async function syncFiles() {
try {
const existingFiles = await list();
console.log('Existing files:', existingFiles);
await Promise.all(
filesToSync.map(async (file) => {
const filePath = path.join(dirToSync, file);
if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath);
await put(file, fileContent);
console.log(`Synced ${file} to blob storage.`);
} else {
console.warn(`File ${filePath} does not exist.`);
}
})
);
} catch (error) {
console.error('Error syncing files:', error);
}
}
// Example usage
(async () => {
const url = 'https://example.com/data.json';
const dest = path.join(dirToSync, 'data.json');
await downloadFile(url, dest);
await syncFiles();
})();