-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
149 lines (123 loc) · 4.13 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const { performance } = require('perf_hooks')
const startTime = performance.now()
const fs = require('fs')
const mkdirp = require('mkdirp')
const { getSheets } = require('./googleapis-client')
const path = require('path')
const { token, target, sheets, moduleType, muteEslintQuotes, customOptions, auth } = require('./options')
;(async() => {
const allSheetsWithTranslations = await loadTranslations(customOptions, auth)
saveTranslationsToFiles(allSheetsWithTranslations)
log([
`Done downloading translations from sheets: ${sheets.join(', ')} `,
`in ${Math.floor(performance.now() - startTime)}ms\n`
].join(''))
})()
async function loadTranslations({ getGoogleAuthCredentials, getValueMapper }, auth) {
let googleAuthCredentials = getGoogleAuthCredentials()
// using auth from customOptions.getGoogleAuthCredentials()
if (!auth && (!googleAuthCredentials || Object.values(googleAuthCredentials).join('').length === 0)) {
throw Error(
'Google Auth Credentials are empty, function getGoogleAuthCredentials() from --customOptions file '
+ 'should return object with "private_key" and "client_email".'
+ `Instead, received: ${JSON.stringify(googleAuthCredentials, null, 2)}`
)
}
// using auth from auth.json
if (auth) {
if (!auth.private_key ||!auth.client_email) {
throw Error(
'Google Auth Credentials are empty, auth option '
+ 'should be an object with "private_key" and "client_email".'
+ `Instead, received: ${JSON.stringify(auth, null, 2)}`
)
}
googleAuthCredentials = {
client_email: auth.client_email,
private_key: auth.private_key
}
}
const sheetsAsJson = await getSheets({
credentials: googleAuthCredentials,
spreadsheetId: token,
sheetsNames: sheets
})
const allSheetsWithTranslations = {}
for (let sheet of sheetsAsJson) {
allSheetsWithTranslations[sheet.title] = convertSheetJsonToTranslation(sheet)
}
return allSheetsWithTranslations
function convertSheetJsonToTranslation(sheet) {
const [, ...locales] = sheet.header
return locales.reduce((acc, locale) => ({
...acc,
[locale]: rowsToTranslations(sheet.rows, sheet.header.indexOf(locale))
}), {})
}
function rowsToTranslations(rows, index) {
return rows
.reduce((acc, row) => {
const key = row[0]
if (!key) return acc
return ({
...acc,
[key]: getValueMapper(row[index])
})
}, {})
}
}
function saveTranslationsToFiles(allSheetsWithTranslations) {
Object.entries(allSheetsWithTranslations)
.forEach(([sheetTitle, sheetTranslations]) => {
Object.entries(sheetTranslations)
.forEach(([locale, localeTranslations]) => {
const localeModuleSource = [
!muteEslintQuotes && eslintQuotes(),
beginModule(),
JSON.stringify(localeTranslations, null, 2),
endModule(),
'\n'
].join('')
const dir = target
.replace(/{locale}/, locale)
.replace(/{sheet}/, sheetTitle)
mkdirp.sync(path.dirname(dir))
// TODO Might be obsolete: empty sheets were removed before
const translationsCount = Object.keys(localeTranslations).length
const emptyWarning = translationsCount === 0 ? '☢ ' : ''
process.stdout.write(`${emptyWarning}Writing ${dir}, ${translationsCount} translations...`)
fs.writeFileSync(path.normalize(dir), localeModuleSource, 'utf-8', err => {
if (err) console.error(err)
})
log(' Done!')
})
})
function eslintQuotes() {
switch (moduleType) {
case 'AMD':
case 'ESM':
return '/* eslint quotes: 0 */\n'
case 'JSON':
return ''
}
}
function beginModule() {
switch (moduleType) {
case 'AMD': return 'define('
case 'ESM': return 'export default '
case 'JSON': return ''
}
}
function endModule() {
switch (moduleType) {
case 'AMD':
return ')'
case 'ESM':
case 'JSON':
return ''
}
}
}
function log() {
console.log.apply(console.log, arguments)
}