schat2-clive/assets/countries.js

31 lines
892 B
JavaScript
Raw Normal View History

2022-11-02 05:16:17 +00:00
'use strict';
const fs = require('fs').promises;
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]));
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];
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],
};
}));
await fs.writeFile('./assets/countries-curated.json', JSON.stringify(imgCountries, null, 4));
2022-11-02 05:16:17 +00:00
console.log('Done!');
}
init();