2022-02-16 10:05:35 +00:00
|
|
|
/**
|
|
|
|
* Language files are fetched from the frappe/books repo
|
|
|
|
* the language files before storage have a ISO timestamp
|
|
|
|
* prepended to the file.
|
|
|
|
*
|
|
|
|
* This timestamp denotes the commit datetime, update of the file
|
|
|
|
* takes place only if a new update has been pushed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs/promises');
|
|
|
|
const path = require('path');
|
2022-02-16 10:41:08 +00:00
|
|
|
const fetch = require('node-fetch').default;
|
2022-02-16 10:05:35 +00:00
|
|
|
const { splitCsvLine } = require('../scripts/helpers');
|
|
|
|
|
2022-02-17 10:12:05 +00:00
|
|
|
const VALENTINES_DAY = 1644796800000;
|
|
|
|
|
2022-02-16 10:05:35 +00:00
|
|
|
async function getLanguageMap(code, isDevelopment = false) {
|
|
|
|
const contents = await getContents(code, isDevelopment);
|
|
|
|
return getMapFromContents(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getContents(code, isDevelopment) {
|
|
|
|
if (isDevelopment) {
|
2022-02-16 10:41:08 +00:00
|
|
|
const filePath = path.resolve('translations', `${code}.csv`);
|
|
|
|
const contents = await fs.readFile(filePath, { encoding: 'utf-8' });
|
|
|
|
return ['', contents].join('\n');
|
2022-02-16 10:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let contents = await getContentsIfExists();
|
|
|
|
if (contents.length === 0) {
|
2022-02-17 10:12:05 +00:00
|
|
|
contents = (await fetchAndStoreFile(code)) ?? contents;
|
2022-02-16 10:05:35 +00:00
|
|
|
} else {
|
2022-02-17 10:12:05 +00:00
|
|
|
contents = (await getUpdatedContent(code, contents)) ?? contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!contents || contents.length === 0) {
|
|
|
|
throwCouldNotFetchFile(code);
|
2022-02-16 10:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMapFromContents(contents) {
|
|
|
|
contents = contents.split('\n').slice(1);
|
|
|
|
return contents
|
|
|
|
.map(splitCsvLine)
|
|
|
|
.filter((l) => l.length >= 2)
|
|
|
|
.reduce((acc, l) => {
|
|
|
|
const key = l[0].slice(1, -1);
|
|
|
|
const translation = l[1].slice(1, -1);
|
|
|
|
acc[key] = { translation };
|
|
|
|
|
|
|
|
const context = l.slice(2);
|
|
|
|
if (context.length) {
|
|
|
|
acc.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:41:08 +00:00
|
|
|
async function getContentsIfExists(code) {
|
2022-02-16 10:05:35 +00:00
|
|
|
const filePath = getFilePath(code);
|
|
|
|
try {
|
|
|
|
return await fs.readFile(filePath, { encoding: 'utf-8' });
|
|
|
|
} catch (err) {
|
|
|
|
if (err.errno !== -2) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2022-02-16 10:41:08 +00:00
|
|
|
}
|
2022-02-16 10:05:35 +00:00
|
|
|
|
|
|
|
async function fetchAndStoreFile(code, date) {
|
2022-02-17 10:12:05 +00:00
|
|
|
let res = await fetch(
|
|
|
|
`https://api.github.com/repos/frappe/books/contents/translations/${code}.csv`
|
|
|
|
);
|
|
|
|
|
|
|
|
let contents = undefined;
|
|
|
|
if (res.status === 200) {
|
|
|
|
const resJson = await res.json();
|
|
|
|
contents = Buffer.from(resJson.content, 'base64').toString();
|
|
|
|
} else {
|
|
|
|
res = await fetch(
|
|
|
|
`https://raw.githubusercontent.com/frappe/books/master/translations/${code}.csv`
|
|
|
|
);
|
2022-02-16 10:05:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:12:05 +00:00
|
|
|
if (!contents && res.status === 200) {
|
|
|
|
contents = await res.text();
|
2022-02-16 10:41:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:12:05 +00:00
|
|
|
if (!date && contents) {
|
|
|
|
date = await getLastUpdated(code);
|
|
|
|
}
|
2022-02-16 10:05:35 +00:00
|
|
|
|
2022-02-17 10:12:05 +00:00
|
|
|
if (contents) {
|
|
|
|
contents = [date.toISOString(), contents].join('\n');
|
|
|
|
await storeFile(code, contents);
|
|
|
|
}
|
2022-02-16 10:05:35 +00:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUpdatedContent(code, contents) {
|
|
|
|
const [shouldUpdate, date] = await shouldUpdateFile(code, contents);
|
|
|
|
if (!shouldUpdate) {
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await fetchAndStoreFile(code, date);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function shouldUpdateFile(code, contents) {
|
|
|
|
const date = await getLastUpdated(code);
|
|
|
|
const oldDate = new Date(contents.split('\n')[0]);
|
2022-02-17 10:12:05 +00:00
|
|
|
const shouldUpdate = date > oldDate || +oldDate === VALENTINES_DAY;
|
|
|
|
|
|
|
|
return [shouldUpdate, date];
|
2022-02-16 10:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getLastUpdated(code) {
|
|
|
|
const url = `https://api.github.com/repos/frappe/books/commits?path=translations%2F${code}.csv&page=1&per_page=1`;
|
2022-02-16 10:41:08 +00:00
|
|
|
const resJson = await fetch(url).then((res) => res.json());
|
2022-02-16 10:05:35 +00:00
|
|
|
|
2022-02-17 10:12:05 +00:00
|
|
|
try {
|
|
|
|
return new Date(resJson[0].commit.author.date);
|
|
|
|
} catch {
|
|
|
|
return new Date(VALENTINES_DAY);
|
2022-02-16 10:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilePath(code) {
|
|
|
|
return path.resolve(process.resourcesPath, 'translations', `${code}.csv`);
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:12:05 +00:00
|
|
|
function throwCouldNotFetchFile(code) {
|
|
|
|
throw new Error(`Could not fetch translations for '${code}'.`);
|
2022-02-16 10:05:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 10:41:08 +00:00
|
|
|
async function storeFile(code, contents) {
|
2022-02-16 10:05:35 +00:00
|
|
|
const filePath = getFilePath(code);
|
2022-02-16 10:41:08 +00:00
|
|
|
const dirname = path.dirname(filePath);
|
|
|
|
await fs.mkdir(dirname, { recursive: true });
|
2022-02-16 10:05:35 +00:00
|
|
|
await fs.writeFile(filePath, contents, { encoding: 'utf-8' });
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { getLanguageMap };
|