-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathgenerateCodeList.js
52 lines (43 loc) · 1.4 KB
/
generateCodeList.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
const fs = require('fs')
const { promisify } = require('util')
const PapaParse = require('papaparse')
const fetchNode = require('node-fetch')
require('dotenv').config()
const writeFilePromise = promisify(fs.writeFile)
const WRITE_PATH = 'countryCodeList.json'
const { SPREADSHEET_KEY } = process.env
const { API_KEY } = process.env
function writeToJsonFile(countryList) {
writeFilePromise(WRITE_PATH, JSON.stringify(countryList), 'utf8')
}
function writeToTypescriptFile(countryList) {
writeFilePromise('./cli/countryCodeList.ts', `export const countryCodeList = ${JSON.stringify(countryList)}`, 'utf8')
}
function fetchSheet({
spreadsheetId, sheetName, apiKey, complete
}) {
const url = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${sheetName}?key=${apiKey}`
return fetchNode(url).then((response) => response.json().then((result) => {
const data = PapaParse.parse(PapaParse.unparse(result.values), {
header: true
})
complete(data)
}))
}
function promisifyFetchSheetData() {
return new Promise((resolve) => {
fetchSheet({
spreadsheetId: SPREADSHEET_KEY,
sheetName: 'Main',
apiKey: API_KEY,
complete(results) {
resolve(results.data)
console.log(`Code list generated: ${results.data.length + 1} flags`)
}
})
})
}
promisifyFetchSheetData().then((data) => {
writeToJsonFile(data)
writeToTypescriptFile(data)
})