cindy-ai-chatbot
Version:
An AI-powered chatbot component for React applications
73 lines (64 loc) • 2.02 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import https from 'https';
// Get the directory name in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Create fonts directory if it doesn't exist
const fontsDir = path.resolve(__dirname, '../src/fonts');
if (!fs.existsSync(fontsDir)) {
fs.mkdirSync(fontsDir, { recursive: true });
}
// URLs for Inter variable fonts
const interFonts = [
{
url: 'https://rsms.me/inter/font-files/InterVariable.woff2',
filename: 'InterVariable.woff2'
},
{
url: 'https://rsms.me/inter/font-files/InterVariable-Italic.woff2',
filename: 'InterVariable-Italic.woff2'
}
];
// Download function
function downloadFile(url, destination) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(destination);
https.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to download ${url}: ${response.statusCode}`));
return;
}
response.pipe(file);
file.on('finish', () => {
file.close();
console.log(`Downloaded ${url} to ${destination}`);
resolve();
});
}).on('error', (err) => {
fs.unlink(destination, () => {}); // Delete the file if there's an error
reject(err);
});
});
}
// Download all fonts
async function downloadAllFonts() {
for (const font of interFonts) {
const destination = path.join(fontsDir, font.filename);
if (!fs.existsSync(destination)) {
try {
await downloadFile(font.url, destination);
} catch (error) {
console.error(`Error downloading ${font.filename}:`, error);
}
} else {
console.log(`${font.filename} already exists, skipping download.`);
}
}
}
downloadAllFonts().then(() => {
console.log('All Inter fonts downloaded successfully!');
}).catch(error => {
console.error('Error downloading fonts:', error);
});