forked from umami-software/umami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload-country-names.js
37 lines (30 loc) · 1.05 KB
/
download-country-names.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const fs = require('fs-extra');
const path = require('path');
const https = require('https');
const chalk = require('chalk');
const src = path.resolve(__dirname, '../lang');
const dest = path.resolve(__dirname, '../public/intl/country');
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 => {
await fs.ensureDir(dest);
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);