schat2-clive/assets/countries.js

31 lines
892 B
JavaScript

'use strict';
const fs = require('fs').promises;
const crypto = require('crypto');
const countries = require('./countries.json');
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();