umami/scripts/download-country-names.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-02-18 06:42:42 +01:00
/* eslint-disable no-console, @typescript-eslint/no-var-requires */
2022-03-17 05:50:24 +01:00
const fs = require('fs-extra');
2020-10-03 20:21:44 +02:00
const path = require('path');
const https = require('https');
const chalk = require('chalk');
2023-08-26 00:06:47 +02:00
const src = path.resolve(__dirname, '../src/lang');
2022-06-27 06:00:29 +02:00
const dest = path.resolve(__dirname, '../public/intl/country');
2020-10-03 20:21:44 +02:00
const files = fs.readdirSync(src);
const getUrl = locale =>
`https://raw.githubusercontent.com/umpirsky/country-list/master/data/${locale}/country.json`;
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
const download = async files => {
2022-03-17 05:50:24 +01:00
await fs.ensureDir(dest);
2020-10-03 20:21:44 +02:00
await asyncForEach(files, async file => {
const locale = file.replace('-', '_').replace('.json', '');
const filename = path.join(dest, file);
if (!fs.existsSync(filename)) {
await new Promise(resolve => {
https.get(getUrl(locale), res => {
console.log('Downloaded', chalk.greenBright('->'), filename);
resolve(res.pipe(fs.createWriteStream(filename)));
});
});
}
});
};
download(files);