-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate-countries.js
81 lines (62 loc) · 1.8 KB
/
generate-countries.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable @typescript-eslint/no-var-requires */
/* This script file generates contry names, and prepares them for translation */
const { inspect } = require('util')
const fs = require('fs')
const { countries } = require('countries-list')
const { default: flag } = require('country-code-emoji')
const prettier = require('prettier')
const path = require('path')
let prettierConfig
;(async () => {
prettierConfig = await prettier.resolveConfig(
path.resolve(__dirname, '../.prettierrc.js')
)
})()
/* Optimize countries.json file from
https://github.com/annexare/Countries/blob/master/data/countries.json
- keep only the coutry name and code
- group by continent
e.g.
{
"EU": [
{ "name": "Andorra", "code": "AD", flag:"emoji" cont:"EU" },
{ "name": "Albania", "code": "AL", flag:"emoji" cont:"EU"},
]
}
*/
function countriesByContinent(countries) {
const result = {}
for (const countryCode in countries) {
const country = countries[countryCode]
if (!result[country.continent]) {
result[country.continent] = []
}
// Kosovo is Serbia
if (countryCode === 'XK') {
continue
}
result[country.continent].push({
name: `t\`${country.name}\``,
code: countryCode,
flag: flag(countryCode),
cont: country.continent
})
}
return result
}
const result = countriesByContinent(countries)
const dir = `${__dirname}/../src/generated`
fs.mkdirSync(dir, { recursive: true })
const file = fs.createWriteStream(`${dir}/countries.js`)
const data = `
import { t } from '@lingui/macro'
export function countries(){
const data = ${inspect(result).replace(/'(?<name>t`.+?`)'/g, '$<name>')}
return data
}`
const formatted = prettier.format(data, {
...prettierConfig,
parser: 'babel'
})
file.write(formatted)
file.end()