97 lines
2.5 KiB
JavaScript
97 lines
2.5 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;
|
|
|
|
response.on('data', chunk => {
|
|
downloadedSize += chunk.length;
|
|
const progress = (downloadedSize / totalSize * 100).toFixed(2);
|
|
process.stdout.write(`\rDownloading ${path.basename(filePath)}: ${progress}%`);
|
|
});
|
|
|
|
response.pipe(file);
|
|
|
|
file.on('finish', () => {
|
|
process.stdout.write('\n');
|
|
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);
|
|
});
|
|
});
|
|
}
|
|
|
|
// Main function to update all files
|
|
async function updateFiles() {
|
|
console.log('Starting CSV file updates...');
|
|
|
|
for (const file of FILES) {
|
|
const filePath = path.join(CSV_DIR, file.name);
|
|
|
|
try {
|
|
// Delete existing file if it exists
|
|
if (fs.existsSync(filePath)) {
|
|
console.log(`Removing existing file: ${file.name}`);
|
|
fs.unlinkSync(filePath);
|
|
}
|
|
|
|
// Download new file
|
|
console.log(`Downloading ${file.name}...`);
|
|
await downloadFile(file.url, filePath);
|
|
console.log(`Successfully updated ${file.name}`);
|
|
} catch (error) {
|
|
console.error(`Error updating ${file.name}:`, error.message);
|
|
}
|
|
}
|
|
|
|
console.log('CSV file update complete!');
|
|
}
|
|
|
|
// Run the update
|
|
updateFiles().catch(error => {
|
|
console.error('Update failed:', error);
|
|
process.exit(1);
|
|
});
|