-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetData.js
executable file
·47 lines (40 loc) · 1.2 KB
/
getData.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
#!/usr/bin/env node
const fs = require('fs');
const gsjson = require('google-spreadsheet-to-json');
let credentials;
try {
credentials = require('./service-account.json');
} catch (e) {
console.warn('no spreadsheet access file found, trying env variables:');
credentials = JSON.parse(process.env.SERVICE_ACCOUNT)
}
const fetchData = (spreadsheetId) =>
gsjson({
spreadsheetId,
credentials
})
module.exports.fetchData = fetchData;
// To avoid nextjs fetching the sheet data once for each locale, we cache it
let data = null;
module.exports.fetchDataCached = async (sheetId) => {
if (data) {
return data;
}
data = await fetchData(sheetId);
setTimeout(() => {
data = null
}, 360_000);
return data;
};
// Running "node getData.js" downloads the locales only
if (require.main === module) {
fetchData(process.argv[2])
.then(function (result) {
console.log('found ' + result.length + ' locales');
fs.writeFileSync('./locales.json', JSON.stringify(result.map(({locale}) => locale), null, 2), 'utf8');
})
.catch(function (err) {
console.error(err.message);
console.error(err.stack);
});
}