'use strict'; const fs = require('fs').promises; const crypto = require('crypto'); // const bhttp = require('bhttp'); const countries = require('./countries.json'); /* async function fetchCountries() { const res = await bhttp.get('http://localhost:3000/api/countries'); if (res.statusCode === 200) { await fs.writeFile('./assets/countries.json', JSON.stringify(res.body, null, 4)); console.log('Done fetching countries!'); return; } console.log(`Failed to fetch countries: ${res.statusCode}`); } */ async function init() { const countryImgs = await fs.readdir('./assets/countries'); const countriesByCode = Object.fromEntries(countries.map((country) => [country.alpha2, country])); const imgCountries = await Promise.all(countryImgs.filter((file) => !['encoded', 'disabled'].includes(file)).map(async (file) => { const code = file.match(/(\w+).png/)[1]; const filename = `${crypto.createHash('md5').update(code).digest('hex').slice(0, 4)}.png`; await fs.copyFile(`./assets/countries/${file}`, `./assets/countries/encoded/${filename}`); return { file: filename, code, ...countriesByCode[code], }; })); await fs.writeFile('./assets/countries-curated.json', JSON.stringify(imgCountries, null, 4)); console.log('Done!'); } init(); // fetchCountries();