167 lines
4.7 KiB
JavaScript
167 lines
4.7 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const https = require('https');
|
|
|
|
// Configuration
|
|
const FILES = [
|
|
{
|
|
name: '39f2x83-products.csv',
|
|
url: 'https://feeds.acherryontop.com/39f2x83-products.csv'
|
|
},
|
|
{
|
|
name: '39f2x83-orders.csv',
|
|
url: 'https://feeds.acherryontop.com/39f2x83-orders.csv'
|
|
},
|
|
{
|
|
name: '39f2x83-purchase_orders.csv',
|
|
url: 'https://feeds.acherryontop.com/39f2x83-purchase_orders.csv'
|
|
}
|
|
];
|
|
|
|
const CSV_DIR = path.join(__dirname, '..', 'csv');
|
|
|
|
// Ensure CSV directory exists
|
|
if (!fs.existsSync(CSV_DIR)) {
|
|
fs.mkdirSync(CSV_DIR, { recursive: true });
|
|
}
|
|
|
|
// Function to download a file
|
|
function downloadFile(url, filePath) {
|
|
return new Promise((resolve, reject) => {
|
|
const file = fs.createWriteStream(filePath);
|
|
|
|
https.get(url, response => {
|
|
if (response.statusCode !== 200) {
|
|
reject(new Error(`Failed to download: ${response.statusCode} ${response.statusMessage}`));
|
|
return;
|
|
}
|
|
|
|
const totalSize = parseInt(response.headers['content-length'], 10);
|
|
let downloadedSize = 0;
|
|
let lastProgressUpdate = Date.now();
|
|
const startTime = Date.now();
|
|
|
|
response.on('data', chunk => {
|
|
downloadedSize += chunk.length;
|
|
const now = Date.now();
|
|
// Update progress at most every 100ms to avoid console flooding
|
|
if (now - lastProgressUpdate > 100) {
|
|
const elapsed = (now - startTime) / 1000;
|
|
const rate = downloadedSize / elapsed;
|
|
const remaining = (totalSize - downloadedSize) / rate;
|
|
|
|
console.log(JSON.stringify({
|
|
status: 'running',
|
|
operation: `Downloading ${path.basename(filePath)}`,
|
|
current: downloadedSize,
|
|
total: totalSize,
|
|
rate: (rate / 1024 / 1024).toFixed(2), // MB/s
|
|
elapsed: formatDuration(elapsed),
|
|
remaining: formatDuration(remaining),
|
|
percentage: ((downloadedSize / totalSize) * 100).toFixed(1)
|
|
}));
|
|
lastProgressUpdate = now;
|
|
}
|
|
});
|
|
|
|
response.pipe(file);
|
|
|
|
file.on('finish', () => {
|
|
console.log(JSON.stringify({
|
|
status: 'running',
|
|
operation: `Completed ${path.basename(filePath)}`,
|
|
current: totalSize,
|
|
total: totalSize,
|
|
percentage: '100'
|
|
}));
|
|
file.close();
|
|
resolve();
|
|
});
|
|
}).on('error', error => {
|
|
fs.unlink(filePath, () => {}); // Delete the file if download failed
|
|
reject(error);
|
|
});
|
|
|
|
file.on('error', error => {
|
|
fs.unlink(filePath, () => {}); // Delete the file if there was an error
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
// Helper function to format duration
|
|
function formatDuration(seconds) {
|
|
if (seconds < 60) return `${Math.round(seconds)}s`;
|
|
const minutes = Math.floor(seconds / 60);
|
|
seconds = Math.round(seconds % 60);
|
|
return `${minutes}m ${seconds}s`;
|
|
}
|
|
|
|
// Main function to update all files
|
|
async function updateFiles() {
|
|
console.log(JSON.stringify({
|
|
status: 'running',
|
|
operation: 'Starting CSV file updates',
|
|
total: FILES.length,
|
|
current: 0
|
|
}));
|
|
|
|
for (let i = 0; i < FILES.length; i++) {
|
|
const file = FILES[i];
|
|
const filePath = path.join(CSV_DIR, file.name);
|
|
|
|
try {
|
|
// Delete existing file if it exists
|
|
if (fs.existsSync(filePath)) {
|
|
console.log(JSON.stringify({
|
|
status: 'running',
|
|
operation: `Removing existing file: ${file.name}`,
|
|
current: i,
|
|
total: FILES.length,
|
|
percentage: ((i / FILES.length) * 100).toFixed(1)
|
|
}));
|
|
fs.unlinkSync(filePath);
|
|
}
|
|
|
|
// Download new file
|
|
console.log(JSON.stringify({
|
|
status: 'running',
|
|
operation: `Starting download: ${file.name}`,
|
|
current: i,
|
|
total: FILES.length,
|
|
percentage: ((i / FILES.length) * 100).toFixed(1)
|
|
}));
|
|
await downloadFile(file.url, filePath);
|
|
console.log(JSON.stringify({
|
|
status: 'running',
|
|
operation: `Successfully updated ${file.name}`,
|
|
current: i + 1,
|
|
total: FILES.length,
|
|
percentage: (((i + 1) / FILES.length) * 100).toFixed(1)
|
|
}));
|
|
} catch (error) {
|
|
console.error(JSON.stringify({
|
|
status: 'error',
|
|
operation: `Error updating ${file.name}`,
|
|
error: error.message
|
|
}));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
console.log(JSON.stringify({
|
|
status: 'complete',
|
|
operation: 'CSV file update complete',
|
|
current: FILES.length,
|
|
total: FILES.length,
|
|
percentage: '100'
|
|
}));
|
|
}
|
|
|
|
// Run the update
|
|
updateFiles().catch(error => {
|
|
console.error(JSON.stringify({
|
|
error: `Update failed: ${error.message}`
|
|
}));
|
|
process.exit(1);
|
|
});
|