'use strict'; const fs = require('fs').promises; const crypto = require('crypto'); const { argv } = require('yargs'); // const bhttp = require('bhttp'); const countries = require('./countries.json'); const states = require('./states-us.json'); const data = { countries, states, flags: countries, }; /* 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 type = argv.type || 'countries'; const countryImgs = await fs.readdir(`./assets/${type}`); const countriesByCode = Object.fromEntries(data[type].map((country) => [country.alpha2, country])); const imgCountries = await Promise.all(countryImgs.map(async (file) => { const code = file.match(/(\w+).png/)?.[1]; if (!code || !countriesByCode[code]) { return null; } const filename = `${crypto.createHash('md5').update(code).digest('hex').slice(0, 4)}.png`; await fs.copyFile(`./assets/${type}/${file}`, `./assets/${type}/encoded/${filename}`); return { file: filename, code, ...countriesByCode[code], }; }).filter(Boolean)); await fs.writeFile(`./assets/${type}-curated.json`, JSON.stringify(imgCountries, null, 4)); console.log('Done!'); } init(); // fetchCountries();