2022-11-02 05:16:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs').promises;
|
2022-11-02 19:46:47 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
const countries = require('./countries.json');
|
2022-11-02 05:16:17 +00:00
|
|
|
|
|
|
|
async function init() {
|
|
|
|
const countryImgs = await fs.readdir('./assets/countries');
|
|
|
|
const countriesByCode = Object.fromEntries(countries.map((country) => [country.alpha2, country]));
|
|
|
|
|
2022-11-02 19:46:47 +00:00
|
|
|
const imgCountries = await Promise.all(countryImgs.filter((file) => !['encoded', 'disabled'].includes(file)).map(async (file) => {
|
2022-11-02 05:16:17 +00:00
|
|
|
const code = file.match(/(\w+).png/)[1];
|
2022-11-02 19:46:47 +00:00
|
|
|
const filename = `${crypto.createHash('md5').update(code).digest('hex').slice(0, 4)}.png`;
|
2022-11-02 05:16:17 +00:00
|
|
|
|
|
|
|
await fs.copyFile(`./assets/countries/${file}`, `./assets/countries/encoded/${filename}`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
file: filename,
|
|
|
|
code,
|
|
|
|
...countriesByCode[code],
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
|
2022-11-02 19:46:47 +00:00
|
|
|
await fs.writeFile('./assets/countries-curated.json', JSON.stringify(imgCountries, null, 4));
|
2022-11-02 05:16:17 +00:00
|
|
|
|
|
|
|
console.log('Done!');
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|